Created
February 28, 2018 04:28
-
-
Save matsubara0507/eb36a6209efc7bd554ef53fd469532eb to your computer and use it in GitHub Desktop.
odeEuler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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