Project 2 - Rocket Simulation

(To start this project, make a new file on the compiler - you don’t need any additional libraries or starter code)

Now that you’ve implemented your first project, you can try a tougher activity that shows how you can implement the skills you’ve learned so far to code up a program to simulate a vertical rocket launch off of the surface of the Earth. We’ll first introduce some of the necessary equations that you’ll need to solve this problem as you’re not expected to know all that background information beforehand. If it seems like it’s something you’re not familiar with, that’s completely fine - this exercise is adapted from a project in an actual class here at Michigan! First, we’ll explain the physics of the problem: you’ll need to calculate acceleration from values we know and approximate the velocity and altitude as time progresses.


We begin with acceleration, a very important value in many engineering applications. An equation that may or may not be new to you (but one you’ll certainly become familiar with if you pursue physics or engineering further) is Newton’s Second Law:

F = ma

where F is the net force (sum of all the forces) applied on an object, m is that object’s mass, and a is the resulting acceleration of that object. We assume this rocket will be taking off from some celestial body (like Earth, for example, but we’ll see what happens if we don’t limit ourselves to just our planet), so we know that gravity will be present. No matter where we are on a planet, gravity will pull the rocket back down towards the planet so it shows up as a negative term when summing forces.

Accounting for gravity, how do we actually get into space? The rocket produces a force called thrust as it fires, which is directly dependent on two quantities, the mass flow rate of the rocket propellant (how much propellent the rocket is using per second) and the effective exhaust velocity of the rocket (how fast that propellant is being pushed out of the rocket). The mass flow rate, (pronounced “m dot”), if we assume it’s constant, can be simply calculated by dividing the mass of the propellant on board by the time of the burn. The effective exhaust velocity, cE, is determined by a whole range of factors such as propellant type, maximum combustion temperature, and nozzle shape, to name a few. There’s an entire class (AERO 335 here at Michigan) built around calculating this value so we’ll provide it as a given value, but we’ll see what happens when we change that value to model different engines. Using these values, we arrive at the critical equation you need to find acceleration:

T = ṁ × cE

From here, we assume that the only forces acting on the rocket are thrust and gravity; in reality, there’s quite a few more, especially atmospheric drag, but we’ll assume these forces are 0 for now. Finally, you can solve for acceleration a with the following equation, where mg is the force of gravity:

T - mg = ma

Under the assumption that the mass flow rate and effective exhaust velocity are constant values, thrust is constant. Gravity will decrease slightly as our altitude increases but we can also assume that it’s a constant. Mass, however, does change as time passes, which means we have to constantly recalculate our thrust values using the above equation. There’s no perfect way to continuously recalculate this value, so we use a method called numerical approximation to get a reasonably close answer. The method we’ll use here is to calculate the acceleration at the start time, then assume it will be approximately constant for a short interval of time, called a timestep. We choose how long this time step is; shorter timesteps are more accurate as we’re able to better approximate the acceleration, but will also make our program have to do more calculations which can make it take a very long time or even crash if the timestep is just too small. With this approximate acceleration, we can approximate velocity and position too, described in the next section. After we find these three values, we can update the propellant mass by subtracting how much fuel was spent during that time step and then move forward in time by one time step and repeat the process until we’ve reached the end of our burn time and are out of propellant.

We have to approximate velocity and position using our calculated acceleration value and chosen timestep. The change in velocity can be approximated as acceleration multiplied by the timestep since acceleration is assumed to be constant for the entire timestep. We know we start at rest until the rocket takes off, so the initial velocity is zero, but we must add the change in velocity caused by the acceleration to the current velocity value for each and every timestep. Similarly, altitude can be approximated as velocity multiplied by a timestep since velocity is assumed to be constant for each timestep. We can assume we take off from sea level where altitude is zero and have to add the change in position caused by the velocity for each timestep.

ΔVelocity ≈ Acceleration × Timestep

ΔAltitude ≈ Velocity × Timestep

These two equations summarize the point above, where the delta symbol, Δ, indicates “rate of change in” a quantity - you’ll see that symbol again in future math and physics classes. 

With what you’ve learned in this activity so far and the equations and concepts explained above, you’ll be able to complete this problem given some starting values. But first, some hints:

With those hints in mind, you should be ready to write your code. Remember, the final answer we’re looking for is the altitude at the end of the burn, which should be your final altitude value. Use cout to print this value to the output terminal. Use the following values for your code, unless otherwise specified:


Using the given values, answer these questions:


This code is pretty flexible since we’ve made some simplifying assumptions and we have quite a few input parameters to play with - feel free to continue trying out different values if you want! Hopefully this activity has given you an understanding of some of the factors that affect rocket launches and their altitudes when the rocket stops firing. Note that this isn’t its final position! The rocket will still have a positive velocity when the engine runs out of fuel, but gravity will still be trying to pull it back to earth, so acceleration is now negative. The rocket will actually end up further away than the value you’ve calculated. If you’re interested in finding this value, you can use a new while loop that checks to make sure velocity is still positive and then update your new velocity, position, and time within the loop (acceleration will be constant since we assume constant gravity and mass will be constant since the rocket isn’t firing anymore and thrust is zero). Feel free to try it out!

Provided below is the solution code for the project, as well as what the final output should look like. Feel free to check against the solution output to see if your solution is right, but try to resist looking at the solution code before putting a full effort to try it on your own (I know, it’s tempting).

Solution Code & Output: