Runge-Kutta and vectors

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
Report Content as Inappropriate

Runge-Kutta and vectors

Mike Houghton

Having looked at 'Learn Physics by Programming in Haskell’ I became quite wobbly with excitement! :)

So I took and implemented some of their ideas. My University level maths is around 40 years old and so I struggled
with some of the later things - but it's slowly coming back!

(I have a copy at https://github.com/banditpig/vectors/blob/master/LearnPhysics.pdf)

So I have

type Scalar = Double
type XYZ = (Scalar, Scalar, Scalar)
newtype Vector = V { xyz :: XYZ}

and set of vector  operations

(^+^) :: Vector -> Vector -> Vector
(^-^) :: Vector -> Vector -> Vector
(*^) :: Scalar -> Vector -> Vector
(^*) :: Vector -> Scalar -> Vector
(^/) :: Vector -> Scalar  -> Vector
(>.<) :: Vector -> Vector -> Scalar -- dot
(><) :: Vector -> Vector -> Vector  -- cross

I then have

type Time = Double
type Displacement = Vector
type Velocity = Vector

type State = (Time, Displacement, Velocity)

eulerStep :: (State -> Vector) -> Double -> State -> State
eulerStep f dt st@(t, r, v) = (t', r', v') where
    t' = t + dt            
    r' = r ^+^  v ^* dt      
    v' = v ^+^  f st ^* dt  

and so for a given acceleration function, time step and start state a solution  is given by

solution :: (State -> Vector) -> Double -> State -> [State]
solution a dt  = iterate (eulerStep a dt)

What I'm trying to do is replace the Euler method of solving with the Rung-Kutta method.
I'm really struggling in seeing how the Rung-Kutta examples I've seen are implemented using vectors.

How should I progress? Any advice would be welcome! (I really want to implement it using the types I have rather than using an external library )

Many Thanks


Beginners mailing list
[hidden email]