Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C-My code doesn't break?

  1. Feb 17, 2005 #1
    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 Scan.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 (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*/
    }

     
     
  2. jcsd
  3. Feb 17, 2005 #2

    NateTG

    User Avatar
    Science Advisor
    Homework Helper

    Have you considered that there are variables, say rrx and rry, other than t that should be reset on every shot?
     
  4. Feb 17, 2005 #3
    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!!!)
     
  5. Feb 17, 2005 #4

    NateTG

    User Avatar
    Science Advisor
    Homework Helper

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

    Code (Text):

    //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 (Text):

    .
    .
    .
    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: Feb 17, 2005
  6. Feb 17, 2005 #5
    Soz...I'm back again. I just reset the values for each iteration, but the problem still exists :(

    Any other ideas?
     
  7. Feb 17, 2005 #6

    NateTG

    User Avatar
    Science Advisor
    Homework Helper

    Can you try setting
    xr=0
    yr=3.7
    instead of rrx and rry?
     
  8. Feb 17, 2005 #7
    YAY! That seems to work fine. Thanks ever so much :tongue: :rofl:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C-My code doesn't break?
  1. My C++ program's flaw (Replies: 19)

  2. C code problem (Replies: 19)

Loading...