[C++] Using Runge-Kutta order 4 to solve system of ODEs

Click For Summary

Discussion Overview

The discussion revolves around the implementation of the Runge-Kutta order 4 (RK4) method to solve a system of ordinary differential equations (ODEs) related to an object's acceleration under gravity and air resistance. Participants are exploring the method's error characteristics, specifically aiming to experimentally verify that the RK4 method exhibits an error of order 4 (O(h4)).

Discussion Character

  • Exploratory
  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant describes their approach to verifying the RK4 error order by comparing results from different step sizes (h = 0.1, h = 0.05, h = 0.001) and expects a specific relationship between the errors.
  • Another participant suggests that the issue may stem from using doubles with integer values in calculations, which could affect precision and results.
  • A later reply proposes adding print statements to check intermediate results within the loop to ensure the algorithm is functioning as expected.
  • Participants discuss the importance of using appropriate precision for output, noting that default precision may obscure small differences in results.
  • There is a suggestion to save results from the smallest step size and compute differences to better analyze the error reduction.
  • Some participants express skepticism about whether changing precision will resolve the underlying issue of error reduction not aligning with expected RK4 behavior.

Areas of Agreement / Disagreement

Participants generally agree on the need to check for precision and the correctness of the implementation. However, there is no consensus on the root cause of the observed error behavior, and multiple viewpoints regarding potential issues remain unresolved.

Contextual Notes

Participants note potential limitations related to the handling of floating-point precision and the structure of the numerical integration loop. There are also unresolved questions about the correctness of the mathematical formulation of the ODE system.

Who May Find This Useful

Readers interested in numerical methods for solving ordinary differential equations, particularly those using the Runge-Kutta method, may find this discussion relevant. It may also benefit those exploring error analysis in numerical computations.

