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

  • Thread starter Hoofbeat
  • Start date
  • Tags
    Break Code
In summary: Formula for rocket movement*/ } /*End of for loop*/ printf("Rocket has landed on the moon at %g", landed); /*Close fout now that we're done with the loop*/ fclose(fout);
  • #1
Hoofbeat
48
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
  • #2
Have you considered that there are variables, say rrx and rry, other than t that should be reset on every shot?
 
  • #3
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!)
 
  • #4
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:
  • #5
Soz...I'm back again. I just reset the values for each iteration, but the problem still exists :(

Any other ideas?
 
  • #6
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?
 
  • #7
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 :tongue: :rofl:
 

1. Why does my code keep breaking?

There can be several reasons why your code is breaking. It could be due to an error in the code itself, compatibility issues with different browsers or systems, or conflicts with other code or plugins. It's important to carefully check your code for any errors and troubleshoot any potential conflicts.

2. How can I prevent my code from breaking?

To prevent your code from breaking, it's important to follow best coding practices and regularly test your code in different environments. You can also use a version control system to track changes and revert back to a previous working version if needed.

3. What is the best way to debug my code when it breaks?

The best way to debug your code is by using a debugger tool or console. This will help you identify any errors or issues in your code and allow you to step through your code line by line to pinpoint the problem.

4. Can I use automated testing to ensure my code doesn't break?

Yes, you can use automated testing to check for any errors or bugs in your code. This can save time and help catch any issues before they become bigger problems. However, it's still important to manually test your code in different environments to ensure compatibility.

5. How can I fix my code if it breaks?

If your code breaks, the first step is to identify the cause of the issue. Once you have identified the problem, you can make the necessary changes to fix it. It's also important to test your code again after making changes to ensure it is working properly.

Similar threads

  • Computing and Technology
Replies
9
Views
5K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top