/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
using namespace std;
// This function calculates the altitude when the rocket runs out of
// propellant using these input parameters in order: initial mass;
// dry mass; burn time; effective exhaust velocity; and gravity
double rocket_launch_calculator(double m_i, double m_dry, double t_b,
double cE, double g) {
// Calculating propellant mass (m_p), m_dot, and thrust
double m_p = m_i - m_dry; // kg
double m_dot = -m_p / t_b; // kg/sec
double thrust = -cE*m_dot; // N
// Initializing our timestep value and all our loop variables
// We'll update all the current_something variables with each
// loop iteration, but they need to start somewhere
double timestep = 0.1; // sec, arbitrary but preferably under 0.1
double current_mass = m_i;
double current_acceleration = 0;
double current_velocity = 0;
double current_altitude = 0;
// This loop does most of the heavy lifting, it creates a new
// variable, time_since_launch, increments it by one timestep
// for each iteration of the loop, and stops once it reaches
// the value of the full burn time
for (double time_since_launch = 0; time_since_launch < t_b;
time_since_launch += timestep) {
current_acceleration = (thrust/current_mass) - g;
current_velocity += current_acceleration*timestep;
current_altitude += current_velocity*timestep;
current_mass += m_dot*timestep;
}
// Here we return the last altitude value but divide by 1000
// to convert it to kilometers each time we use the function
return current_altitude/1000;
}
int main()
{
// Initial given values:
double m_i = 100000; // kg
double m_dry = 10000; // kg
double t_b = 100; // sec
double cE = 4000; // m/sec
double g = 9.81; // m/(sec^2)
// For Part 1, we simply use the given values
double final_altitude =
rocket_launch_calculator(m_i, m_dry, t_b, cE, g);
cout << "Part 1" << endl;
cout << "The final altitude for Part 1 is " << final_altitude <<
" km. " << "We reached space!" << endl << endl;
// Here we store the final altitude using the given values in a new
// variable to use later for Part 5
double final_altitude_given = final_altitude;
// For Part 2, we change the burn time to 140 sec
t_b = 140; // sec
final_altitude =
rocket_launch_calculator(m_i, m_dry, t_b, cE, g);
cout << "Part 2" << endl;
cout << "The final altitude for Part 2 is " << final_altitude <<
" km. " << "The final altitude increased!" <<
endl << endl;
// For Part 3, we change the burn time back to 100 sec and
// change the effective exhaust velocity to 4500 m/sec
t_b = 100; // sec
cE = 4500; // m/sec
final_altitude =
rocket_launch_calculator(m_i, m_dry, t_b, cE, g);
cout << "Part 3" << endl;
cout << "The final altitude for Part 3 is " << final_altitude <<
" km. " << "The final altitude increased!" <<
endl << endl;
// For Part 4, we change the effective exhaust velocity back to
// 4000 m/sec and change the dry mass to 15000 kg
cE = 4000; // m/sec
m_dry = 15000; // kg
final_altitude =
rocket_launch_calculator(m_i, m_dry, t_b, cE, g);
cout << "Part 4" << endl;
cout << "The final altitude for Part 4 is " << final_altitude <<
" km. " << "The final altitude decreased..." <<
endl << endl;
// For Part 5, keep the dry mass at 15000 kg and change the
// initial mass to be 125000 kg
m_i = 125000; // kg
final_altitude =
rocket_launch_calculator(m_i, m_dry, t_b, cE, g);
// This calculates the ratio between the final altitude we just
// calculated against the final altitude using the given values
// and multiplies it by 100 to get the percentage
double altitude_ratio = 100*(final_altitude/final_altitude_given);
cout << "Part 5" << endl;
cout << "The final altitude for Part 5 is " << final_altitude <<
" km." << endl << "The new final altitude is " << altitude_ratio
<< "% of the final altitude using the given values, " <<
"which is NOT within 5% of this value." << endl << endl;
// For Part 6, we go back to the original initial and dry mass
// values of 100000 kg and 10000 kg, respectively, and we change
// the gravity value depending on each planet we're on
m_i = 100000; // kg
m_dry = 10000; // kg
double g_moon = 1.62; // m/(sec^2)
double g_mars = 3.711; // m/(sec^2)
double g_jupiter = 24.79; // m/(sec^2)
double g_titan = 1.352; // m/(sec^2)
double final_altitude_moon = rocket_launch_calculator(m_i,
m_dry, t_b, cE, g_moon);
double final_altitude_mars = rocket_launch_calculator(m_i,
m_dry, t_b, cE, g_mars);
double final_altitude_jupiter = rocket_launch_calculator(m_i,
m_dry, t_b, cE, g_jupiter);
double final_altitude_titan = rocket_launch_calculator(m_i,
m_dry, t_b, cE, g_titan);
cout << "Part 6" << endl;
cout << "The final altitude launching from the Moon is " <<
final_altitude_moon << " km." << endl;
cout << "The final altitude launching from Mars is " <<
final_altitude_mars << " km." << endl;
cout << "The final altitude launching from Jupiter is " <<
final_altitude_jupiter << " km." << endl;
cout << "The final altitude launching from Titan is " <<
final_altitude_titan << " km." << endl;
}