Assignment 1 (BONUS)

This page has a bonus exercise for assignment 1. Please complete it if you found assignment 1 to be easy or mostly review or if you want to extra challenge!

Advanced: Using Astropy

Astropy is an astronomy Python package (read more here) which provides many useful tools. Two tools that are particularly useful for assignment 1 are astropy.constant and astropy.units.

The astropy.constant module has many scientific constants available so we don’t need to Google them and copy / paste them into our code each time. See all built-in constants here.

The astropy.units module allows us to do unit transformations so we never have to have headaches converting seconds to kilo-years ever again!.

# Import both modules
from astropy import constants as const
from astropy import units as u
import numpy as np

Here we use the constants g0 for gravitational acceleration on Earth and the unit u.s to give our time t units of seconds.

g = const.g0
t = 10 * u.s

H = (1/2) * g * t**2
print(f'On Earth, after {t} sec, we will fall down {H:1.2f}')

If instead we want to specify time in hours, no problem!

t = 1 * u.hour

Now we just make sure to convert to seconds using .cgs to turn any units into Centimeter-Gram-Second (CGS) units.

t = t.cgs
H = (1/2) * g * t**2
H = H.to(u.km)

print(f'On Earth, after {t}, we will fall donw {H:1.2f}')

If we make sure to call .cgs in our function, we can be sure that no matter what units were on the number we passed in, we will be multiplying g and t in CGS units.

def free_fall(t):
    g = const.g0.cgs
    t = t.cgs
    
    H = 0.5 * g * t**2
    return H

Finally, we can use the .to() method to do a unit conversion to another unit that astropy recognizes.

t = 10 * u.hour
free_fall(t).to(u.pc)

[hands-on] Prove g is 9.8 on Earth

Now that we’ve shown you an example of using Astropy, try to prove g is about 9.8 on Earth with Astropy using the following equation:

\[ F = ma = \frac{GMm}{r^2} \Longrightarrow a = \frac{GM}{r^2} \]

Don’t forget to look up constants available in astropy.constants and convert to consistent units before doing any computations!

# try it here

(Optional) Commit and push this file with your assignment 1 submission to receive feedback! You could also update your assignment 1 solutions to use astropy.constants and astropy.units for more practice!

See you next week!