Sharednotebooks / Orbital Mechanics Mini-Project.ipynbOpen in CoCalc
Authors: Nabih Estefan, Ivonne Munoz
Views : 20
In [1]:
# Configure Jupyter so figures appear in the notebook %matplotlib inline # Configure Jupyter to display the assigned value after an assignment %config InteractiveShell.ast_node_interactivity='last_expr_or_assign' # import functions from the modsim.py module from modsim import * import math
In [2]:
m = UNITS.meter s = UNITS.second km = UNITS.kilometer h = UNITS.hour kg = UNITS.kilogram
kilogram
In [3]:
def make_system(): ME=5.972e24 * kg MS=1.989e30 *kg G = 6.673 * (10 ** -11) * m**3 / (s**2 * kg) d=149600000000 * m v=0 * m/s init = State(d=d, v=v) return System(init=init, ME = ME, MS = MS, G = G, t_end = 10000000000 * s, dt = 86400 * s)
In [4]:
def slope_func(state, t, system): d, v = state ME, MS, G = system.ME, system.MS, system.G a = (G * MS) / (d ** 2) dddt = v dvdt = -a return dddt, dvdt
In [5]:
def event_func(state, t, system): """Return the height of the penny above the sidewalk. """ d, v = state return d
In [6]:
def plot_position(results): plot(results.d, label='Distance') decorate(xlabel='Time (days)', ylabel='Position (thousands of km)')
In [7]:
system = make_system() results, details = run_ode_solver(system, slope_func, events=event_func) details
values
success True
message A termination event occurred.
In [8]:
results.index /= 86400 results.d /= 30000000 plot_position(results)
In [9]:
results.tail()
d v
61.000000 1219.1143114566005 meter -74042.58746572041 meter / second
62.000000 993.5263844032074 meter -84425.25841874613 meter / second
63.000000 731.7937189278599 meter -101476.43675857506 meter / second
64.000000 405.2796152048905 meter -140568.79364469112 meter / second
64.784597 6.357828776041667e-14 meter -564836.071922193 meter / second
In [10]:
def make_system_2d(): ME=5.972e24 * kg MS=1.989e30 *kg G = 6.673 * (10 ** -11) * m**3 / (s**2 * kg) d = Vector(149600000000, 0) * m v = Vector(0, 30000) * m / s init = State(d=d, v=v) return System(init=init, ME = ME, MS = MS, G = G, t_end = 32400000* s, dt = 86400 * s)
In [11]:
def slope_func_2d(state, t, system): d, v = state ME, MS, G = system.ME, system.MS, system.G a = -((d.hat() * G * MS)/(d.mag ** 2)) ddxdt = v.x ddydt = v.y dvxdt = a.x dvydt = a.y dddt = Vector(ddxdt, ddydt) dvdt = Vector(dvxdt, dvydt) return dddt, dvdt
In [12]:
def plot_position_2d(results): xs = results_2d.d.extract('x') xs /= 1000000000 xs.index /= 86400 ys = results_2d.d.extract('y') ys /= 1000000000 ys.index /= 86400 plt.plot(xs, ys,':', color = "blue") plt.plot(0,0, color="yellow") decorate(xlabel='Distance in X (millions of kilometers)', ylabel='Distance in Y (millions of kilometers)')
In [13]:
system_2d = make_system_2d() results_2d, details_2d = run_ode_solver(system_2d, slope_func_2d)
In [14]:
plot_position_2d(results_2d)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-14-700bebde5cbe> in <module> ----> 1 plot_position_2d(results_2d) <ipython-input-12-f290e9ab0611> in plot_position_2d(results) 8 ys.index /= 86400 9 ---> 10 plt.plot(xs, ys,':', color = "blue") 11 plt.plot(0,0, color="yellow") 12 /srv/conda/envs/notebook/lib/python3.7/site-packages/matplotlib/pyplot.py in plot(scalex, scaley, data, *args, **kwargs) 2793 return gca().plot( 2794 *args, scalex=scalex, scaley=scaley, **({"data": data} if data -> 2795 is not None else {}), **kwargs) 2796 2797 /srv/conda/envs/notebook/lib/python3.7/site-packages/matplotlib/axes/_axes.py in plot(self, scalex, scaley, data, *args, **kwargs) 1666 lines = [*self._get_lines(*args, data=data, **kwargs)] 1667 for line in lines: -> 1668 self.add_line(line) 1669 self.autoscale_view(scalex=scalex, scaley=scaley) 1670 return lines /srv/conda/envs/notebook/lib/python3.7/site-packages/matplotlib/axes/_base.py in add_line(self, line) 1900 line.set_clip_path(self.patch) 1901 -> 1902 self._update_line_limits(line) 1903 if not line.get_label(): 1904 line.set_label('_line%d' % len(self.lines)) /srv/conda/envs/notebook/lib/python3.7/site-packages/matplotlib/axes/_base.py in _update_line_limits(self, line) 1922 Figures out the data limit of the given line, updating self.dataLim. 1923 """ -> 1924 path = line.get_path() 1925 if path.vertices.size == 0: 1926 return /srv/conda/envs/notebook/lib/python3.7/site-packages/matplotlib/lines.py in get_path(self) 1025 """ 1026 if self._invalidy or self._invalidx: -> 1027 self.recache() 1028 return self._path 1029 /srv/conda/envs/notebook/lib/python3.7/site-packages/matplotlib/lines.py in recache(self, always) 668 if always or self._invalidx: 669 xconv = self.convert_xunits(self._xorig) --> 670 x = _to_unmasked_float_array(xconv).ravel() 671 else: 672 x = self._x /srv/conda/envs/notebook/lib/python3.7/site-packages/matplotlib/cbook/__init__.py in _to_unmasked_float_array(x) 1388 return np.ma.asarray(x, float).filled(np.nan) 1389 else: -> 1390 return np.asarray(x, float) 1391 1392 /srv/conda/envs/notebook/lib/python3.7/site-packages/numpy/core/_asarray.py in asarray(a, dtype, order) 83 84 """ ---> 85 return array(a, dtype, copy=False, order=order) 86 87 ValueError: setting an array element with a sequence.
In [15]:
def sun(): r =
File "<ipython-input-15-7bee5f225b63>", line 2 r = ^ SyntaxError: invalid syntax
In [16]:
help(plot)
Help on function plot in module modsim.modsim: plot(*args, **options) Makes line plots. args can be: plot(y) plot(y, style_string) plot(x, y) plot(x, y, style_string) options are the same as for pyplot.plot
In [ ]: