Construction of elliptical orbits

I am trying to write code that depicts the elliptic paths of an object using the equation for the ellipse r = a (1-e ^ 2) / (1 + e * cos (theta)). I would also like this data to be placed in an array for another use.

from numpy import *#Imports Python mathematical functions library import matplotlib.pyplot as plt #Imports plot library from pylab import * a = 5 e = 0.3 theta = 0 while theta <= 2*pi: r = (a*(1-e**2))/(1+e*cos(theta)) print("r = ",r,"theta = ",theta) plt.polar(theta, r) theta += pi/180 plt.show() 

The code calculates the correct values ​​for r and theta, but the graph is empty. A polar graph window will appear, but nothing has been built.

Please, help. Thanks in advance.

+4
source share
2 answers

Do not call plt.polar once for each point. Instead, call it once, with all the data as input:

 import numpy as np #Imports Python mathematical functions library import matplotlib.pyplot as plt #Imports plot library cos = np.cos pi = np.pi a = 5 e = 0.3 theta = np.linspace(0,2*pi, 360) r = (a*(1-e**2))/(1+e*cos(theta)) plt.polar(theta, r) print(np.c_[r,theta]) plt.show() 

enter image description here


By the way, numpy can perform calculations as two-line, instead of using while-loop:

 theta = np.linspace(0,2*pi, 360) # 360 equally spaced values between 0 and 2*pi r = (a*(1-e**2))/(1+e*cos(theta)) 

This defines theta and r as numpy arrays (rather than single values).

+8
source

I think you need to do points.append([theta,r]) and then at the end of plt.polar(points) ... which also makes a fuzzy design

 from numpy import *#Imports Python mathematical functions library import matplotlib.pyplot as plt #Imports plot library from pylab import * a = 5 e = 0.3 theta = 0 points = [] while theta <= 2*pi: r = (a*(1-e**2))/(1+e*cos(theta)) print("r = ",r,"theta = ",theta) points.append((theta, r)) theta += pi/180 #plt.polar(points) #this is cool but probably not what you want plt.polar(*zip(*points)) plt.show() 
+2
source

Source: https://habr.com/ru/post/1433632/


All Articles