problem

  • Read Stull Chapter 2 pages 27-29 and upload a spreadsheet or program that successfully computes the true anomaly $\nu$ using (2.3b) for given a date of April 12 (day 102). Upload the xlsx file, screenshot, code listing, whatever to Connect (assignment 1).

Python solution 1: simple script


In [17]:
from numpy import sin, pi

d=102
C=2*pi
P= 365.256363 #sidereal orbital period (Stull p. 29)
dp = 4 #day of perihelion
M = C*(d - dp)/P  #Stull eq. 2.2
nu = M + 0.0333988*sin(M) +  0.0003486*sin(2*M) + 0.0000050*sin(3*M)  #Stull eq. 2.3b
print("For day 102:\nDay number = {}, M={:5.3f} rads, nu = {:5.3f} rads".format(d, M, nu))


For day 102:
Day number = 102, M=1.686 rads, nu = 1.719 rads

Python solution 2: function with for loop

Write a function in a loop following the Whirlwind Functions section


In [18]:
def find_nu(day_num):
    """
    find the true anomoly nu using stull equation 2.3b
    
    Parameters
    ----------
    
    day_num: float or array
        number of day in year
        
    Returns
    -------
    
    M, nu : float or array
       M (radians) is the mean anomaly
       nu (radians) is the true anomaly
    """
    C=2*pi
    P= 365.256363 #sidereal orbital period (Stull p. 29)
    dp = 4 #day of perihelion
    M = C*(day_num - dp)/P  #Stull eq. 2.2
    nu = M + 0.0333988*sin(M) +  0.0003486*sin(2*M) + 0.0000050*sin(3*M)  #Stull eq. 2.3b
    return M,nu

days = [4,18,32,46,60,74,88,102,116,172,266,356]  #put the days in alist
#iterate over each value in the list
for day_num in days:
    M,nu = find_nu(day_num)
    print("Day number = {}, M={:5.3f} rads, nu = {:5.3f} rads".format(day_num, M, nu))


Day number = 4, M=0.000 rads, nu = 0.000 rads
Day number = 18, M=0.241 rads, nu = 0.249 rads
Day number = 32, M=0.482 rads, nu = 0.497 rads
Day number = 46, M=0.722 rads, nu = 0.745 rads
Day number = 60, M=0.963 rads, nu = 0.991 rads
Day number = 74, M=1.204 rads, nu = 1.236 rads
Day number = 88, M=1.445 rads, nu = 1.478 rads
Day number = 102, M=1.686 rads, nu = 1.719 rads
Day number = 116, M=1.927 rads, nu = 1.958 rads
Day number = 172, M=2.890 rads, nu = 2.898 rads
Day number = 266, M=4.507 rads, nu = 4.474 rads
Day number = 356, M=6.055 rads, nu = 6.047 rads

Python solution 3: using a numpy array

Like Matlab or R, python can do vector and matrix operations that run much faster than for loops

See Pine section 3.3 for the array syntax


In [19]:
days_array = np.array(days)
days_array


Out[19]:
array([  4,  18,  32,  46,  60,  74,  88, 102, 116, 172, 266, 356])

In [20]:
M, nu = find_nu(days_array)
print(type(M),type(nu))


<class 'numpy.ndarray'> <class 'numpy.ndarray'>

In [21]:
M


Out[21]:
array([ 0.        ,  0.24082974,  0.48165948,  0.72248921,  0.96331895,
        1.20414869,  1.44497843,  1.68580817,  1.92663791,  2.88995686,
        4.50695653,  6.0551477 ])

In [22]:
nu


Out[22]:
array([ 0.        ,  0.24896043,  0.49742268,  0.74492429,  0.9910704 ,
        1.23555868,  1.47819537,  1.71890213,  1.95771431,  2.89810808,
        4.47440332,  6.04744067])

See the Whirlwind iterators section for an introduction to the zip function used below. How would you do the following in Matlab?


In [23]:
for the_day, the_M, the_nu in zip(days_array,M,nu):
    print("Day number = {}, M={:5.3f} rads, nu = {:5.3f} rads".format(the_day, the_M, the_nu))


Day number = 4, M=0.000 rads, nu = 0.000 rads
Day number = 18, M=0.241 rads, nu = 0.249 rads
Day number = 32, M=0.482 rads, nu = 0.497 rads
Day number = 46, M=0.722 rads, nu = 0.745 rads
Day number = 60, M=0.963 rads, nu = 0.991 rads
Day number = 74, M=1.204 rads, nu = 1.236 rads
Day number = 88, M=1.445 rads, nu = 1.478 rads
Day number = 102, M=1.686 rads, nu = 1.719 rads
Day number = 116, M=1.927 rads, nu = 1.958 rads
Day number = 172, M=2.890 rads, nu = 2.898 rads
Day number = 266, M=4.507 rads, nu = 4.474 rads
Day number = 356, M=6.055 rads, nu = 6.047 rads

In [ ]: