Plotting elliptical orbits

前端 未结 2 769
青春惊慌失措
青春惊慌失措 2021-01-13 13:47

I\'m trying to write a code that plots the elliptical paths of an object using the equation for the ellipse r=a(1-e^2)/(1+e*cos(theta)). I\'d also like this data to be put

2条回答
  •  长情又很酷
    2021-01-13 14:40

    Do not call plt.polar once for every 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 do the calculation as a two-liner, instead of using a 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).

提交回复
热议问题