Results same w/ Euler, Improved Euler, & Runge-Kutta

I've been trying to do some simulations using the Euler, improved Euler, and Runge-Kutta methods. My results for the improved Euler and Runge-Kutta are very close to my results with the plain old Euler method. If there is any improvement, it is negligible. This lack of improvement surprises me. Is it a common occurrence? Is it likely that I have a round off error somewhere?

Answers and Replies

Integral
Staff Emeritus
Science Advisor
Gold Member
You should be able to take larger time steps with the RK and maintain accuracy. A lot depends on the DE you are solving. If the solution is smooth and slowly changing then it does not matter much what method you choose. If there are rapid changes (say a step function involved in the boundary or initial conditions) then you will see a difference.

The standard Euler method loses even a simple pendulum pretty quickly, the improved Euler and RK does much better.

I'm doing a mass spring. The equation for acceleration

a = -kx -bv

seems to keep things in bounds. I have, however, been having some phase problems.

arildno
Science Advisor
Homework Helper
Gold Member
Dearly Missed
I'm doing a mass spring. The equation for acceleration

a = -kx -bv

seems to keep things in bounds. I have, however, been having some phase problems.

This is not surprising. In pendulum problems, it is generally true that phase inaccuracies (period mismatching) will occur earlier than amplitude inaccuracies.

Are there other numerical methods, perhaps more obscure ones, that specifically address the issue of phase?

Integral
Staff Emeritus
Science Advisor
Gold Member
Go with RK4

RK4 seems to keeps things in phase a little better than the Euler. In contrast, at higher frequencies the magnitude of the oscillations seem to be better represented by the Euler.

Integral
Staff Emeritus
Science Advisor
Gold Member
Keep a VERY close eye on the Euler methods, I have seen it produce absolutly beautiful garbage. The forward step version is signifanlty more stable then the standard Euler.

I had a fairly good improvement using the leap-frog method instead of the Euler. In contrast, I used the Verlet method and everthing seemed to be much worse than the Euler. I can't say if I'm doing the Verlet correctly, or if one should expect the Verlet method to be way off for a damped oscillator.

Keep a VERY close eye on the Euler methods, I have seen it produce absolutly beautiful garbage. The forward step version is significantly more stable then the standard Euler.

Point taken. I'm using an "adaptive" leap-frog scheme, but I might be able to improve my frequency of oscillation. Perhaps I will try some sort of adaptive RK4.

Integral
Staff Emeritus
Science Advisor
Gold Member
Just a couple of aside comments.

If you take steps that are a power of 2 you will not be introducing round off errors. If you are not aware of it .1 (decimal) when translated to binary becomes the infinitely repeating fraction .00011001100.... So no mater how many digits you can carry, you MUST round off .1.

To me this is a very good reason to AVOID taking a step size which is a power of 10.

That's an interesting point. But what if my output data needs to have a step size that is a factor of 10? Should I use a factor of 2 that is smaller, and then interpolate?

jim mcnamara
Mentor
Integral has it exactly.

Floating point representations of numbers have issues. For example: in C, the float, double, and long double data types are FP's with increasing precision. As you extend precision you move the fuzz "further out", and it may sometimes defer the onset of your kind of issue to a later iteration. It does not get rid of it.

If you're using C, consider long double data types and see if it buys you anything in terms of the timing of the onset of phase issues. It may not.
I don't know what your goal is.

You could also interpolate the final results and use a factor of 2. Or find a BCD library () with the precision you need for your model.

Integral
Staff Emeritus
Science Advisor
Gold Member
If you feel you must take a power of 10 step use an integer counter to increment your steps then multiply by the step size. Do NOT add your power of 10 step to increment your process.

I recall that on my old 8 bit AppleII that the code

5 x= 0
10 x = x + .1

yielded .9999999 after 10 interations.

but
5 x = 0 : n =0 : st= .1
10 for n = 1 to 10
15 x = n*st
20 next n

gave correct results

The first method accumulates the round off error, while the second does not.

I'm not sure what you are doing in your code. The first one looks like a numerical method, but the second looks like you are preparing a vector, x, for a plot.

A step size of 1 through my numbers way off. I tried setting the step size to
h = 1.0/65536.0;
and then multiplying my output array by 66536.0 and the real step size as follows
for(i=0; i<length; i++)
{

out_put = out_put*65536.0*step_size;

}

I've now done a 4th order Runge-Kutta and a Euler-Cromer-Leap-Frog. The Euler-Cromer-Leap-Frog seems to win out on every test. I've long help the belief that Runge-Kuttas were better than Euleroid methods. My tests have shown this not to be the case. Am I missing something? Is there a such thing as some sort of Rung-Kutta-Leap-Frog?