import functions from the numpy module following the Whirlwind module section
print out a formatted string following the Whirlwind "Format strings" section
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))
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))
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]:
In [20]:
M, nu = find_nu(days_array)
print(type(M),type(nu))
In [21]:
M
Out[21]:
In [22]:
nu
Out[22]:
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))
In [ ]: