Skip to content

Instantly share code, notes, and snippets.

@lovasoa
Last active December 7, 2016 17:13
Show Gist options
  • Save lovasoa/1728f8d07f84694553ece52b5d3f9c76 to your computer and use it in GitHub Desktop.
Save lovasoa/1728f8d07f84694553ece52b5d3f9c76 to your computer and use it in GitHub Desktop.
Delay differential equations in Haskell

ddeint.hs

Delay differential equations in Haskell

How to use

Download

Download ddeint.hs and a haskell compiler (in ubuntu: sudo apt install ghc)

Specify your ODE

In ddeint.hs, replace the following functions:

  • dydt : a function that takes an array of the previous computed values and t, and returns the derivative of your function. For instance, if dy/dt = f(y(t-m)), write dydt y t = f(y(t-m))
  • initialY : an list of historic values for y. For instance, initialY = [0,1,2] corresponds to
    • y(t0) = 0
    • y(t0-dt) = 1
    • y(t0 - 2dt) = 2

Compile

ghc -O2 ddeint.hs

run

./ddeint
import Data.List
dt = pi/100
t0 = 0
tmax = 50
dydt y t = y (t - 3*pi/2)
initialY = map sin [0, (-dt) .. (-2*pi) ]
--- Integrator
newval (oldYs, oldT) =
let
y t = oldYs !! round ((oldT - t)/dt)
newY = (head oldYs) + dt * (dydt y oldT)
in
Just (newY, (newY:oldYs, oldT + dt))
integrated = unfoldr newval (initialY, t0)
--- Display results
main = sequence $ take (round ((tmax-t0)/dt)) $ map print integrated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment