Skip to main content
gamedev.net
🔒 Locked

Euler/Mid-point /4th order Runge-Kutta Integrators

Started by bargasteh 20 replies 7.8k views
Original Post
bargasteh
bargasteh
Hello, every body. What exactly Integrators do, what are it's responsibilities and, which one of the above integrators are better ?! I dont have a clear picture of integrators, please explain it in easy way! Thank you. Looking forward to learn.
OpenGl + C++
Trap
Trap
wikipedia on numeric ode integrators

They don't compute integrals of functions but solve ordinary differential equations. ODEs naturally arise in physics.

RK4 is the best of the 3.
bargasteh
bargasteh
Any more thought?

OpenGl + C++
Eelco
Eelco
Quote:
Original post by Trap
RK4 is the best of the 3.

is it? id say it depends.
Squirm
Squirm
An object is undergoing constant acceleration a. (this is a spaceship in a game, falling under gravity).

The integral of a over time gives velocity. The integral of velocity over time gives position, which is what you actually need to draw the graphics on the screen.

What you would do at first is : V = V + A * T; P = P + V * T;
which is a simple approximation that everyone can grasp. That IS euler integration.

When you have everything working and decide that motion is a bit unstable and things sometimes shoot off for no reason, then you read up on numerical integrators and choose whichever you feel is most appropriate. RK4 is popular because it is a nice balance of speed and accuracy. It is most certainly not the "best" in every situation.
bargasteh
bargasteh
Thank you for your simple explanation, but still I dont undestand something now:
For example I am working on a simple spring, I use very simple spring formula and it works fine. where should I use Euler now ? I really dont get exactly where I should use it ?!

OpenGl + C++
Trap
Trap
Show your formula, most probably you are already using euler.

