# Loops in C-Programming

1. Feb 21, 2005

### Hoofbeat

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:

• ###### Page3 Scan.jpg
File size:
19.1 KB
Views:
52
2. Feb 21, 2005

### faust9

You do something like this:

int a=0,b;

for (i=1; i++; i<10);
{
b=10+1;
a+=b;
}

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.

3. Feb 21, 2005

### Hoofbeat

Ok...I think I might understand that. So what does the notation 'a+=b' actually mean?

4. Feb 21, 2005

### NateTG

a+=
is the same as
a=a+

similarly
a-=
is
a=a-

also
a*=
a/=
and
a%=

5. Feb 21, 2005

### Hoofbeat

Ok, I'm probably being really thick asking this, but what does a=a+ mean then?! lol

6. Feb 21, 2005

### imabug

a+=b => a = a + b

similarly for things like -=, /=, *=

7. Feb 21, 2005

### NateTG

BTW, I just noticed, this should really be
for(i=1;i<10;i++)

Since the second statement is the test condition. Otherwise, the loop will run for a good long while longer than 9 times.

8. Feb 21, 2005

### Hoofbeat

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"?

9. Feb 21, 2005

### faust9

Yes, thanks.

10. Feb 21, 2005

### faust9

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.

11. Feb 21, 2005

### NateTG

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()) {
a_new=calculatenewacceleration();
furtzwithnewandoldacceleration();
a_old=a_new;
}

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:
xm=R*cos(omega*t);
ym=R*sin(omega*t);

12. Feb 22, 2005

### Hoofbeat

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

13. Feb 22, 2005

### NateTG

Unless you're doing something wierd, you shouldn't care at all about the old acceleration.

14. Feb 22, 2005

### Hoofbeat

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!)

15. Feb 22, 2005

### NateTG

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.

$$v_{new} = v_{old} + a t$$
$$p_{new}=p_{old}+t \times \frac{v_{new}+v_{old}}{2}$$

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) {
.
.
.
v_new_x=v_old_x+a_x*time_increment;
p_x=p_x+time_increment*(v_new_x+v_old)/2;
.
.
.
v_old_x=v_new_x;
}

Since the statements are executed in order, it doesn't change v_old_x until it's been used to update the position.

16. Feb 23, 2005

### Hoofbeat

Thanks, that makes more sense now