Heya, could someone take a look at my code for me? I'm trying to do the second part of "Part 1" from the problem sheet:(adsbygoogle = window.adsbygoogle || []).push({});

http://www.hoofbeat.tv/Page4%20Scan.jpg [Broken] [apologies the image is small on the site - I don't have enough webspace to upload it any larger]

However, my code never seems to break, ie. the rocket never lands on the moon. So I tried using the conditions from the first part of section 1 and seeing if I could get the correct angle of 89.9degrees. This works if I consider angles in the range 89.8->90.0 incrementing in 0.1 and it prints out "the rocket has landed on the moon". However, as soon as I set the range any bigger the process just continues until it has covered the range of angles and thus the rocket supposedly never lands on the moon.

One again the demonstrator couldn't find out what I was doing wrong (he suggested I changed my floats to doubles, but it hasn't made any difference). If someone, could offer any hints/suggestions I'd be really greatful as I really need to get this section finished so I can move on to part 2 (which looks horrible as well).

Thanks once again. My code is below:

Code (Text):

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

/*CO51 ROCKET SCIENCE - Part 1 ii)*/

/*A program designed to plot the trajectory of a rocket on a mission to land on the moon*/

int main(int argc, char *argv[])

{

FILE *fout;

fout = fopen("rocket2.data", "w");

if (fout == NULL)

{

printf("Cannot open rocket2.data\n");

exit(-1);

}

//const double pi=3.1415; /*This defines the value of pi*/

const double u=0.0066; /*This defines the intial velocity of rocket and is equivalent to v*/

const double xe=0, ye=0; /*These are the Earth's coords: (0, 0) - ie. where it's centre lies*/

const double xm=0, ym=225.7; /*These are the Moon's coords: (0, 225.7)*/

const double Mm=1.0, Me=83.3; /*These are the masses of the Moon & Earth Respectively*/

const double Rm=1.0, Re=3.7; /*These are the radii of the Moon & Earth Respectively*/

const double G=9.63e-7; /*This is the gravitational field constant*/

int dt=10; /*This is the step size delta.t*/

double xr=0.0, yr=3.7; /*These are the coords of the Rocket)*/

double vx, vy; /*These are the current velocities in both x & y directions*/

double ax=0, ay=0; /*These are the accelerations in both x & y directions*/

double rrx, rry; /*These are the components of the vector from Earth to Rocket*/

double rmx, rmy; /*These are the components of the vector from Earth to Moon*/

/*Do NOT confuse with Re & Rm which are radii*/

double de=0.0, dm=0.0; /*These are abbreviations for calculations late on to improve layout*/

double theta=0.0, angle=0.0; /*'Theta' is in degrees; 'angle' is in radians*/

double landed=0;

int t=0;

/*Using a for loop*/

for (theta=89.8; theta<=90.0; theta=theta+0.1)

{

angle=((theta*M_PI)/180.0);

for (t=0;t<=1000000;t=t+dt) /*The rocket may not reach the moon, so we consider a*/

/*time period of 1000000 and see where it is at this point*/

{

vx = u*cos(angle);

vy = u*sin(angle);

rmx = xm - xe;

rmy = ym - ye;

rrx = xr - xe;

rry = yr - ye;

de = sqrt(rrx*rrx+rry*rry);

dm = sqrt((rrx-rmx)*(rrx-rmx) + (rry-rmy)*(rry-rmy));

ax=

(G*Me/(de*de))*(-rrx)/de +

(G*Mm/(dm*dm))*(rmx-rrx)/dm;

ay=

G*Me/(de*de)*(-rry)/de +

G*Mm/(dm*dm)*(rmy-rry)/dm;

vx=vx+(dt*ax); /*Step 4 of Euler's method*/

vy=vy+(dt*ay); /*Step 4 of Euler's method*/

xr=xr+(dt*vx); /*Step 5 of Euler's method*/

yr=yr+(dt*vy); /*Step 5 of Euler's method*/

//printf("X coord: %lf\n", xr);

//printf("Y coord: %lf\n", yr);

//printf("distance to moon: %lf\n", dm);

if(dm < Rm) {

landed=1; /*ie. the Rocket has landed on the moon*/

break;

}

}

printf("Angle in degrees: %f\n", theta);

printf("Distance to moon after max time: %f\n", dm);

if(landed==1) {

printf("The rocket has hit the moon\n");

printf("Angle Required: %f\n", angle);

//printf("Distance to moon after max time: %lf\n", dm);

break;

fprintf(fout, "%lf\t%lf\n", xr, yr);

}

}

system("PAUSE"); /*This code is specific to a Windows system and*/

return 0; /*prevents the window closing automatically*/

}

**Physics Forums | Science Articles, Homework Help, Discussion**

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# C-My code doesn't break?

Loading...

Similar Threads for code doesn't break |
---|

Safe removal of an external disk |

**Physics Forums | Science Articles, Homework Help, Discussion**