Created
October 22, 2015 06:55
-
-
Save ishiy1993/3d7f3bf65e3c29392522 to your computer and use it in GitHub Desktop.
Haskellでオイラー法
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 (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