#include <stdio.h>
#include <math.h>
const int number_of_steps = 211;
float dt = 0.5;
float mass = 5900.0;
// The r and theta variable sets
float r[number_of_steps];
float r_vel[number_of_steps];
float r_acc[number_of_steps];
float theta[number_of_steps];
float theta_vel[number_of_steps];
float theta_acc[number_of_steps];
// Computations for Lift and Drag forces
float Lift[number_of_steps];
float Drag[number_of_steps];
float vel_twind[number_of_steps];
float sq_V[number_of_steps];
float dy_pressure[number_of_steps];
float density_rho[number_of_steps];
// The sets of angles, and time
float angle_of_attack[number_of_steps];
float flight_path_angle[number_of_steps];
float time[number_of_steps];
void Calc_Function(int i);
void Save_Result();
int main() {
// Set the initial conditions
flight_path_angle[0] = -0.10471975333;
r[0] = 122.0;
r_vel[0] = 11.0*sin(flight_path_angle[0]);
theta[0] = 0.0;
theta_vel[0] = 11.0*cos(flight_path_angle[0])/r[0];
time[0] = 0.0;
for(int i = 1; i < number_of_steps; i++) {
// Calculate the new using the old value
Calc_Function(i-1);
r_vel[i] = r_vel[i-1] + dt*r_acc[i-1];
r[i] = r[i-1] + dt*r_vel[i-1];
theta_vel[i] = theta_vel[i-1] + dt*theta_acc[i-1];
theta[i] = theta[i-1] + dt*theta_vel[i-1];
flight_path_angle[i] = atan2(r_vel[i-1], r[i-1]*theta_vel[i-1]);
time[i] = time[i-1] + dt;
}
Calc_Function(number_of_steps-1); // Last accleration update
Save_Result();
printf("Done~~~~~\n");
getchar();
}
void Calc_Function(int i) {
// The calculation of Lift and Drag forces
density_rho[i] = 1.752*pow(10.0,9.0)*exp((6378.0-r[i])/6700.0);
vel_twind[i] = r[i]*theta_vel[i] - 7.292*pow(10.0, -5.0)*r[i];
sq_V[i] = r_vel[i]*r_vel[i] + vel_twind[i]*vel_twind[i];
dy_pressure[i] = 0.5*density_rho[i]*sq_V[i];
angle_of_attack[i] = atan2(r_vel[i], vel_twind[i]);
Lift[i] = 0.4*(12.0*pow(10.0, -6.0))*dy_pressure[i];
Drag[i] = 1.2*(12.0*pow(10.0, -6.0))*dy_pressure[i];
// The calculation of accerlations
r_acc[i] = -3.986*pow(10.0, 5.0)/(r[i]*r[i]) - (Drag[i]/mass)*sin(angle_of_attack[i]) + (Lift[i]/mass)*cos(angle_of_attack[i]) + r[i]*theta_vel[i]*theta_vel[i];
theta_acc[i] = -(Drag[i]/mass)*cos(angle_of_attack[i])/r[i] - (Lift[i]/mass)*sin(angle_of_attack[i])/r[i] - 2*r_vel[i]*theta_vel[i]/r[i];
}
void Save_Result() {
FILE *pFile;
pFile = fopen("Results.txt", "w");
for (int i = 0; i < number_of_steps; i++) {
fprintf(pFile, "%f\t %f\t %f\t %f\t %f\t %f\t %f\n", time[i], r[i], r_vel[i], r_acc[i], theta[i], theta_vel[i], theta_acc[i]);
}
fclose(pFile);
}