Rocket Trajectory


SUBMITTED BY: Guest

DATE: May 19, 2013, 1:27 a.m.

FORMAT: C++

SIZE: 2.1 kB

HITS: 2337

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. /*A program designed to plot the trajectory of a rocket on a mission to land on the moon*/
  5. int main(int argc, char *argv[])
  6. {
  7. const float pii=3.1415; /*This defines the value of pi*/
  8. const float u=0.0066; /*This defines the intial velocity of rocket and is equivalent to v0*/
  9. const float angle=((89.9*pii)/180.0); /*This defines the angle of projection*/
  10. const float xe=0, ye=0; /*These are the Earth's coords: (0, 0) - ie. where it's centre lies*/
  11. const float xm=0, ym=225.7; /*These are the Moon's coords: (0, 225.7)*/
  12. const float Mm=1.0, Me=83.3; /*These are the masses of the Moon & Earth Respectively*/
  13. const float Rm=1.0, Re=3.7; /*These are the radii of the Moon & Earth Respectively*/
  14. const float G=(9.81*pow(10, -7)); /*This is the Gravitational constant)*/
  15. int dt=10; /*This is the step size delta.t*/
  16. float xr=0, yr=3.7; /*These are the coords of the Rocket)*/
  17. float vx, vy; /*These are the current velocities in both x & y directions*/
  18. float ax=0, ay=0; /*These are the accelerations in both x & y directions*/
  19. float rrx, rry; /*These are the components of the vector from Earth to Rocket*/
  20. float rmx, rmy; /*These are the components of the vector from earth to Moon*/
  21. /*Do NOT confuse with Re & Rm which are radii*/
  22. vx = u*cos(angle);
  23. vy = u*sin(angle);
  24. int t=0;
  25. /*Using a while loop*/
  26. while(pow((pow(xm-xr,2)) + (pow(ym-yr,2)),1/2)>=Rm)
  27. /*When the distance to the centre of the moon < radius of the moon,*/
  28. /*then the rocket has landed and the calculation can be stopped*/
  29. {
  30. t=t+dt; /*Time increases by 10 each time*/
  31. rmx = xm - xe;
  32. rmy = ym - ye;
  33. rrx = xr - xe;
  34. rry = yr - ye;
  35. ax = (((-G*Me)*xr)/(pow(((rrx*rrx)+(rry*rry)),(3/2)))) - ((G*Mm*(xr-xm))/(pow((pow(((rrx-rmx)),2)+(pow(((rry-rmy)),2))),(3/2)))); /*Acceleration in x-direction*/
  36. ay = (((-G*Me)*yr)/(pow(((rrx*rrx)+(rry*rry)),(3/2)))) - ((G*Mm*(yr-ym))/(pow((pow(((rrx-rmx)),2)+(pow(((rry-rmy)),2))),(3/2)))); /*Acceleration in y-direction*/
  37. vx=vx+(dt*ax); /*Step 4 of E

comments powered by Disqus