Runge-Kutta to integrate acceleration twice

velocityX1 = velocityX;
velocityX2 = velocityX + CorrectedAccel * 9.80665 *dt*0.5f;
velocityX3 = velocityX2 + CorrectedAccel * 9.80665 *dt*0.5f;
velocityX4 = velocityX3 + CorrectedAccel * 9.80665 * dt;
velocityX5 = (velocityX1+2.0f*velocityX2+2.0f*velocityX3+velocityX4)/6;
positionX1 = positionX;
positionX2 = positionX + velocityX5 * dt*0.5f;
positionX3 = positionX2 + velocityX5 * dt*0.5f;
positionX4 = positionX3 + velocityX5 * dt;
positionX5 = (positionX1+2.0f*positionX2+2.0f*positionX3+positionX4)/6;

This is part of my program whereby I use the RK4 method to inetgrate from acceleration to position.
The acceleration is a value output from an IMU
dt used is 0.01 or 1/100
Is this code that i wrote correct?
It looks okay to me except that you are mixing floats and doubles (9.80665 and then 0.5f). In numerical methods, you probably want to avoid using 32bit floating point numbers and stick to doubles.

It's been a LONG time since I've done Runge-Kutta so I can't really correct you on your implementation of the method.
Oh ok i would correct the numerical error then.Thx anyway
Any help here?
Why not feed the integrator an equation that you already know the exact solution for,
such as a particle moving in a uniform gravitational field with no air resistance (parabola) ?

Then if the integrator follows the exact curve closely you are probably OK
Well the problem in writing this program is that the acceleration input is from an IMU which would mean that it is discrete. And it is not time dependent,thus i am not able to work out an equation to put into the integrator. I tried the euler integrator but there are horrible errors
If it's not time dependant, then you can't integrate over time. It's just not mathematically possible. You need to know your constant (dX condition)

If your IMU is a simulation and not a real device, then it is probably sufficient to integrate it directly instead of using Runge-Kutta like so:
1
2
velocity[ii] += accelleration[ii] * dt;
position[ii] += velocity[ii] * dt;


If you'r integrating over position, then use dPosition, instead of dt.
Last edited on
Topic archived. No new replies allowed.