🔒 Locked
Euler/Mid-point /4th order Runge-Kutta Integrators
Advertisement
Original Post
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.
They don't compute integrals of functions but solve ordinary differential equations. ODEs naturally arise in physics.
RK4 is the best of the 3.
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.
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.
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]
[Edited by - xdotdt on February 28, 2005 8:17:42 PM]
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.
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
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?
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?
1. your using acceleration in evaluate before its defined (put acceleration function above evaluate function).
2. Im not sure, but if I remember correctly
try that for 2 argumented function (I think this may be wrong?)
[Edited by - Genjix on March 8, 2005 8:56:27 AM]
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]
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
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
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?
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement