   runge kutta
  i want to use runge kutta for my integrator but am having problems. can someone confirm im doing this right below?also am i right in thinking that adaptive runge kutta is inappropriate cos you only get a fixed time step per update and so you cannot change your h value really.# -*- coding: utf-8 -*-def rungeKutta(y0, h, df):    k1 = df(0, y0)    k2 = df(0.5*h, y0 + 0.5*h*k1)    k3 = df(0.5*h, y0 + 0.5*h*k2)    k4 = df(h,y0 + h*k3)    return y0 + h*(k1 + 2*k2 + 2*k3 + k4)/6# runge kuttaacc = 2vel = 0pos = 0for t in xrange(10):    h = 1.0    df = lambda h, pos: vel*h    pos = rungeKutta(pos, h, df)    df = lambda h, vel: acc*h    vel = rungeKutta(vel, h, df)    print pos, vel# euleracc = 2vel = 0pos = 0for t in xrange(10):    oldvel = vel    for h in xrange(10):   pos += oldvel*0.1   vel += acc*0.1    print pos, veloutput:0.0 1.00.5 2.01.5 3.03.0 4.05.0 5.07.5 6.010.5 7.014.0 8.018.0 9.022.5 10.00.0 2.02.0 4.06.0 6.012.0 8.020.0 10.030.0 12.042.0 14.056.0 16.072.0 18.090.0 20.0as you see the rk4 and euler value differ a bit muchthanks for your time ;)   
  I'm no python guru, but from "df = lambda h, pos: vel*h" you seem to only pass it the updated pos value, and not the updated velocity as well. So from what I can see, it will always use the same velocity value, instead of the intermediate one.Also, you multiply by h both in df and in the rungeKutta function. Don't do it in df.So, df needs both position and velocity as input, and it should return the derivatives of these two. You'll need to modify your rungeKutta function accordingly.;  ok thanks for your answer.ill remove the *h from the derivative functions... didnt know that.about taking the derivative of the position and new velocity... how can I give it the new velocity when I don't know it yet? do you mean something like:A = accelerationV = velocityP = offset positionh = time stepfunc RK4(h, xi, fdXdt)    k1 = fdXdt(0  , xi)    k2 = fdXdt(h/2, xi + h*k1/2)    k3 = fdXdt(h/2, xi + h*k2/2)    k4 = fdXdt(h  , xi + h*k3)    return xi + h*(k1 + 2*k2 + 2*k3 + k4)/6for loop:    h = ...    fdVdt = func(t): return A// dont multiply by t, right?    Vi+1 = RK4(h, Vi, fdVdt)    fdPdt = func(): return Vi+1// dont multiply by t, right?    Pi+1 = RK4(h, Pi, fdPdt)i wrote it in pseudo code ;)so here we calculate the velocity before the offset[Edited by - sdndsjnsdkjsdn on September 28, 2009 2:05:09 AM];  What exactly are you using the RK4 algorithm for? It may turn out you're better off with another, simpler algorithm like f.inst. the Velocity verlet.   ;  RK4 has been popular for a long time as a general-purpose initial value problem solver, but I think people have started moving away from it for physical simulation and towards symplectic integrators, since RK4 introduces numerical damping.Both (velocity- and position-) Verlet and semi-implicit Euler are symplectic.  This is a technical property, but it has some nice consequences: Symplectic integrators conserve momentum, and always have bounded energy error (since they exactly conserve a slightly "wrong" energy function).A slightly different but ultimately equivalent way to look at these is as variational integrators, which are really easy to derive.  (This is the way I understand them.)   ;  Hi all,thanks for your answers. I have a dynamic body with acceleration and angular acc applied. so i might haveacc A over 0.1sacc B over 0.5s...angular acc X over 0.1sangular acc Y over 0.1sWe then minus the timestep from each of these and create a sum for the total acceleration this update. if the remaining time for a force is less than the step then we just do acc * (remaining time/time step).So I basically want to adjust my objects position/rotation based on these forces. I thought RK4 was the best algorithm to use.If someone can tell me whats the best algorithm to use or point me to an implementation then that would be great :)emergent: i saw verlet mentioned quite a bit... should i use this?   ;  RK4 assumes that calculating the derivative is easy, so if it's not because you're actually integrating the position and velocity of multiple bodies in a complex system (imagine a densely connected mesh of springs, for instance), it's considerably slower without much gain.The short answer for best integrator is verlet or velocity verlet.  The long answer was already provided by Emergent. :)  For the case of ballistic motion verlet gives you 100% accurate results anyway, so there's no need to get more involved.Even in the case of a mesh spring system, if you amoratize for the cost of computing the forces of springs during some given step size of h, doing verlet 3 times is better than doing RK4 once and still having to evaluate forces at 3 different times.   ;  thanks really for your help. ill use verlet and i made a new post about using verlet now.   