Python - Kepler's 3rd Law of Planetary Motion

by matt392 in Circuits > Software

51 Views, 1 Favorites, 0 Comments

Python - Kepler's 3rd Law of Planetary Motion

KeplerThirdLaw.jpg
Kepler3rdLawgraphic.jpg
print ("This program will calculate Kepler's 3rd Law.")

#  We need the math module for the square root function
import math

#  Kepler's 3rd Law Formula is: P**2 = k * a**^3
#  P is period of orbit, k is a constant, a is the average distance
#  There are 3 sections of the program based on what the user wants to solve for

#  Function to solve for period of orbit
#    Solve for P (period of the orbit): enter k and a
#    Formula: p = square root of (k * (a**3) )
def solveForPeriod():
    print ("Solving for period:")
    constant = int(input("Please enter the constant: ") )
    averageDistance = int(input("Please enter the average distance: ") )
    squreRootNumber = (constant * (averageDistance**3))
    periodOfOrbit = math.sqrt(squreRootNumber)
    print("The period of Orbit is: ", periodOfOrbit)
    
#  Function to solve for average distance of object
#    Solve for a (average distance of the object): enter P and k
#    Formula: a = cube root of ( (p**2) / k )
def solveForAverageDistance():
    print ("Solving for average distance:")
    periodOfOrbit = int(input("Please enter the period of orbit: ") )
    constant = int(input("Please enter the constant: ") )
    cubeRootNumber = ((periodOfOrbit**2)/constant)
    # Now raise the number to 1/3 to get the cube root
    #  Note that you cannot raise it to .33 because it will not be accurate
    averageDistance = cubeRootNumber**(1/3)
    print("The average distance is: ", averageDistance)
    
#  Function to solve for k
#    Solve for k (constant which varies): enter P and a
#    Formula: k = (p**2)/(a**3)
def solveForK():
    print ("Solving for k the Kepler constant:")
    periodOfOrbit = int(input("Please enter the period of orbit: ") )
    averageDistance = int(input("Please enter the average distance: ") )
    constant = (periodOfOrbit**2)/(averageDistance**3)
    print ("The constant is: ", constant)

continueCalculations = "y"

while (continueCalculations=="y"):
    #  Ask user if they want to solve for period, average distance or constant
    print ("If you would like to solve for period, enter 1.")
    print ("If you would like to solve for average distance, enter 2.")
    print ("If you would like to solve for Kepler constant, enter 3.")
    choiceToSolve = int(input("Enter 1, 2 or 3: ") )

    # if choiceToSolve is 1, run function solveForPeriod()
    # if choiceToSolve is 2, run function solveForAverageDistance()
    # if choicetoSolve is 3, run function solveForK()
    if choiceToSolve == 1:
        solveForPeriod()
    elif choiceToSolve == 2:
        solveForAverageDistance()
    elif choiceToSolve == 3:
        solveForK()
    else:
        print("Please enter 1, 2 or 3.")

    continueCalculations = input("Would like to do another calculation of Kepler's 3rd Law? (y/n): ")
    
print("==================================")
print ("Thank you to Professor Morgan at the University of Northern Iowa for the formula and explanation.")

Downloads