Skip to content

Instantly share code, notes, and snippets.

@0not
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0not/09dce776d3b4d6158b6d to your computer and use it in GitHub Desktop.
Save 0not/09dce776d3b4d6158b6d to your computer and use it in GitHub Desktop.
Numerically and analytically solve the radioactive decay problem (dN/dt = -kN) and plot the results.
module Main where
import Graphics.EasyPlot
dt = 0.001 -- Timestep
tau = 0.05 -- Decay constant
n0 = 1000 -- Initial number of particles
main = do
plot X11 $ [Data2D [Title "Euler", Color Red, Style Lines] [] numerical,
Data2D [Title "Actual", Color Blue, Style Lines] [] analytic]
-- Analytic results
analytic :: [(Double, Double)]
analytic = [(t, n0 * (exp (-t/tau))) | t <- [0.0, dt .. tau*5]]
-- Numerical results
numerical :: [(Double, Double)]
numerical = [(t, num t) | t <- [0.0, dt..tau*5]]
-- Numerically solve dN/dt = -kN
num :: Double -> Double
num t
| t > 0 = let t' = num (t - dt) in t' - dt / tau * t'
| otherwise = n0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment