From: Michael Orlitzky Date: Sun, 9 Dec 2012 08:22:25 +0000 (-0500) Subject: Add an implementation of Euler's method that takes the step size 'h' as an argument... X-Git-Url: http://gitweb.michael.orlitzky.com/?p=numerical-analysis.git;a=commitdiff_plain;h=df93519c1e307699477d3d37484f44041d27354f Add an implementation of Euler's method that takes the step size 'h' as an argument instead of the number of subintervals 'n'. --- diff --git a/src/ODE/IVP.hs b/src/ODE/IVP.hs index 381ff8a..5d715a6 100644 --- a/src/ODE/IVP.hs +++ b/src/ODE/IVP.hs @@ -78,3 +78,35 @@ eulers_method x0 y0 xN f n = go ((x0,x1):rest) y0 f = y1 : (go rest y1 f) where y1 = eulers_method1 x0 y0 f (x1 - x0) + + +-- | Perform as many iterations of Euler's method over the interval +-- [$x0$, $xN$] as is necessary for the given step size $h$. The +-- number of subintervals `n` will be calculated automatically. A +-- list of y-values will be returned. +-- +-- The implementation simply computes `n` from the length of the +-- interval and the given $h$, and then calls 'eulers_method'. +-- +-- Examples: +-- +-- >>> let x0 = 0.0 +-- >>> let xN = 1.0 +-- >>> let y0 = 1.0 +-- >>> let f x y = y +-- >>> let ys = eulers_method x0 xN y0 f 10 +-- >>> let ys' = eulers_methodH x0 xN y0 f 0.1 +-- >>> ys == ys' +-- True +-- +eulers_methodH :: (RealFrac a, RealFrac b) + => a -- ^ x0, the initial point + -> b -- ^ y0, the initial value at x0 + -> a -- ^ xN, the terminal point + -> (a -> b -> b) -- ^ The function f(x,y) + -> a -- ^ h, the step size. + -> [b] +eulers_methodH x0 y0 xN f h = + eulers_method x0 y0 xN f n + where + n = floor $ (xN - x0) / h