[Haskell-beginners] Runge-Kutta and vectors

mike h mike_k_houghton at yahoo.co.uk
Fri May 12 07:49:12 UTC 2017


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
etc

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

Mike









More information about the Beginners mailing list