Haskellで拡散方程式を書いてみた。その一

拡散方程式をHaskellで書いてみた。

拡散方程式の陽解法にあるCのプログラムをHaskellに書き直した。

diffusion.hs

nmax :: Double
nmax = 20
n :: Integer
n = 20000

dt = 1.0 / 10000.0
dx = 1.0 / nmax
a = dt / dx / dx

-- initial data
u_init = [2.0  * (x/nmax) * (1.0 - (x/nmax)) | x <- [0 .. nmax]]

-- 1step
uu h= 0 : zipWith3 (\x y z -> a  * x + (1.0 - 2.0 * a) * y + a * z) (tail(tail h)) (tail h) h
		++ [0]

--iteration1
ach ([[]], y)= ([[]], y)
ach ([], y)= ([], y)
ach (x:xs,y) = if y <10000
	            then (x:fst (ach ((uu x):xs, y+1)), y)
	            else (x:xs, y)

printresult = last (fst (ach ([u_init],1)))

ghci
:l diffusion.hs
printrusultで結果が見える。

ループと再帰 | すぐに忘れる脳みそのためのメモ