1
我在Haskell中實現了Runge-Kutta。但是,如果在位置爲(0,0,-10)且速度爲(0,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