# RK4 wrt a mass and spring system

## Recommended Posts

Mantrid    188
hi all i've been studying RK4 the past few days (using the gaffer notes online as well as some random lecture slides i've found) now i've gotten the gyst of the basic maths of RK4, but i'm puzzled by this bit: k1 = f (tn, xn) k2 = f (tn + h⁄2, xn + h⁄2 k1) k3 = f (tn + h⁄2, xn + h⁄2 k2) k4 = f (tn + h, xn + h k3) what are the parameters i'm passing as 't' and 'x'??!?!!? i've got the forces etc, my hooke's law code is looking ok but not tested till i get this working basically what i'm needing output are the velocities of both particles connected to the spring (for hooke's law calculations) and the particles' new positions (for rendering) im using a Vector3D struct (containing floatx, y, z as well as some overloaded operators) to store my forces, mass, velocities, position etc. as the cloth will be in 3D space Cheers all

##### Share on other sites
Mantrid    188
ahh

if the original is

then i'm passing the OVERALL time, then x?

does this mean i have to do the RK4 process twqice, once for velocity and once for position (passing as 'x' each time) or is it ok to pass the velocity as 'x' then do a simple euler to get the position from that?

or am i totally on the wrong track?

##### Share on other sites
nmi    978
Take a look at this wiki:
http://en.wikipedia.org/wiki/Runge-Kutta_methods

So in your case you know the equation:
x' = f(t, x); x(t0) = x0

The parameter t is the current time, h is the time step (i.e. time difference from next frame time to current frame time) and x is the current value of your function.

The next value of your function (for time t+h) is then:
x_new = x + h*(k1 + 2*k2 + 2*k3 + k4)/6

##### Share on other sites
Mantrid    188
hm cheers man, i think im starting to understand better now

so, if position ix 'x', i firstly do dx/dt where x'=f(t, x), then to get velocity 'v' i do another RK4 for dv/dx passing v' = f(x, v)?

im wondering now, if that's right, then will two rk4s like this each timestep cause significant overhead

##### Share on other sites
nmi    978
Quote:
 Original post by Mantriddoes this mean i have to do the RK4 process twqice, once for velocity and once for position (passing as 'x' each time) or is it ok to pass the velocity as 'x' then do a simple euler to get the position from that?

Your value x should contain all state information at a given time t. This means x is a vector that represents location, velocity, acceleration, ... The values you will put into this vector depend on the problem.

The function f calculates the first derivative of x with resprect to time. So if your state vector x contains location and velocity (i.e. x = [loc, vel]), the derivate will contain velocity and acceleration (i.e. x' = [vel, 0], in case of no acceleration). Since also the time is passed to function f, those values may as well change with time.

##### Share on other sites
Mantrid    188
ohhhhhhhh i see!!!! i'll start having a look at it that way and see what i can come up with, many thanks!