Sign in to follow this  
Mantrid

RK4 wrt a mass and spring system

Recommended Posts

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
Quote:
Original post by Mantrid
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?


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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this