Comparson
of
Euler's Approximation and Exact Solutions to
The Initial Value Problem
x[ti] = xi
dx = f[x,t] dt

by
K. D.  Stroyan
University of Iowa

copyright 1997 by Academic Press, Inc. - All rights reserved.

Special Mathematica Function: Euler's Method by Iteration

eulerstep[f_, {t_,x_}, {t0_,x0_}, dt_] :=
Module[{x1},
		x1 = (f /. {t->t0,x->x0}) ;
		{N[t0 + dt],N[x0 + x1 dt]}]
		
eulerSoln[f_,{t_,x_},{initialX_,initialT_},finalT_,dt_] :=
	NestList[eulerstep[f,{t,x},#,dt]&,
			N[{initialT,initialX}],Round[finalT/dt]]

Notebook Overview

Euler's Approximation to the solution of a differential equation

dx/dt = f[x,t]

is simply the "microscope equation"

	x[t+dt] = x[t] + x'[t]·dt + ϵ · dt

dropping the error  ϵ ·dt, and using the function  f[x,t]  to compute  x'[t] = dx/dt.

We begin with the initial condition

x[0] = xi

then use the approximation

x[0+dt] = x[0] + x'[0] dt = x[0] + f[x[0],0] dt
telling us the new value  x[dt].  Similarly,

x[t+dt] = x[t] + x'[t] dt = x[t] + f[x[t],t] dt,  for  t = dt, 2dt, 3dt, ...

We studied this earlier in the text in several special cases.  See the NoteBook EulerApprox.

Euler's approximation is conceptually clear, use where you start and how you change to approximate the next value.  Symbolic methods of solution are often not clear in this respect, rather, you do some tricks of integration and algebra to come up with a family of solutions and then use the initial condition to select the actual solution that begins at the initial condition and changes according to the law of the diffeerential equation.

This NoteBook explores the convergence of Euler's approximation to the exact symbolic solution for a few cases where you can find the symbolic solution.

References to the Text

This NoteBook goes with Chapter 21 of the text.

Step-by-step

Comparison for growth law f[t_,x_] := x t

For the differential equation

(dx/dt) = t x

you should use separation of variables and verify that solutions are of the form

x[t] = C Exp[((t^2)/2)]

for an unknown constant  C.  Mathematica can solve this with the same unknown constant when you enter the next command.

DSolve[x'[t]==t x[t],x[t],t]

Exact Solution with first Initial Condition

Mathematica can also solve the initial value problem
	x[0] = 1	
	dx/dt = t x
<Enter> the next command:

Clear[t, x, f, soln, nsol]; f[t_, x_] := t x; xi = 1; ti = 0; tFinal = 3; soln=DSolve[{x[ti] == xi,x'[t]==f[t, x[t]]},x[t],t]solnPlot1 = 
      Plot[x[t] /. soln, {t, 0, tFinal}, 
        PlotLabel -> "Exact Solution"];

Check the Solution

Verify the solution with Mathematica's differentiation:

x[t] /. soln /. t -> 0D[(x[t] /. soln), t] == t (x[t] /. soln)

Euler's Approximation

Here is a sample solution with the Euler Approximation from the Initialization cell above.

dt = 0.1; Null; nsol = eulerSoln[f[t, xE], {t, xE}, {xi, ti}, tFinal - ti, dt]; 
    nsolPlot1 = 
      ListPlot[nsol, PlotLabel -> "Approximate Solution"]; Show[solnPlot1, nsolPlot1];

Solutions with New  Initial Conditions

xi = 2; soln=DSolve[{x[ti] == xi,x'[t]==f[t, x[t]]},x[t],t]solnPlot2 = 
      Plot[x[t] /. soln, {t, 0, tFinal}, 
        PlotLabel -> "Exact Solution"]; nsol = eulerSoln[f[t, xE], {t, xE}, {xi, ti}, tFinal - ti, dt]; nsolPlot2 = ListPlot[nsol, PlotLabel -> "Approximate solution"]; 
    Show[solnPlot1, nsolPlot1, solnPlot2, nsolPlot2];

Practice Exercises

Solve the initial value problems

x[0] = 1	&	x[0] = 2
dx/dt = t x		dx/dt = t x

again using the smaller step size  dt = 0.05  in Euler's approximation.

Combined Computation

The next cell combines the computations above comparing an exact solution with the Euler approximation to an IVP.

Clear[t, x, f, soln, nsol]; f[t_, x_] := (x/t); xi = 1; ti = 1; tFinal = 3; dt = 0.1; soln=DSolve[{x[ti] == xi,x'[t]==f[t, x[t]]},x[t],t]solnPlot1 = Plot[x[t] /. soln, {t, ti, tFinal}]; nsol = eulerSoln[f[t, xE], {t, xE}, {xi, ti}, tFinal - ti, dt]; nsolPlot1 = ListPlot[nsol, PlotLabel -> "Approximate solution"]; 
    Show[solnPlot1, nsolPlot1]; xi = 2; soln=DSolve[{x[ti] == xi,x'[t]==f[t, x[t]]},x[t],t]solnPlot2 = Plot[x[t] /. soln, {t, ti, tFinal}]; nsol = eulerSoln[f[t, xE], {t, xE}, {xi, ti}, tFinal - ti, dt]; nsolPlot2 = ListPlot[nsol, PlotLabel -> "Approximate solution"]; 
    Show[solnPlot1, nsolPlot1, solnPlot2, nsolPlot2];

Exercise 21.4.3

You need to find the symbolic solution to the problems

x[ti] = xi
	dx/dt = f[x,t]

for the functions  f[x,t] and initial conditions in the text.  Then use the following samples to compare your symbolic solution to Euler's approximation.

a

x[0] = 1
dx = x t dt

b

x[1] = 0
dx = 1/t dt

c

x[0] = 0
dx = 2 t dt

d

x[0] = 2
dx = x^2 dt

e

x[0] = 0
dx = 2x dt

f

x[0] = 0
dx =  Exp[-t-x] dt

HINT:
dx = Exp[-x] Exp[-t] dt
Exp[ x ] dx = Exp[-t] dt
Exp[x] = -Exp[-t] + c
x = Log[c - Exp[-t]]

g

x[0] = 0
dx = 3 x^(2/3) dt

HINT: Check out
x[t] = 0
x[t] = t^3