-1

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.

H.G
  • 11
  • 2
  • where use radius function? – eyllanesc Sep 21 '17 at 03:16
  • Why do you create a function and not use it?, Use the values directly: `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)) plt.polar(rad, r) plt.show()` – eyllanesc Sep 21 '17 at 03:18
  • When executing the previous code I get the following: https://imgur.com/a/sHT13 – eyllanesc Sep 21 '17 at 03:21

1 Answers1

0

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

user3666197
  • 1
  • 6
  • 50
  • 92
Anil_M
  • 10,893
  • 6
  • 47
  • 74