为什么我的Runge-Kutta实现振荡为0?

时间:2014-02-13 02:19:11

标签: math haskell integration physics

我在Haskell中实现了Runge-Kutta。但是,当在位置为(0,0,-10)且速度为(0,0,0)的物体上运行时,它会非常快速地来回振荡,直到 到达(0,0,0)的位置。

出了什么问题?

(注意:我的实施基于this resource*||*运算符分别将标量乘以矢量,将矢量乘以标量。

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

1 个答案:

答案 0 :(得分:0)

在这种情况下,acceleration函数正在为弹簧建模。

我已将其更改为:

acceleration :: (Vec3 Double, Vec3 Double) -> Second -> Vec3 Double
acceleration (sx, sv) t =  sv