- #1
computerex
- 68
- 0
Hello guys. I am taking an introductory course in computer programming. For one assignment I was to make up a word problem, then write a program to solve it. I wrote a program to calculate the radius of a circular orbit based on the orbiting object's current velocity, and other parameters. Can someone check my math, to see if there are any obvious error?
Code:
# Programmer : Mohd Ali
# Date : 6/29/2010
# Description: calculates the radius of an orbit based on the
# current velocity that would give a circular orbit
# based on the uniform circular motion equation
G = 6.67300e-11 # acceleration due to gravity
# the story, makes it easier to print out
story = ["You and your companions are traveling",
"happily in your little spaceship. When",
"suddenly fortune strikes! The person in",
"charge of keeping track of the periapsis",
"fell asleep, forgetting to perform the routine",
"boost burns. Now the spaceship is entering the",
"atmosphere at orbital velocities.",
"This is not going to end well...",
" ",
"But wait! Why not use the on board",
"flight computer to calculate the jump",
"coordinates to jump to a safer altitude?!",
" ",
" ",
"*pilot fires up the onboard flight computer*",
" ",
" ",
" "
]
# calculate the force of gravity between two objects r meters apart
# and weighing m1 and m2 kg respectively
def getGravForce(m1, m2, r):
return (G*m1*m2)/(r*r);
# "entry point"
def main():
# print the story
for i in range(0, len(story)):
print story[i]
# get user input
fPlanetRad = float(raw_input("Enter the major body's radius (m): "))
fPMass = float(raw_input("Enter the major body's mass (kg): "))
fVMass = float(raw_input("Enter the orbiting body's mass (kg): "))
fVAlt = float(raw_input("Enter the orbiting body's altitude (m): "))
fVel = float(raw_input("Enter the relative velocity (m/s): "))
# phrasing the problem as a word problem
print " "
print " "
print "*computer, what altitude should we jump to given that*"
print "*the planet's mass is " + str(fPMass) + " kg and our mass is*"
print "* " + str(fVMass) + " kg, our velocity with respect to the*"
print "*planet's axis of rotation is " + str(fVel) + " m/s and our*"
print "*altitude with respect to the surface is " + str(fVAlt/1000.0) + " km*"
print "*with the radius of the planet being " + str(fPlanetRad/1000.0) + "km?*"
print " "
print " "
print "calculating..."
# some calculations, should be correct. It is very late here :)
# a = v^2/r
# a - acceleration
# v - velocity
# r - distance
# r = v^2/a
# f = ma
# calculate the spaceship's acceleration with respect to the planet
# based only on the force of gravity
f = getGravForce(fPMass, fVMass, fPlanetRad+fVAlt)
a = f/fVMass
# calculate the new orbit radius
r = fVel*fVel/a
print " "
print "Force due to gravity : " + str(f) + " Newtons"
print "Acceleration due to gravity: " + str(a) + " m/s^2"
print "Radius of new orbit : " + str(r/1000.0) + " km"
print " "
# if rdiff < 0 | we are too energetic, we need to jump to a lower altitude (unlikely, as we are burning)
rdiff = r-(fVAlt+fPlanetRad)
if rdiff < 0:
print "Jump inward " + str(rdiff*-1/1000.0) + " km"
else:
if rdiff > 0:
print "Jump outward " + str(rdiff/1000.0) + " km"
else:
print "Your orbit is circular...sorry ;)"
main()