libelec
Messages
173
Reaction score
0
(DISCLOSURE: I have already posted this problem in http://math.stackexchange.com/questions/256393/calculate-runge-kutta-order-4s-order-of-error-experimentally, but found no satisfactory answer)

The problem is this. I need to experimentally check that RK4 method has an error of order 4 (O(h4)). To do this, I have calculated the value the method returns in a certain point for the step values h = 0.1 and h = 0.05 and compared those results to the one I got for the step value h = 0.001. I should check, then, that the difference between h0.001 and h0.1 must be 16 times larger than the difference between h0.001 and h0.05 (since I reduced the step by half, the error must be reduced 24 = 16 times).

I have the following ODE that describes an object's acceleration when falling from high altitude and being subjected to air friction:

2y/∂t2 = -9.8 + β e-y/α |v|2

Because so far I have only learned to deal with these second-order equations analytically by turning them into a system of ODEs, I propose the following variable change:

- ∂y/∂t = v(t) (derivative of position with respect to time equals speed)
- ∂v/∂t = ∂2y/∂t2 (derivative of speed with respect to time equals acceleration, or the second derivative of position with respect to time).

Therefore I get the system:

- ∂v/∂t = -9.8 + β e-y/α |v|2 = f(v, y, t)
- ∂y/∂t = v = g(v, t)

Runge-Kutta 4 for this system is expressed then as:

vn+1 = vn + 1/6(k1v + 2k2v + 2k3v + k4v)
yn+1 = yn + 1/6(k1y + 2k2y + 2k3y + k4y)

Where f(v, y, t) = -9.8 + β e-y/α |v|2 and g(v, t) = v

Now, for Runge-Kutta 4, I need to calculate 4 constants that will give me an idea of the function increment at the i-th point. This I have to do for each equation, so I calculate:

- For v:

* k1v = h f(v, y, t)
* k2v = h f(v + 0.5 k1v, y + 0.5 k1y, t + 0.5 h)
* k3v = h f(v + 0.5 k2v, y + 0.5 k2y, t + 0.5 h)
* k4v = h f(v + k3v, y + k3y, t + h)

- For y:

* k1y = h g(v, t)
* k2y = h g(v + 0.5 k1v, t + 0.5 h)
* k3y = h g(v + 0.5 k2v, t + 0.5 h)
* k4y = h g(v + k3v, t + h)

In this cases, I find that the increase by 0.5 h or h in t does nothing, since t doesn't appear in neither function.

I have written the following program to solve this. The problem I have is that, no matter the step sizes I take, I always find that the error is reduced by half when I reduce the step size by half.

Code:
#include <math.h>
#include <cmath>
#include <sstream>
#include <fstream>
#include <vector>
#include <cstdlib>

using namespace std;

long double rungeKutta(long double h)
{
	long double alpha = 6629;
	long double beta = 0.0047;

	long double pos = 39068;
	long double speed = 0;

	for (int i = 1; h*i < 120; i++)
	{
		long double k1v = h * (-9.8 + beta * exp(-pos/alpha) * pow(speed, 2));
        long double k1y = h * speed;
        long double k2v = h * (-9.8 + beta * exp(-(pos + 0.5*k1y)/alpha) * pow(speed + 0.5*k1v, 2));
        long double k2y = h * (speed + 0.5*k1v);
        long double k3v = h * (-9.8 + beta * exp(-(pos + 0.5*k2y)/alpha) * pow(speed + 0.5*k2v, 2));
        long double k3y = h * (speed + 0.5*k2v);
        long double k4v = h * (-9.8 + beta * exp(-(pos + k3y)/alpha) * pow(speed  + k3v, 2));
        long double k4y = h * (speed + k3v);

        speed = speed + (k1v + 2.0*(k2v + k3v) + k4v)/6;
        pos = pos + (k1y + 2.0*(k2y + k3y) + k4y)/6;
	}

	return pos;
}

int main()
{

	long double errorOne = rungeKutta(0.01);
	long double errorTwo = rungeKutta(0.005);
	long double real = rungeKutta(0.0001);

	cout << fabs(real-errorOne) << endl << fabs(real - errorTwo) << endl;
	system("pause");
	return 0;
}

Any ideas why this happens?

I have the following suspects:

  • I have built the system wrong, and that's why no matter what algorithm I use I always get linear reduction
  • There's something wrong with the way I calculate the constants

Thanks
 
Technology news on Phys.org
You have a lot of doubles listed where you assign them with integer values.

You have some calculations where you're dividing double factors with integer values which may be the actual source of the problem.

Lastly, your for loop uses a terminating comparison that compares a long double to an integer. Its always better to compare apples to apples that way you know its not the compiler.

Also I would put a print statement inside your loop to show intermediate results and to see if the for loop is actually executed that may help you determine where if any your problem lies.
 
I changed the code to reflect the tips you gave regarding the cross-type operations:
Code:
long double rungeKutta(long double h)
{
	long double alpha = 6629.0;
	long double beta = 0.0047;

	long double pos = 39068.0;
	long double speed = 0;

	for (double i = 1; h*i < 120.0; i++)
	{
		long double k1v = h * (-9.8 + beta * exp(-pos/alpha) * pow(speed, 2));
        long double k1y = h * speed;
        long double k2v = h * (-9.8 + beta * exp(-(pos + 0.5*k1y)/alpha) * pow(speed + 0.5*k1v, 2));
        long double k2y = h * (speed + 0.5*k1v);
        long double k3v = h * (-9.8 + beta * exp(-(pos + 0.5*k2y)/alpha) * pow(speed + 0.5*k2v, 2));
        long double k3y = h * (speed + 0.5*k2v);
        long double k4v = h * (-9.8 + beta * exp(-(pos + k3y)/alpha) * pow(speed  + k3v, 2));
        long double k4y = h * (speed + k3v);

        speed = speed + (k1v + 2.0*(k2v + k3v) + k4v)/6.0;
        pos = pos + (k1y + 2.0*(k2y + k3y) + k4y)/6.0;

	cout << "i = " << i << ", speed = " << speed << "pos = " << pos << endl;
	}

	return pos;
}

But I get the same results. I added the print statement to check if there is some mistake in the for, but found nothing.
 
Here's a discussion on long double usage that may be helpful to you.

http://www.cplusplus.com/forum/beginner/34088/

Basically, I think you need to use 'L' at the end of your long double constants.

What did the for loop print tell you?

Did the loop cycle as many times as you expected?

Did you see 'k', speed and pos values changing as expected?

I can't comment on your algorithm so I can't help there but checking intermediate results will give you a sense that it is working.
 
Here's your problem:
Code:
cout << "i = " << i << ", speed = " << speed << "pos = " << pos << endl;

You're using the default precision here. You aren't going to see any change until you get to a step size of 3 seconds or so, and with that, you'll only see a tiny difference in the velocity.

You either need to print with greater precision, or even better, save the results from your smallest step size, compute the difference between the final velocity for step size #i versus step size #0, and print those differences along with the values.
 
D H said:
Here's your problem:
Code:
cout << "i = " << i << ", speed = " << speed << "pos = " << pos << endl;

You're using the default precision here. You aren't going to see any change until you get to a step size of 3 seconds or so, and with that, you'll only see a tiny difference in the velocity.

You either need to print with greater precision, or even better, save the results from your smallest step size, compute the difference between the final velocity for step size #i versus step size #0, and print those differences along with the values.

I don't see what difference it makes. The relation is one is half of the other wheather I print 6 or 10 digits. I mean, if I give it larger precision, it may go from printing 0.06666 and 0.03333 to 0.066661234 and 0.033334564.

EDIT: I think I see what you may be missing: my second post only has the one function rungeKutta(); the whole program is in the first post.
 
Last edited:
I see now that you are printing the differences in the final position.

Did you do what jedishrfu suggested in post #4:
- Did you fix your use of double constants such as 0.001?
- Did you verify that the loop cycled as many times as you expected?

There is another problem with your scheme. Any numerical integration technique is subject to two main sources of error: Errors inherent in the technique itself, and errors that result from finite precision arithmetic (e.g., long double). Errors inherent in the technique dominate for large step sizes while errors that result from using finite precision arithmetic dominate for small step sizes. Decreasing the step size *increases* the error when the step size is so small that arithmetic is the dominant source of error.

How do you know that those step sizes of 0.0001, 0.05, and 0.01 seconds are all in the range where technique errors dominate? (Hint: You don't. You might want to rethink your use of step sizes.)
 
Last edited:
Hi libelec!

RK does not necessarily give an increase of precision of O(h4).
It depends on the differential equation.

To clarify, suppose your beta would be zero (no friction), then there would be no effect of step size at all, since each approximation would be perfect.
That is, RK would effectively be O(1).

To find O(h4), try a different differential equation.
For instance one where the acceleration depends linearly on the position (like a system with a spring in it).
 
This problem does yield O(h^4) behavior when h is the range where errors inherent in RK4 dominate over numerical errors. No problem will exhibit O(h^4) behavior for all time steps if you use finite precision arithmetic (e.g., IEEE floating point).
 
  • #10
D H said:
How do you know that those step sizes of 0.0001, 0.05, and 0.01 seconds are all in the range where technique errors dominate? (Hint: You don't. You might want to rethink your use of step sizes.)

An empirical way to do that is keep doubling your step size till the solution goes unstable, and then back off from there. The masochistic way is to do a theoretical stability analyiss for your differential equation!

Based on experience, I would expect the long double issues are a red herring. You shouldn't need to use long double arithmetic to show this effect. Doing everything in double should be good enough.
 
  • #11
I'm not sure what compiler you're using, but note that Microsoft's C / C++ compilers ignore long double and just treat it as double (both are 64 bits). The old 16 bit compilers, Visual C / C++ 2.X or older, were the last ones to support long doubles (80 bit floatling point).
 

Similar threads

  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
14
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 4 ·
Replies
4
Views
8K
  • · Replies 2 ·
Replies
2
Views
4K