Euler's Method

\(TextForm
    \`The\ following\ `EulerMethod'\ routine\ implements\ Euler' s\ method\ 
        for\ numerically\ solving\ the\ initial\ value\ 
        problem . 

\ \ \ \ \ 	\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 
          \(dy\/dx\)\  = \ f \((x, y)\), \ \ \ \ \ \ 
    y \((x\_0)\)\  = \ 
      y\_0 . 
Here\ is\ the\ Mathematica\ code\ for\ \ `EulerMethod'\ 
        \(\((largely\ borrowed\ from\ page\ 74\ of\ the\ Mathematica\ 
              supplement)\) : \)\)

\(\(EulerMethod[\ f_, {x0_, \ y0_}, \ h_, \ n_\ ]\  := \ 

		
      Module[
		\ \ \ {EMStep}, 
		\ \ \ 
        Interpolation[
\ \ \ 	\ \ \ \ \ \ \ \ \ 
          EMStep[{x_, y_}]\  := \ N[{x + h, y + h*f[x, y]}]; 

          		\ \ \ \ \ \ \ \ \ \ NestList[EMStep, {x0, y0}, n], 

          			\ \ \ \ \ \ \ InterpolationOrder\  -> \ 1
		\ \ \ ]
	\ ]
        \ \ \ \ \ \ \ \ \ \ \ \ \ \)\)

To use the routine first define a function f[x_,y_] of two variables
corresponding to the right side of the differential equation.  For
example,

f[x_, y_]  :=  y  -  x

Then type

EulerMethod[f, {x0, y0}, h, n]

where f is the function you defined, x0 and y0 are the initial data, h is the step size, and n is the number of steps taken.  The routine then outputs a piecewise linear function of one variable that approximates the solution of the differential equation above.  Notice that the domain of the function will extend from x0 to x0 + h × n, so you need to be careful about making sure you make n large enough so that the domain includes any points of interest.    For the function f defined above, let's see what kind of output EulerMethod gives us and compare it with the exact solution obtained by DSolve.  We'll try an initial value of y(0) = 1, a step size of .1, and 50 steps.  This will give us an approximate solution defined for 0 < x < 5.

approx  =  EulerMethod[f, {0, 2},  .1,  50]

DSolve[{y'[x]  ==  y[x]  -  x,   y[0]  ==  2}, y[x],  x]

exactsol =  y[x] /. First[%]

Plot[{approx[x],  exactsol},  {x, 0, 5}]

So the solutions diverge quite a bit after x = 2.  Let's see what effect a smaller step size has.

approx  =  EulerMethod[f, {0, 2},  .01,  500]

Plot[{approx[x],  exactsol},  {x, 0, 5}]

This looks quite a bit better, though still not perfect.  Let's try dropping the step size by another factor of 10.

approx  =  EulerMethod[f, {0, 2},  .001,  5000]

Plot[{approx[x],  exactsol},  {x, 0, 5}]

The difference is now nearly impossible to see!  It's important to realize that we've been depending on an exact solution in order to check the accuracy of our approximate solution.  When an exact solution isn't available, we can at least compare approximate
solutions (for different step sizes) with each other and see if shrinking the step size affects the approximation very much.  Let's try this idea out on a first order ODE with righthand side given by

f[x_, y_]  :=  x^2  +  y^2

approx1  =  EulerMethod[f, {0, 1},  .1, 10]

approx2  =  EulerMethod[f, {0, 1},   .01,  100]

Plot[{approx1[x], approx2[x]},  {x, 0,  .9}]

Clearly  at least one of these approximate solutions  isn't trustworthy beyond x=.4!  Let's try a third approximation with smaller step size.

approx3  =  EulerMethod[f, {0, 1},   .001,  900]

Plot[{approx1[x], approx2[x],  approx3[x]},  {x, 0,  .9}]

It looks like things are starting to settle down from the second to the third approximation.  Nevertheless, a further decrease in step size would likely yield a noticeable gain in accuracy...