Question: In this problem you will modify the vPython code below in Trinket to send a rocket from the earth to the moon. In the simulation
In this problem you will modify the vPython code below in Trinket to send a rocket from the earth to the moon. In the simulation the earth rotates, and the moon orbits the earth. You have to time your launch so that the rocket reaches the moon instead of going off into space in another direction. The rocket launches from earth by instantly acquiring a velocity component in the radial direction (away from the center of the earth). It is successful if it hits the moon. Otherwise it fails. You should use the results of your calculations on the rocket parameters in your code (modifying them if necessary).
Calculation Loop
This type of code calculates the motion of everything in a calculation loop that makes small time steps at time interval dt. During each time step, the code updates the force, momentum, and position of each object to move it along its path.
The force is just the force of gravity from each object. The calculation loop calculates the total force on the object at the current position.
Use the updated force on the object to update the momentum using the impulse-momentum theorem, p =F tp=Ft, where tt is dt in the code.
Use the updated momentum to calculate how far the object moves in tt and update the position. Since the momentum is p =mv p=mv, we can solve for v v and use the approximation that the displacement is r =v tr=vt to update the position of the object.
The calculation loop does some tests to make see if the rocket has crashed into the earth, escaped into space, or successfully reached the moon.
The calculation loop stops when (1) the rocket reaches the moon, (2) the rocket crashes into the earth, (3) the rocket escapes into space, or (4) the total simulation time exceeds tmax (initially set to 16 days in the simulation).
GlowScript 2.6 VPython # Earth/moon/rocket orbital motion problem
# Get an external library get_library('https://trinket.io/proxy/http://dannycab.github.io/docs/glowscript/physutil.js')
## Physical constants (Units) G = 6.67*10**(-11) # N m^2/kg^2 tDay = 24*3600 # s tSidMon = 27*24*3600 + 7 *3600 + 43 * 60 + 11.6 # Moon to orbital period in s dEarthMoon = 384.4*10**6 # m radEarth = 6371000 # m radMoon = 1737000 # m
################################################################# # Launch speed (edit with a better value) vLaunch = 100 # m/s # This is the angle between the rocket and the moon at launch. # Set it to a your desired launch angle so you do not have to click. launchAngle = 30 # degree #################################################################
# Scene properties scene.width = 500 scene.height = 500 tmax = 16 * tDay # Maximum time before simulation stops.
# Moon's properties moon = sphere(pos=vector(dEarthMoon, 0, 0), radius=5 * radMoon, \ texture=textures.stucco, make_trail=True) moon.mass = 7.342*10**22 ## kg moon.p = moon.mass*vector(0,dEarthMoon*2*pi/tSidMon,0) # momentum in kg m/s
# Earth's properties omegaEarth = 2*pi/tDay # angular velocity for rotation in rad/s earth = sphere(radius=5*radEarth,texture=textures.earth) # The next two rotations are purely cosmetic so that the earth image is # oriented in the right direction at the beginning of the simulation. # The following rotation makes the north pole in the z-direction (out of # the screen) in the earth image. earth.rotate(angle = pi/2, axis=vector(1, 0, 0)) # The following rotation makes the USA face the moon so the launchpad is # in the western hemisphere. earth.rotate(angle = pi, axis=vector(0, 0, 1)) # Earth has opposite momentum from moon so the total earth + moon # momentum is 0. earth.p = -moon.p earth.mass = 5.972*10**24 # kg
# Rocket properties vERot = radEarth * omegaEarth # tangential velocity due to earth's rotation rocket = sphere(pos=radEarth * vector(1, 0, 0), radius=radEarth, \ color=color.red, make_trail=False) rocket.mass = 4500 # kg # Momentum of the rocket on earth's surface rocket.p = rocket.mass * vERot * cross(vector(0, 0, 1), \ mag(rocket.pos - earth.pos)) # kg m/s # Draw an arrow in the direction of the total force on the rocket. Note: # the arrow is always the same length and does not scale with the force. fRvec = arrow(pos=rocket.pos, axis=5*radEarth*vector(-1, 0, 0), \ color=color.red) # Store the rocket position to compare with the updated position. This # will allow us to know if the rocket is moving away from the earth or # falling back to earth. r_prev = mag(rocket.pos) # Store the closest approach to the moon to help us fine-tune the launch # parameters. dRM_min = dEarthMoon - radEarth launched = False # Have we launched yet? landed = False # Have we reached the moon? falling = False # Are we falling back to earth? crashed = False # Have we crashed into earth? tooFar = False # Are we heading off into space never to return?
# Draw the launchpad so we can see where the rocket will launch. launchpad = box(pos=vector(5 * radEarth, 0, 0), axis = vector(0, 1, \ 0), size = vector(1.5 * radEarth, 1.5 * radEarth, \ 1.5 * radEarth), color=color.red)
################################################# # This section allows you to launch the rocket by clicking on the scene. # Prints a message. Note that the rocket will launch at launchAngle if # you do not click. def process(event): global launched # rocket is launched if not(launched): launched = True # New momentum is old momentum (moving on surface of earth) # plus launch momentum (away from center of earth) rocket.p = rocket.p + rocket.mass * vLaunch * \ hat(rocket.pos-earth.pos) rocket.angle = atan2(rocket.pos.y, rocket.pos.x) moon.angle = atan2(moon.pos.y, moon.pos.x) theta_RM = degrees(rocket.angle - moon.angle) print("Rocket is launched!") print("theta_RM = ", theta_RM, " deg") scene.bind('mousedown', process) #################################################
# Initialize the time t = 0 # s dt = 100 # time step in s
######################################################################## # Calculation loop # Loop when none of the end conditions has occurred. while (not crashed) and (not tooFar) and (not landed) and (t < tmax): rate(500) # This adjusts the timing of the animation. ############################################################ # Calculate gravitational forces on moon and earth dEM = moon.pos - earth.pos # Displacement vector from earth to moon # Calculate force of gravity on moon due to earth. fMoonEarth = G*moon.mass*earth.mass/mag(dEM)**2 * hat(dEM) # Force on earth due to moon is opposite. fEarthMoon = -fMoonEarth ############################################################ # Calculate Rocket position information dRM = moon.pos - rocket.pos # Displacement vector from rocket to moon # If dRM is closer than the previous closest approach, save it. if mag(dRM) < dRM_min: dRM_min = mag(dRM) # If dRM is closer than 10 moon radii from moon, print it out to help # fine-tune launch properties. if mag(dRM) < 10 * radMoon: print('dRM: ', mag(dRM)/radMoon, ' moon radii') dRE = earth.pos - rocket.pos # Displacement vector from rocket to earth # Calculate the force on the rocket using the law of gravitation # described earlier. ############################################################ # Calculate the force on the rocket using the law of gravitation # described earlier. fMoonRocket = vector(0, 0, 0) fEarthRocket = vector(0, 0, 0) fNetRocket= fEarthRocket + fMoonRocket ############################################################ # Update earth and moon momentum earth.p = earth.p + fMoonEarth * dt moon.p = moon.p + fEarthMoon * dt ############################################################ # Update rocket momentum if not(launched): # Calculate the momentum of the rocket as it is rotating on the earth rocket.p = rocket.mass * radEarth * omegaEarth * \ cross(vec(0,0,1), hat(rocket.pos - earth.pos)) else: # Edit this line of code to account for the force on the rocket due # to gravity rocket.p = (G*rocket.mass*earth.mass)/(r_prev) ############################################################ # Update earth and moon position earth.pos = earth.pos + earth.p/earth.mass * dt moon.pos = moon.pos + moon.p/moon.mass * dt ############################################################ ## Edit this line of code to update the rocket's position using the ## updated momentum rocket.pos = rocket.pos + rocket.p/rocket.mass * dt ############################################################ # Update various parameters # Check to see if rocket is falling to earth if (mag(rocket.pos) < r_prev): falling = True else: falling = False # Store current rocket distance for use in the next loop r_prev = mag(rocket.pos) # Adjust the force arrow for the new force and position fRvec.pos=rocket.pos fRvec.axis=5*hat(fNetRocket)*radEarth # Calculate the total energy (T + U) of the rocket to see if it will # escape earth (unless it hits tne moon) rocket.energy = mag(rocket.p)**2 / (2 * rocket.mass) - G * \ rocket.mass * earth.mass/mag(dRE) - G * rocket.mass * \ moon.mass/mag(dRM)
############################################################ # Rotate the earth and launchpad earth.rotate(angle = omegaEarth*dt, axis = vector(0,0,1), \ pos=earth.pos) launchpad.rotate(angle = omegaEarth*dt, axis = vector(0,0,1), \ origin=earth.pos) ############################################################ # Increment the time by dt t = t + dt ############################################################ # Test for stopping conditions # Have we landed on the moon? if (mag(dRM) <= radMoon): landed = True print("Reached the moon. Mission successful!") # Have we crashed into earth (within 2 earth radii of center)? if (launched) and (mag(rocket.pos) <= 2 * radEarth) and falling: print("Rocket crashed to earth!") crashed = True # Have we missed the moon and are escaping (never to return)? if (mag(rocket.pos) > 1.5 * dEarthMoon) and (rocket.energy >= 0): tooFar = True print("Rocket is lost in space!") ############################################################ # This section launches the rocket once it reaches launchAngle # If we have not launched, see if we have reached launchAngle if not launched: # Calculate rocket angle from starting angle rocket.angle = atan2(rocket.pos.y, rocket.pos.x) # Calculate moon angle from starting angle moon.angle = atan2(moon.pos.y, moon.pos.x) # Calculate angle between rocket and moon theta_RM = degrees(rocket.angle - moon.angle) # If we have reached launchAngle, launch the rocket if (abs(theta_RM - launchAngle) < degrees(omegaEarth * dt)): launched = True # New momentum is old momentum (moving on surface of earth) + # launch momentum (away from center of earth) rocket.p = rocket.p + rocket.mass * vLaunch * \ hat(rocket.pos-earth.pos) print("Rocket is launched!") print("theta_RM = ", theta_RM, " deg") ############################################################ # End of calculation loop ########################################################################
# Print useful information print("Closest approach to moon: ", dRM_min/radMoon, " moon radii")
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
