Exercise 4.4

Exercise 4.4#

Unless stated otherwise, for this module you may make use of any part of the Python standard library and the packages presented in these notes. The challenge for this activity, however, is to only use what you’ve learned in this chapter so far to complete the question. In other words, try to see if you can complete this exercise without using if statements, for loops, custom functions and methods from other Python modules.

Question 1#

Write a script that calculates the following and prints the results:

  1. \(\sqrt{2}\)

  2. \((3.6/4.7)^4\)

  3. \(\frac{3\times5.789}{3.32^6 + 10}\)

Question 2#

Write a script that swaps the values of two variables . Print out both the initial values and the final values. So if you start the script like this:

#variables to swap
var1 = 3
var2 = 5

print('Initial values:')
print('var1 is', var1)
print('var2 is', var2)
print(' ')

and then after you have swapped the variables, end the script like this:

print('Final values:')
print('var1 is', var1)
print('var2 is', var2)

you should see the following output:

Initial values:
var1 is 3
var2 is 5

Final values:
var1 is 5
var2 is 3

Question 3#

The phase of a variable star with period \(P\) is the decimal part of \(t/P\), where \(t\) is the time elapsed since a given date. Write a python program that calculate the phase when \(t = 3456.789\) days and the period is \(P = 3.1416\) days.

[The answer is 0.327540106952].

Question 4#

The most energy efficient trajectory between Earth and Mars is an orbit with Earth at perihelion (\(R_2 = 1.496\times10^{8}\)km) and Mars at aphelion (\(R_1 = 2.279\times10^{8}\)km). Calculate the semi-major axis \(a = \tfrac{1}{2} (R_1 + R_2)\) and eccentricity \(e = (R_1 - R_2)/(R_1 + R_2)\). The product of the gravitational constant and solar mass, \(\mu_S = GM_\circ = 1.32\times10^{20}\) Newton.m\(^2\)/kg . Write a python program that:

  1. Calculates the velocity of the spacecraft (in km/s) at the Earth’s location using

\[ V = \sqrt{\frac{\mu_S(1 + e)}{a (1 - e)}} \]
  1. Calculates the velocity of the Earth (in km/s) itself using

\[ V_E = \sqrt{\mu_S/R_2} \]
  1. Calculates the duration of the journey to Mars (in days) using

\[ T = \pi a^{3/2}/\mu_S^{1/2} \]

Your output should look like this:

Velocity at perihelion = 32.64 km/s
Velocity of Earth = 29.70 km/s
Duration = 259.5 days