|
| 1 | +""" This module runs gradient descent on the locations of Mars on it's |
| 2 | +orbitalplane in order to find the best-fit elliptical orbit for Mars. |
| 3 | +""" |
| 4 | + |
| 5 | +# Developed by Pulkit Singh, Niheshkumar Rathod & Rajesh Sundaresan |
| 6 | +# Copyright lies with the Robert Bosch Center for Cyber-Physical Systems, |
| 7 | +# Indian Institute of Science, Bangalore, India. |
| 8 | + |
| 9 | +#----------------------------------------------------------------------------# |
| 10 | + |
| 11 | +import numpy as np |
| 12 | +import math |
| 13 | + |
| 14 | +#----------------------------------------------------------------------------# |
| 15 | + |
| 16 | +def evaluateDistance(xMars, yMars, xFocus, yFocus, majorAxis): |
| 17 | +""" Computes the cost of fitting an ellipse with one focus at the sun, |
| 18 | +and the other at [xFocus, yFocus], given the locations of Mars and |
| 19 | +the length of the major axis. |
| 20 | +
|
| 21 | +Cost is caculated using the following formula: |
| 22 | +(sum of distances from focii - length of major axis)^2 |
| 23 | +
|
| 24 | +Parameters: |
| 25 | +xMars (float): list of x-coordinates of Mars locations |
| 26 | +yMars (float): list of y-coordinates of Mars locations |
| 27 | +xFocus (float): x-coordinate of second focus |
| 28 | +yFocus (float): y-coordinate of second focus |
| 29 | +majorAxis (float): length of the major axis |
| 30 | +
|
| 31 | +Returns: |
| 32 | +squareDist (float): cost of fitting the ellipse (sum of square |
| 33 | +distances) |
| 34 | +
|
| 35 | +""" |
| 36 | + |
| 37 | +# Finding the distance of each point from the origin |
| 38 | +dist = [] |
| 39 | + |
| 40 | +# calculating (distance to origin + distance to focus2 |
| 41 | +# - major axis length)^2 for each (x, y) pair |
| 42 | +for i in range(len(xMars)): |
| 43 | +distOrigin = math.sqrt(math.pow(xMars[i], 2) + math.pow(yMars[i], 2)) |
| 44 | +distFocus = math.sqrt(math.pow((xMars[i] - xFocus), 2) |
| 45 | ++ math.pow((yMars[i] - yFocus), 2)) |
| 46 | +dist.append(math.pow((distOrigin + distFocus - majorAxis), 2)) |
| 47 | + |
| 48 | +# adding up all the square distances |
| 49 | +squareDist = sum(dist) |
| 50 | +return squareDist |
| 51 | + |
| 52 | +#----------------------------------------------------------------------------# |
| 53 | + |
| 54 | +def computeGradient(xMars, yMars, xFocus, yFocus, majorAxis): |
| 55 | +""" Computes the gradient vector with respect to the x-y coordinates of |
| 56 | +the second focus andthe length of the major axis. |
| 57 | +
|
| 58 | +gradient vector = [df/d(xFocus), df/d(yFocus), df/d(majorAxis)] |
| 59 | +
|
| 60 | +Parameters: |
| 61 | +xMars (float): list of x-coordinates of Mars locations |
| 62 | +yMars (float): list of y-coordinates of Mars locations |
| 63 | +xFocus (float): x-coordinate of second focus |
| 64 | +yFocus (float): y-coordinate of second focus |
| 65 | +majorAxis (float): length of the major axis |
| 66 | +
|
| 67 | +Returns: |
| 68 | +gradient (float list): list of required gradients |
| 69 | +
|
| 70 | +""" |
| 71 | + |
| 72 | +dxFocus = [] |
| 73 | +dyFocus = [] |
| 74 | +dmajorAxis = [] |
| 75 | + |
| 76 | +# df/d(xFocus) = (-2 * (xMars - xFocus) * evaluateDistance) / distFocus |
| 77 | +# df/d(yFocus) = (-2 * (yMars - yFocus) * evaluateDistance) / distFocus |
| 78 | +# df/d(majorAxis) = -2 * evaluateDistance |
| 79 | + |
| 80 | +# computing partial derivatives for each set of coordinates |
| 81 | +for i in range(len(xMars)): |
| 82 | +distOrigin = math.sqrt(math.pow(xMars[i], 2) + math.pow(yMars[i], 2)) |
| 83 | +distFocus = math.sqrt(math.pow((xMars[i] - xFocus), 2) |
| 84 | ++ math.pow((yMars[i] - yFocus), 2)) |
| 85 | +dist = distOrigin + distFocus - majorAxis |
| 86 | + |
| 87 | +xDiff = xMars[i] - xFocus |
| 88 | +yDiff = yMars[i] - yFocus |
| 89 | + |
| 90 | +dxFocus.append((-2 * xDiff * dist) / distFocus) |
| 91 | +dyFocus.append((-2 * yDiff * dist) / distFocus) |
| 92 | +dmajorAxis.append(-2 * dist) |
| 93 | + |
| 94 | +# returning gradient vector |
| 95 | +gradient = [float(sum(dxFocus)), |
| 96 | +float(sum(dyFocus)), |
| 97 | +float(sum(dmajorAxis))] |
| 98 | +return gradient |
| 99 | + |
| 100 | +#----------------------------------------------------------------------------# |
| 101 | + |
| 102 | +# Takes x-y coordinate matrix of different Mars locations, x-y coordinates of |
| 103 | +# the second focus and the length of the major axis as input, and runs |
| 104 | +# gradient descent to find the best fit ellipse. Returns the x-y coordinates |
| 105 | +# of the found focus, and the length of the major axis of the ellipse. |
| 106 | +def findEllipse(xMars, yMars, xf, yf, axis): |
| 107 | +""" Finds the best-fit ellipse for the Mars Orbit using gradient |
| 108 | +descent. Returns the x-y coordinates of the found focus and the |
| 109 | +length of the major axis. |
| 110 | +
|
| 111 | +Parameters: |
| 112 | +xMars (float): list of x-coordinates of Mars locations |
| 113 | +yMars (float): list of y-coordinates of Mars locations |
| 114 | +xFocus (float): x-coordinate of second focus |
| 115 | +yFocus (float): y-coordinate of second focus |
| 116 | +majorAxis (float): length of the major axis |
| 117 | +
|
| 118 | +Returns: |
| 119 | +xf (float): x-coordinate of the found focus |
| 120 | +yf (float): y-coordinate of the found focus |
| 121 | +axis (float): length of the major axis |
| 122 | +cost (float list): list of costs in each gradient descent |
| 123 | +iteration. |
| 124 | +
|
| 125 | +""" |
| 126 | + |
| 127 | + |
| 128 | +# initialising alpha as the step value |
| 129 | +alpha = 0.001 |
| 130 | + |
| 131 | +# initialising array to keep track of cost values in gradient descent |
| 132 | +cost = [] |
| 133 | + |
| 134 | +# running gradient descent |
| 135 | +for i in range (10000): |
| 136 | +# finding cost for given parameters |
| 137 | +squareDist = evaluateDistance(xMars, yMars, xf, yf, axis) |
| 138 | +#print("square dist"), squareDist |
| 139 | + |
| 140 | +# adding current cost to list of previous costs |
| 141 | +cost.append(squareDist) |
| 142 | + |
| 143 | +# finding gradient with parameter values a & b |
| 144 | +delta = computeGradient(xMars, yMars, xf, yf, axis) |
| 145 | + |
| 146 | +# updating parameter values |
| 147 | +xf = xf - (alpha * delta[0]) |
| 148 | +yf = yf - (alpha * delta[1]) |
| 149 | +axis = axis - (alpha * delta[2]) |
| 150 | + |
| 151 | +return xf, yf, axis, cost |
| 152 | + |
| 153 | +#----------------------------------------------------------------------------# |
0 commit comments