Skip to content

Instantly share code, notes, and snippets.

@matsubara0507
Created February 28, 2018 04:28
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 matsubara0507/eb36a6209efc7bd554ef53fd469532eb to your computer and use it in GitHub Desktop.
Save matsubara0507/eb36a6209efc7bd554ef53fd469532eb to your computer and use it in GitHub Desktop.
odeEuler
import Data.List (mapAccumL)
showTupleWith :: (Show a, Show b) => String -> (a, b) -> String
showTupleWith sep (a, b) = show a ++ sep ++ show b
dxList :: (Enum a, Floating a) => a -> a -> [a]
dxList dx xmax = [0.0, dx..xmax]
odeEuler :: (Enum a, Floating a) => (a -> a) -> a -> a -> a -> [(a, a)]
odeEuler f yinit dx xmax = snd $ mapAccumL calc yinit $ zip xs (tail xs)
where
xs = dxList dx xmax
calc y (x, x') = (y + (x' - x) * f y, (x, y))
main = do
putStrLn $ unlines $ map (showTupleWith " ") $ odeEuler id 1.0 0.01 10.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment