Skip to content

Instantly share code, notes, and snippets.

@ishiy1993
Created October 22, 2015 06:55
Show Gist options
  • Save ishiy1993/3d7f3bf65e3c29392522 to your computer and use it in GitHub Desktop.
Save ishiy1993/3d7f3bf65e3c29392522 to your computer and use it in GitHub Desktop.
Haskellでオイラー法
import Data.List (unfoldr)
-- dy/dx = f
f x y = - a * y
-- パラメータ
a = 1
-- 区間と初期値
x0 = 0
x1 = 5
y0 = 1
-- 分割数
n = 100
dx :: Double
dx = (x1 - x0) / n
euler :: (Double,Double) -> Maybe ((Double,Double),(Double,Double))
euler xy@(x,y) | x < x1 = Just (xy, xy')
| otherwise = Nothing
where xy' = (x',y')
x' = x + dx
y' = y + dx * (f x y)
main :: IO ()
main = writeFile "euler.dat" . toData $ unfoldr euler (x0,y0)
toData :: [(Double,Double)] -> String
toData xys = unlines $ map (\(x,y) -> (show x) ++ " " ++ (show y)) xys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment