What could be causing my rocket to never land on the moon?

  • Thread starter Thread starter Hoofbeat
  • Start date Start date
  • Tags Tags
    Break Code
AI Thread Summary
The code provided is intended to simulate the trajectory of a rocket landing on the moon, but it encounters issues where the rocket never lands. The user initially finds success with a narrow angle range of 89.8 to 90.0 degrees, but broader ranges lead to the rocket not landing. A key problem identified is that certain variables, such as the rocket's coordinates, need to be reset for each angle iteration. After resetting the rocket's position variables at the start of each angle iteration, the code successfully simulates the landing. The solution highlights the importance of correctly initializing variables within loops to ensure accurate simulations in programming.
Hoofbeat
Messages
48
Reaction score
0
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:

http://www.hoofbeat.tv/Page4%20Scan.jpg [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:
#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*/
}
 
Last edited by a moderator:
Computer science news on Phys.org
Have you considered that there are variables, say rrx and rry, other than t that should be reset on every shot?
 
NateTG said:
Have you considered that there are variables, say rrx and rry, other than t that should be reset on every shot?

Ummm interesting comment. I assumed that for each angle iteration the whole process would start again and all values would automatically start at their initial assigned values. Is that not the case then and how do I go about resetting them? (you can tell I don't know how to program!)
 
Hoofbeat said:
Ummm interesting comment. I assumed that for each angle iteration the whole process would start again and all values would automatically start at their initial assigned values. Is that not the case then and how do I go about resetting them? (you can tell I don't know how to program!)

Well, only the stuff inside the braces gets re-executed:

Code:
//Stuff out here is not repeated
int i=0;
int j=0;
printf ("This is going to be printed once.\n");
//Stuff inside the loop gets repeated
for(i=0;i<5;i++) {
   printf ("This gets printed five times.\n");
   for(j=0;j<4;j++) {
      printf("This gets printed 20 times\n");
   }
}

You can just set them in the exterior loop - something like:

Code:
.
.
.
for(angle=0;angle<2*M_pi;angle=angle+.001) {
   vx=u*cos(angle);
   vy=u*sin(angle);
   rrx=0;
   rry=0
   for(t=0;t<100000;t+=10) {
     .
     .
     .
   }
}
 
Last edited:
Soz...I'm back again. I just reset the values for each iteration, but the problem still exists :(

Any other ideas?
 
Hoofbeat said:
Soz...I'm back again. I just reset the values for each iteration, but the problem still exists :(

Any other ideas?

Can you try setting
xr=0
yr=3.7
instead of rrx and rry?
 
NateTG said:
Can you try setting
xr=0
yr=3.7
instead of rrx and rry?

YAY! That seems to work fine. Thanks ever so much :-p :smile:
 
Back
Top