Python - Using Relativistic Redshift/Observed Wavelenght to Calculate Velocity of Star/Galaxy/Quasar

by matt392 in Circuits > Software

1449 Views, 3 Favorites, 0 Comments

Python - Using Relativistic Redshift/Observed Wavelenght to Calculate Velocity of Star/Galaxy/Quasar

SampleGalaxySpeed.png
FormulaRelativisticRedshift.jpg
FlowchartVelocityStarGalaxy.png

# Updated Oct 2024 to include floating numbers to be entered

print ("Calculate the relativistc redshift and velocity of a star or galaxy.")

# Function to find redshift

def calculateRedshift():

# Observed wavelength entered by user and converted to an integer

observedWavelength = float(input("Enter the observed wavelength in Angstroms: "))

# Wavelength of element entered by user and converted to an integer

elementWavelength = float(input("Enter the element's wavelength in Angstroms: "))

# Calculate the change or shift in the wavelength

changeWavelength = observedWavelength - elementWavelength

print ("The redshift formula is: redshift = (change in wavelength/wavelength of the element)")

redshift = (changeWavelength/elementWavelength)

print ("The redhshift of the star or galaxy is:", redshift)

return redshift

# Function to calculate velocity of the star or galaxy

def calculateVelocity (redshiftCalculated):

print("The relativistic redshift formula is: ( (((redshiftCalculated+1)^2) - 1)/(((redshiftCalculated+1)^2) + 1) ) * 300000)")

velocity = (( (((redshiftCalculated+1)**2) - 1)/ (((redshiftCalculated+1)**2) + 1) ) * 300000)

return velocity

# Call the Redshift function

redshiftCalculated = calculateRedshift()

# Call the velocity function

velocity = calculateVelocity(redshiftCalculated)

print ("The velocity is: ",velocity, "km/s")

print("------")

print ("Thank you to Professor Morgan at the University of Northern Iowa for the formula and explanation.")

print ("Note that a positive value means the object is moving away from Earth.")

print ("A negative value means that the object is moving towards Earth.")

Python - Using Relativistic Redshift to Calculate Velocity of Star/Galaxy/Quasar

FlowchartVelocityStarGalaxy.png

# Updated Oct 2024 to include floating numbers to be entered

print ("Calculate the relativistc redshift and velocity of a star or galaxy.")


# Function to find redshift

def calculateRedshift():

# Observed wavelength entered by user and converted to an integer

observedWavelength = float(input("Enter the observed wavelength in Angstroms: "))

# Wavelength of element entered by user and converted to an integer

elementWavelength = float(input("Enter the element's wavelength in Angstroms: "))

# Calculate the change or shift in the wavelength

changeWavelength = observedWavelength - elementWavelength

print ("The redshift formula is: redshift = (change in wavelength/wavelength of the element)")

redshift = (changeWavelength/elementWavelength)

print ("The redhshift of the star or galaxy is:", redshift)

return redshift


# Function to calculate velocity of the star or galaxy

def calculateVelocity (redshiftCalculated):

print("The relativistic redshift formula is: ( (((redshiftCalculated+1)^2) - 1)/(((redshiftCalculated+1)^2) + 1) ) * 300000)")

velocity = (( (((redshiftCalculated+1)**2) - 1)/ (((redshiftCalculated+1)**2) + 1) ) * 300000)

return velocity


# Call the Redshift function

redshiftCalculated = calculateRedshift()


# Call the velocity function

velocity = calculateVelocity(redshiftCalculated)


print ("The velocity is: ",velocity, "km/s")

print("------")

print ("Thank you to Professor Morgan at the University of Northern Iowa for the formula and explanation.")

print ("Note that a positive value means the object is moving away from Earth.")

print ("A negative value means that the object is moving towards Earth.")