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

Loops in C-Programming

  1. Feb 21, 2005 #1
    I'm trying to use the improved euler method to calculate the position vector of a rocket on a mission from the earth to the moon. The method involves calculating new values of acceleration whilst time is incremented and using this IN ADDITION to the previous values of acceleration to find the new velocities, and similarly using the new and old velocities to find yet ne position vectors etc. Being a novice at C-programming and a complete newcomer to the improved euler method, I was hoping that someone would be able to advise me how I actually incorporate that into a loop?! Surely, as time increments my old acceleration values will be replaced by new ones and thus how can I use both!

    You can see my previous formulae for acceleration in the following thread:

    & here is the image of the page from my project problem sheet, explaining the method I have to use. Thanks

    Attached Files:

  2. jcsd
  3. Feb 21, 2005 #2
    You do something like this:

    int a=0,b;

    for (i=1; i++; i<10);

    Every iteration of the loop b is set to 11 (or any value you'd like it to be), then a+=b is used which is equivalent to a=(a+b). This tells the program to take whatever value is stored in a add that to b then restore the value back into a.

    Hope this helped.
  4. Feb 21, 2005 #3
    Ok...I think I might understand that. So what does the notation 'a+=b' actually mean?
  5. Feb 21, 2005 #4


    User Avatar
    Science Advisor
    Homework Helper

    is the same as


  6. Feb 21, 2005 #5
    Ok, I'm probably being really thick asking this, but what does a=a+ mean then?! lol
  7. Feb 21, 2005 #6
    a+=b => a = a + b

    similarly for things like -=, /=, *=
  8. Feb 21, 2005 #7


    User Avatar
    Science Advisor
    Homework Helper

    BTW, I just noticed, this should really be

    Since the second statement is the test condition. Otherwise, the loop will run for a good long while longer than 9 times.
  9. Feb 21, 2005 #8
    Yeah, I understand the order of things in the loop. I'm still really confused though with how I can use both the new variables and old variables within the same loop to compute something. Surely by using "a=a+b" I'm defining a new value of a so then if I want to calculate say "c = a old + a new" surely with such a code I'll just get "c=a new + a new"?
  10. Feb 21, 2005 #9
    Yes, thanks.
  11. Feb 21, 2005 #10
    That's why you don't want to write it as c=ao+an... You want to write it as an=ao+c or as above you can dispense with the old new and simply use a+=c. Math functions are performed on the right side of the = then stored into the variable on the left. so if you have a=a+c then what the computer does is it takes the old value for 'a' adds that to 'c' then stores this new value back into a thus erasing the old a and creating a new a (except new variables are not really created only their values changed). The a+=c is simply a shorthand notation for a=a+c. If you want to retain all of your values from the first iteration to the last then you will need to a) make a whole bunch of variables to store each element (don't do this) or the better solution b) use an array. If you only care about the end result then use the a+=(some_variable_here) scheme.
  12. Feb 21, 2005 #11


    User Avatar
    Science Advisor
    Homework Helper

    Well, the first thing to keep in mind is that you have to be careful about the order you do things in when you're programming. If you do something like (pseudocode):
    Code (Text):

    while (thereisstillstufftodo()) {
    Then you'll have the old and new acceleration to futz with simultaneously. Since I'm guessing you're doing question 4.2 things are actually not that complicated.

    Regarding variables:
    Variables (more or less) are contained by the curly braces. This example isn't something you'd typically see, (people don't typically throw braces around for no apparent reason) but it should give you some idea:
    Code (Text):

    int global; /*global variables exist everywhere */
    int main () {
       int i; /* clearly i exists here */
       /* global exists here also */
          int j;
          /* i,j and global exist here */
        /* no more j */
    /* i does not exist here */
    Cheat section here:

    Really, all you should need to do is change xm and ym to be floats rather than constants, add constants for R and omega, and add something like the following lines in the interior loop:
  13. Feb 22, 2005 #12
    I'd already included the bit in your "cheat section" in my new formulae. The real problems lay with how I could use both the new and old accelerations in the same formula for the velocity
  14. Feb 22, 2005 #13


    User Avatar
    Science Advisor
    Homework Helper

    Unless you're doing something wierd, you shouldn't care at all about the old acceleration.
  15. Feb 22, 2005 #14
    Well I'm trying to use the improved Euler Method and it says I have to use both old values of x and y to calculate new velocity and likewise the new and old velocities to calculate the acceleration. ARGHHHHH I'm so confused (and I've just met another girl doing same project as me who is even more confused!) :bugeye:
  16. Feb 22, 2005 #15


    User Avatar
    Science Advisor
    Homework Helper

    Not all that familiar with the improved Euler method, but after a quick google, I still am not that sure you'd want to deal with the old acceleration, but rather, it's old and new velocities that you're interested in.

    The improved Euler method (more or less) apparently involves averaging the velocity at the beginning and end of the segment instead of using the velocity at just one of the endpoints. This is a little circular, since you might need to know where the rocket ends up (which is affected by the velocity ) in order to figure out the velocity at the end. Fortunately, I don't think you have to worry about that.

    [tex]v_{new} = v_{old} + a t[/tex]
    [tex]p_{new}=p_{old}+t \times \frac{v_{new}+v_{old}}{2}[/tex]

    This should look suspiciously like the trapezoid method for numerical integration if you're familiar with that.

    If you can, try to get a handle on how this works before you start trying to implement it in your program.

    In the program that might look something like this:

    Code (Text):

    for(t=0;t<1000;t+=time_increment) {
    Since the statements are executed in order, it doesn't change v_old_x until it's been used to update the position.
  17. Feb 23, 2005 #16
    Thanks, that makes more sense now
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Loops in C-Programming
  1. C++ for loop help (Replies: 6)

  2. While loop in C (Replies: 11)

  3. C++ program help (Replies: 2)