Plotting Elliptical Orbits

1.1k views Asked by At

I am trying to plot elliptical orbits using python. Using formula r=a(1-e^2)/(1+ecos(theta)). The data points needed to be plotted are:

  • values of a: 0.39, 0.72, 1.00, 1.52
  • values of e: 0.205, 0.007, 0.017, 0.093
  • values of theta (Degrees): 77.5, 132, 103, 336

Here is my code so far:

import numpy as np
import matplotlib.pyplot as plt 

def radius(r,deg,rad):
    a=np.array([0.39,0.72,1.00,1.52])
    e=np.array([0.205,0.007,0.017,0.093])
    deg=np.array([77.5,132,103,336])
    rad=(deg*np.pi)/180
    r=(a*(1-e**2))/(1+e*np.cos(rad))
    return r,deg,rad

plt.polar(rad, r)
plt.show()

The error I seem to get is NameError: name 'r' is not defined. Since I returned the function I don't understand why it is not defined.

1

There are 1 answers

0
Anil_M On

Not sure, if your math and plot function is correct, but looking at your function radius you are pretty much hard coding a,e,deg and returning r,deg,rad.
Since you are not passing any values / variables to the function, it needs to be
radius() and not radius(r,deg,rad)

Also, you need collect the values returned by function and then use that for plotting.

import numpy as np
import matplotlib.pyplot as plt 

def radius():
    a=np.array([0.39,0.72,1.00,1.52])
    e=np.array([0.205,0.007,0.017,0.093])
    deg=np.array([77.5,132,103,336])
    rad=(deg*np.pi)/180
    r=(a*(1-e**2))/(1+e*np.cos(rad))
    return r,deg,rad

r,deg,rad = radius()
plt.polar(rad, r)
plt.show()

Figure enter image description here