Runge-Kutta method: Difference between revisions

→‎{{header|Haskell}}: Minor tidying of existing draft - removed redundant import, applied hlint, hindent.
(→‎{{header|Haskell}}: Minor tidying of existing draft - removed redundant import, applied hlint, hindent.)
Line 825:
Using GHC 7.4.1.
 
<lang haskell>import Data.Listdv
:: Floating a
=> a -> a -> a
dv = (. sqrt) . (*)
 
dvfy ::t Floating= a1 =>/ 16 * (4 + at ->^ a2) ->^ a2
dv = (. sqrt). (*)
 
rk4
fy t = 1/16 * (4+t^2)^2
:: (Enum a, Fractional a)
rk4 :: (Enum a, Fractional a)=> (a -> a -> a) -> a -> a -> a -> [(a, a)]
rk4 fd y0 a h = zip ts $ scanl (flip fc) y0 ts where
where
ts = [a,h ..]
fc t y =
fc t y = sum . (y :) . zipWith (*) [1 / 6, 1 / 3, 1 / 3, 1 / 6] $
scanl
$ scanl (\k f -> h * fd (t + f * h) (y + f * k)) (h * fd t y) [1/2,1/2,1]
(h * fd t y)
[1 / 2, 1 / 2, 1]
 
task =
rk4 :: (Enum a, Fractional a)=> (a -> a -> a) -> a -> a -> a -> [(a,a)]
mapM_
rk4 fd y0 a h = zip ts $ scanl (flip fc) y0 ts where
$ map (print . (\(x, y) -> (truncate x, y, fy x - y)) )
ts = [a,h ..]
$ (filter (\(x, _) -> 0 == mod (truncate $ 10 * x) 10) $
fc t y = sum. (y:). zipWith (*) [1/6,1/3,1/3,1/6]
$ take 101 $ rk4 dv 1.0 0 0.1)</lang>
$ scanl (\k f -> h * fd (t+f*h) (y+f*k)) (h * fd t y) [1/2,1/2,1]
 
task = mapM_ print
$ map (\(x,y)-> (truncate x,y,fy x - y))
$ filter (\(x,_) -> 0== mod (truncate $ 10*x) 10)
$ take 101 $ rk4 dv 1.0 0 0.1</lang>
 
Example executed in GHCi:
9,655

edits