Python - Magnitude-Distance Formula for Stars/Galaxies
by matt392 in Circuits > Software
37 Views, 1 Favorites, 0 Comments
Python - Magnitude-Distance Formula for Stars/Galaxies

print ("This program will calculate magnitude-distance for stars and galaxies.") # We need the math module for base 10 log function import math # Magnitude-Distance Formula is: (m - M) = ( (-5) + (5 * (log (d) ) ) # m is apparent magnitude, M is absolute magnitude, d is distance in parsec # There are 3 sections of the program based on what the user wants to solve for # Function to solve for apparent magnitude # Solve for m (apparent magnitude): enter M and d # Formula: m = ( (-5) + (5 * ( log(d) ) ) + M ) def solveForApparentagnitude(): print ("Solving for apparent magnitude:") absoluteMagnitude = int(input("Please enter the absolute magnitude: ") ) distanceInParsecs = int(input("Please enter the distance in Parsecs: ") ) # Find the base 10 log of the distance resultLogBase10 = math.log10(distanceInParsecs) apparentMagnitude = ( (-5) + (5*resultLogBase10) + absoluteMagnitude) print("The apparent magnitude is: ", apparentMagnitude) # Function to solve for absolute magnitude # Solve for M (absolute magnitude): enter m and d # Formula: M = ( ((-5) + (5 * (log (d) ) ) ) - m ) * (-1) def solveForAbsoluteMagnitude(): print ("Solving for absolute magnitude:") apparentMagnitude = int(input("Please enter the apparent magnitude: ") ) distanceInParsecs = int(input("Please enter the distanceInParsecs: ") ) # Find the base 10 log of the distance resultLogBase10 = math.log10(distanceInParsecs) absoluteMagnitude = ( ( (-5) + (5 * resultLogBase10) ) - apparentMagnitude) * (-1) print("The absolute magnitude is: ", absoluteMagnitude) # Function to solve for distance # Solve for distance: enter m and M # Formula: (m - M) = (-5 + (5 * (log (d) ) ) ) def solveForDistance(): print ("Solving for distance:") apparentMagnitude = int(input("Please enter the apparent magnitude: ") ) absoluteMagnitude = int(input("Please enter the absolute magnitude: ") ) logOfDistance = ( (apparentMagnitude-absoluteMagnitude) + 5 )/5 distanceInParsecs = 10**logOfDistance print ("The distance in Parsecs is: ", distanceInParsecs) continueCalculations = "y" while (continueCalculations=="y"): # Ask user if they want to solve for apparent magnitude, absolute magnitude or distance in Parsecs print ("If you would like to solve for apparent magnitude, enter 1.") print ("If you would like to solve for absolute magnitude, enter 2.") print ("If you would like to solve for distance in Parsecs, enter 3.") choiceToSolve = int(input("Enter 1, 2 or 3: ") ) # if choiceToSolve is 1, run function solveForApparentagnitude() # if choiceToSolve is 2, run function () # if choicetoSolve is 3, run function () if choiceToSolve == 1: solveForApparentagnitude() elif choiceToSolve == 2: solveForAbsoluteMagnitude() elif choiceToSolve == 3: solveForDistance() else: print("Please enter 1, 2 or 3.") continueCalculations = input("Would like to do another calculation of magnitude-distance? (y/n): ") print("==================================") print ("Thank you to Professor Morgan at the University of Northern Iowa for the formula and explanation.")