{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE Safe #-}
module Physics.Learn.RungeKutta
( rungeKutta4
, integrateSystem
)
where
import Physics.Learn.StateSpace
( StateSpace(..)
, Diff
, Time
, (.+^)
)
import Data.VectorSpace
( (^+^)
, (*^)
, (^/)
)
rungeKutta4 :: StateSpace p => (p -> Diff p) -> Time p -> p -> p
rungeKutta4 :: forall p. StateSpace p => (p -> Diff p) -> Time p -> p -> p
rungeKutta4 p -> Diff p
f Scalar (Diff p)
dt p
y
= let k0 :: Diff p
k0 = Scalar (Diff p)
dt forall v. VectorSpace v => Scalar v -> v -> v
*^ p -> Diff p
f p
y
k1 :: Diff p
k1 = Scalar (Diff p)
dt forall v. VectorSpace v => Scalar v -> v -> v
*^ p -> Diff p
f (p
y forall p. StateSpace p => p -> Diff p -> p
.+^ Diff p
k0 forall v s.
(VectorSpace v, s ~ Scalar v, Fractional s) =>
v -> s -> v
^/ Scalar (Diff p)
2)
k2 :: Diff p
k2 = Scalar (Diff p)
dt forall v. VectorSpace v => Scalar v -> v -> v
*^ p -> Diff p
f (p
y forall p. StateSpace p => p -> Diff p -> p
.+^ Diff p
k1 forall v s.
(VectorSpace v, s ~ Scalar v, Fractional s) =>
v -> s -> v
^/ Scalar (Diff p)
2)
k3 :: Diff p
k3 = Scalar (Diff p)
dt forall v. VectorSpace v => Scalar v -> v -> v
*^ p -> Diff p
f (p
y forall p. StateSpace p => p -> Diff p -> p
.+^ Diff p
k2)
in p
y forall p. StateSpace p => p -> Diff p -> p
.+^ (Diff p
k0 forall v. AdditiveGroup v => v -> v -> v
^+^ Scalar (Diff p)
2 forall v. VectorSpace v => Scalar v -> v -> v
*^ Diff p
k1 forall v. AdditiveGroup v => v -> v -> v
^+^ Scalar (Diff p)
2 forall v. VectorSpace v => Scalar v -> v -> v
*^ Diff p
k2 forall v. AdditiveGroup v => v -> v -> v
^+^ Diff p
k3) forall v s.
(VectorSpace v, s ~ Scalar v, Fractional s) =>
v -> s -> v
^/ Scalar (Diff p)
6
integrateSystem :: StateSpace p => (p -> Diff p) -> Time p -> p -> [p]
integrateSystem :: forall p. StateSpace p => (p -> Diff p) -> Time p -> p -> [p]
integrateSystem p -> Diff p
systemDerivative Time p
dt
= forall a. (a -> a) -> a -> [a]
iterate (forall p. StateSpace p => (p -> Diff p) -> Time p -> p -> p
rungeKutta4 p -> Diff p
systemDerivative Time p
dt)