Cepheid Variable Star - Code Example

#!/usr/bin/python3 # =============================================================== # problen from # lco.global/spacebook/distance/ # cepheid-variable-stars-supernovae-and-distance-measurement/ # # A Cepheid variable star has a period of 3.7 days, and from # this we know its absolute magnitude is -3.1. Its apparent # magnitude is 5.5. How far away is this Cepheid variable star? # # This star is 524.8 parsecs away. # =============================================================== import math # --------------------------------------------------------------- # ---- using absolute and apparent magnitude calculate # ---- the distance in parsecs to a Cepheid variable star # --------------------------------------------------------------- def parsecs_distance(M:float,m:float) -> float: ''' M = absolute magnitude m = apparent magnitude ''' ##print() ##print(f'absolute magnitude {M}') ##print(f'apparent magnitude {m}') return math.pow(10,(m-M+5)/5) # --------------------------------------------------------------- # ---- convert parsecs to lightyears # --------------------------------------------------------------- def parsecs_to_lightyears(parsecs:float) -> float: ''' convert parsecs to lightyears 1 parsec = 3.26156 lightyears ''' return parsecs * 3.26156 # --------------------------------------------------------------- # ---- main # --------------------------------------------------------------- # ---- example 1 p = 34 # period (days) m = 23.0 # apparent magnitude M = -5.65 # absolute magnitude d = parsecs_distance(M,m) ly = parsecs_to_lightyears(d) print(f'distance to star: {d:.4e} parsecs {ly:.4e} lightyears') # ---- example 2 p = 3.7 # period (days) m = 5.5 # apparent magnitude M = -3.1 # absolute magnitude d = parsecs_distance(M,m) ly = parsecs_to_lightyears(d) print(f'distance to star: {d:.4e} parsecs {ly:.4e} lightyears')