Question

I have implemented Runge-Kutta in Haskell. However, when run on an object with a position of (0, 0, -10) and a velocity of (0, 0, 0), it very quickly oscillates back and forth until reaching a position of (0, 0, 0).

What is going wrong?

(Note: I am implementing based on this resource and the *| and |* operators multiply a scalar by a vector, and a vector by a scalar, respectively.

integrate :: PhysicalObject a => a -> Second -> Second -> (Position3, Velocity3)
integrate object t dt = (p, v)
  where p = (position object) + (dxdt |* dt)
        v = (velocity object) + (dvdt |* dt)
        dxdt = (1.0 / 6.0) *| (dx1 + (2 *| (dx2 + dx3)) + dx4)
        dvdt = (1.0 / 6.0) *| (dv1 + (2 *| (dv2 + dv3)) + dv4)
        (dx1, dv1) = (velocity object, acceleration (position object, velocity object) t)
        (dx2, dv2) = evaluate (position object, velocity object) t (0.5*dt) (dx1, dv1)
        (dx3, dv3) = evaluate (position object, velocity object) t (0.5*dt) (dx2, dv2)
        (dx4, dv4) = evaluate (position object, velocity object) t dt (dx3, dv3)

evaluate :: (Vec3 Double, Vec3 Double) -> Second -> Second -> (Vec3 Double, Vec3 Double) -> (Vec3 Double, Vec3 Double)
evaluate (ix, iv) t dt (dx, dv) = (odx, odv)
  where odx = sv
        odv = acceleration (sx, sv) (t + dt)
        sx  = ix + (dx |* dt)
        sv  = iv + (dv |* dt)

acceleration :: (Vec3 Double, Vec3 Double) -> Second -> Vec3 Double
acceleration (sx, sv) t =  (-k *| sx) - (b *| sv)
  where k = 10
        b = 1
Was it helpful?

Solution

The acceleration function is modeling a spring, in this case.

I have changed it to:

acceleration :: (Vec3 Double, Vec3 Double) -> Second -> Vec3 Double
acceleration (sx, sv) t =  sv
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top