If you want to see euler failing put in a very big spring constant and start the simulation with the spring compressed a bit. If you are using euler it will explode. With RK4 it will explode too, but at bigger constants.
xdotdt
xdotdt
I will explain the situation needs to solve ODE, and Euler method which is one of the easiest way. For example, suppose that you hit a ball. The ball has some spatial and time variables, i.e., position, velocity, acceleration, and so on. (To make the problem simple, I won't mention angular ones.) We want to know sequential ball positions. As you already know, v(velocity) is time derivative of x, that is, v = dx/dt, by newtonian dynamics. a(acceleration) is time derivative of v, that is, a = dv/dt. We can rewrite above like v = x', a = v'. Integrating these, we have x = ¡òv dt, v = ¡òa dt. However for almost situtation, it is impossible to integrate those by algebraic way. Thus, we'll use numerical methods. Numerical method approximates the result at the instant after from current values. Euler method comes from cutting Taylor Series from the third entry to infinity. Writing x' = v = f(t), we rewrite x with Taylor Series, x(t) = x(t0) + x'(t0)(t-t0) + ¨öx''(t0)(t-t0)©÷ + ... = x(t0) + f(t0)(t-t0) + ... Cutting entries from the third one, we have x(t) ¡Ö x(t0) + f(t0)(t-t0). Let t0 be the current time. Suppose that t is the time at which we want to know where the ball is, that is, t is t0 + ¥Ät. Given above, we can rewrite x(t0 + ¥Ät) = x(t0) + f(t0)¥Ät. Replacing t0 with t, we have x(t + ¥Ät) = x(t) + f(t)¥Ät. When we use this equation, we say, Euler method is used. You can replace x with v to get velocity at that time. I mean, v(t + ¥Ät) = v(t) + g(t)¥Ät, where g is g(t) = a = v'.

[Edited by - xdotdt on February 28, 2005 8:17:42 PM]
kju:b
bargasteh
bargasteh
Thank you so much everybody. I understand it now.
OpenGl + C++
teamonkey
teamonkey
Quote:
Original post by Eelco
Quote:
Original post by Trap
RK4 is the best of the 3.

is it? id say it depends.


It does, but 3 times out of 4 it's the best solution, anecdotally speaking. It has the best balance of speed, stability and accuracy for general complex simulations. If you need more accuracy or stability than RK4, you're usually better off finding some way of reducing your time step (e.g. by doing two steps of half the size). RK4 is a good tool for practically any simulation, even if it isn't the best.

Mid-point (I assume you mean RK2 or something similar) is good when speed is more of an issue, but one RK4 step is usually better than two RK2 steps of half the size (depends what you're doing).

Euler's right out - RK2 is more accurate for a smallish speed overhead and Verlet integration is much more stable for virtually no overhead.
[teamonkey] [blog] [tinyminions]
bargasteh
bargasteh
Hello, I have a question about fourth-order Runge-Kutta (RK4)

source code is taken from here: http://www.gaffer.org:8080/articles/Integration.html

struct State    {         float x;          // position         float v;          // velocity    };struct Derivative    {         float dx;          // derivative of position: velocity         float dv;          // derivative of velocity: acceleration    };Derivative evaluate(const State &initial, float t, float dt, const Derivative &d)    {         State state;         state.x = initial.x + d.dx*dt;         state.v = initial.v + d.dv*dt;         Derivative output;         output.dx = state.v;         output.dv = acceleration(state, t+dt);    }float acceleration(const State &state, float t)    {         const float k = 10;         const float b = 1;         return -k * state.x - b*state.v;    }void integrate(State &state, float t, float dt)    {         Derivative a = evaluate(state, t);         Derivative b = evaluate(state, t, dt*0.5f, a);         Derivative c = evaluate(state, t, dt*0.5f, b);         Derivative d = evaluate(state, t, dt, c);         const float dxdt = 1.0f/6.0f * (a.dx + 2.0f*(b.dx + c.dx) + d.dx);         const float dvdt = 1.0f/6.0f * (a.dv + 2.0f*(b.dv + c.dv) + d.dv);         state.x = state.x + dxdt * dt;         state.v = state.v + dvdt * dt;    }


when I compile the above code, it gives me a error saying :
'evaluate' : function does not take 2 parameters
that is correct because evaluate function takes 4 parameter not 2, what is wrong with that ?! anybody know?
OpenGl + C++
Genjix
Genjix
1. your using acceleration in evaluate before its defined (put acceleration function above evaluate function).
2. Im not sure, but if I remember correctly
Derivative evaluate(const State &initial, float t){         Derivative output;         output.dx = initial.v;         output.dv = acceleration(initial, t);}

try that for 2 argumented function (I think this may be wrong?)

[Edited by - Genjix on March 8, 2005 8:56:27 AM]
bargasteh
bargasteh
Thanks but It didnt work :(
What is missing ?!
OpenGl + C++
Extrarius
Extrarius
You're calling evaluate with only 2 parameters in the line "Derivative a = evaluate(state, t);"
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
bargasteh
bargasteh
Yes, I know, but if you go to the original website, where I took the code, you will see its like that, and because I am not sure exactly how RK4 works, therfore I dont know what is wrong.
please helppp
OpenGl + C++
Extrarius
Extrarius
For the first one, dt should be '0' and the state would be dx = state.v and dv = current acceleration.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
bargasteh
bargasteh
I changed it to:

Derivative x;
x.dx = state.v;
x.dv = acceleration(state, t+dt);

Derivative a = evaluate(state, t, 0.0, x);
Derivative b = evaluate(state, t, dt*0.5f, a);
Derivative c = evaluate(state, t, dt*0.5f, b);
Derivative d = evaluate(state, t, dt, c);

but still it doesnt work, what is wrong?
Is it what i was suppose to do ?

Thanks
OpenGl + C++
Genjix
Genjix
Quote:
Original post by Genjix
1. your using acceleration in evaluate before its defined (put acceleration function above evaluate function).
2. Im not sure, but if I remember correctly
*** Source Snippet Removed ***
try that for 2 argumented function (I think this may be wrong?)

i made a mistake in post above, check that again (add this function as well, along with other functions). Is this correct, anyone?
bargasteh
bargasteh
Then what is the correct answer, I dont think its difficult for experts here, please can you leave a feedback for me ?
Thank you
OpenGl + C++

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.