7.2 Euler's Method

Mathematica script by Chris Parrish,
cparrish@sewanee.edu

Problems from Hughes-Hallett, Gleason, et al,
"Calculus," Wiley, 1994

Euler

Euler - Contains Mathematica Code Only - No Solved Exercises

Exercises

Slope Field for dy/dx = Sin[x] Sin[y]

Hughes-Hallett, Gleason, et al, Exercise 9.3.1, page 493

In[348]:=

Here is a large-scale view of the slope field.

In[349]:=

Clear[f,x,y,a,b,c,d];

f[x_,y_] := Sin[x] Sin[y];

a = 0; b = 4;    (* a <= x <= b *)
c = 0; d = 4;    (* c <= y <= d *)

pts = {{0,2},{0,Pi}};

field = PlotVectorField[{1,f[x,y]},
{x,a,b},{y,c,d},
PlotLabel -> "dy/dx = Sin[x] Sin[y]",
Axes -> True,
AxesLabel -> {"x","y"},
PlotPoints -> 20,
Prolog -> ManganeseBlue,
Epilog -> {Red,PointSize[0.02],
Map[Point,pts]}];

... and here is a closer view for dealing with the problem at hand.

In[355]:=

a = 0;   b = 0.3;    (* a <= x <= b *)
c = 2.0; d = 2.1;    (* c <= y <= d *)

pts = {{0,2}};

field = PlotVectorField[{1,f[x,y]},
{x,a,b},{y,c,d},
PlotLabel -> "dy/dx = Sin[x] Sin[y]",
Axes -> True,
AxesLabel -> {"x","y"},
PlotPoints -> 10,
AspectRatio->1.0,
Prolog -> ManganeseBlue,
Epilog -> {Red,PointSize[0.02],
Map[Point,pts]}];n = 3;

In[359]:=

alpha = 2;
n = 3;

eulerTable = euler[f,a,b,n,alpha];
TableForm[eulerTable,
TableSpacing -> {0,3}]

Out[362]//TableForm=

 ti wi Euler 0. 2. 0.1 2. 0.2 2.00908 0.3 2.02707

In[363]:=

approx = ListPlot[eulerTable,
PlotStyle -> {PointSize[.02],OrchidDark},
DisplayFunction -> Identity];

Show[{field,approx},
PlotLabel -> "Euler Approximations",
AxesLabel -> {"t",None},
DisplayFunction -> \$DisplayFunction];

Now let's take the initial point to be (0,π).

In[365]:=

a = 0;   b = 0.3;    (* a <= x <= b *)
c = 3.1; d = 3.2;    (* c <= y <= d *)

pts = {{0,Pi}};

field = PlotVectorField[{1,f[x,y]},
{x,a,b},{y,c,d},
PlotLabel -> "dy/dx = Sin[x] Sin[y]",
Axes -> True,
AxesLabel -> {"x","y"},
PlotPoints -> 10,
AspectRatio->1.0,
Prolog -> ManganeseBlue,
Epilog -> {Red,PointSize[0.02],
Map[Point,pts]}];n = 3;

In[369]:=

alpha = Pi;
n = 3;

eulerTable = euler[f,a,b,n,alpha];
TableForm[eulerTable,
TableSpacing -> {0,3}]

Out[372]//TableForm=

 ti wi Euler 0. 3.14159 0.1 3.14159 0.2 3.14159 0.3 3.14159

In[373]:=

approx = ListPlot[eulerTable,
PlotStyle -> {PointSize[.02],OrchidDark},
DisplayFunction -> Identity];

Show[{field,approx},
PlotLabel -> "Euler Approximations",
AxesLabel -> {"t",None},
DisplayFunction -> \$DisplayFunction];

Slope Field for dy/dt = 1/t

Hughes-Hallett, Gleason, et al, Exercise 9.3.3, page 494

In[375]:=

Clear[f,t,y,a,b,c,d];

f[t_,y_] := 1/t;

a = 1; b = 2;    (* a <= t <= b *)
c = 0; d = 1;    (* c <= y <= d *)

pts = {{1,0}};

field = PlotVectorField[{1,f[t,y]},
{t,a,b},{y,c,d},
PlotLabel -> "dy/dt = 1/t",
Axes -> True,
AxesLabel -> {"t","y"},
PlotPoints -> 20,
Prolog -> ManganeseBlue,
Epilog -> {Red,PointSize[0.02],
Map[Point,pts]}];

In[381]:=

alpha = 0;
n = 10;

eulerTable = euler[f,a,b,n,alpha];
TableForm[eulerTable,
TableSpacing -> {0,3}]

Out[384]//TableForm=

 ti wi Euler 1. 0. 1.1 0.1 1.2 0.190909 1.3 0.274242 1.4 0.351166 1.5 0.422594 1.6 0.489261 1.7 0.551761 1.8 0.610584 1.9 0.66614 2. 0.718771

In[385]:=

approx = ListPlot[eulerTable,
PlotRange->{0,1},
PlotStyle -> {PointSize[.02],OrchidDark}];

In[386]:=

field = PlotVectorField[{1,f[t,y]},
{t,a,b},{y,c,d},
PlotLabel -> "Euler Approximations",
AxesLabel -> {"t","y"},
Axes -> True,
PlotPoints -> 20,
Prolog -> ManganeseBlue,
Epilog -> {PointSize[0.02],
OrchidDark,Map[Point,eulerTable],
Red,Map[Point,pts]}];

Slope Field for dy/dx = 2x

Hughes-Hallett, Gleason, et al, Exercise 9.3.5, page 494

In[387]:=

Clear[f,x,y,a,b,c,d];

f[x_,y_] := 2x;

a = 0; b = 1;    (* a <= x <= b *)
c = 1; d = 2;    (* c <= y <= d *)

pts = {{0,1}};

field = PlotVectorField[{1,f[x,y]},
{x,a,b},{y,c,d},
PlotLabel -> "dy/dx = 2x",
Axes -> True,
AxesLabel -> {"x","y"},
PlotPoints -> 20,
Prolog -> ManganeseBlue,
Epilog -> {Red,PointSize[0.02],
Map[Point,pts]}];

First, take n = 2.

In[393]:=

alpha = 1;
n = 2;

eulerTable = euler[f,a,b,n,alpha];
TableForm[eulerTable,
TableSpacing -> {0,3}]

Out[396]//TableForm=

 xi wi Euler 0. 1. 0.5 1. 1. 1.5

In[397]:=

approx = ListPlot[eulerTable,
PlotStyle -> {PointSize[.02],OrchidDark}];

In[398]:=

field = PlotVectorField[{1,f[x,y]},
{x,a,b},{y,c,d},
PlotLabel -> "Euler Approximations",
AxesLabel -> {"x","y"},
Axes -> True,
PlotPoints -> 20,
Prolog -> ManganeseBlue,
Epilog -> {PointSize[0.02],
OrchidDark,Map[Point,eulerTable],
Red,Map[Point,pts]}];

Now, take n = 4.

In[399]:=

alpha = 1;
n = 4;

eulerTable = euler[f,a,b,n,alpha];
TableForm[eulerTable,
TableSpacing -> {0,3}]

Out[402]//TableForm=

 xi wi Euler 0. 1. 0.25 1. 0.5 1.125 0.75 1.375 1. 1.75

In[403]:=

approx = ListPlot[eulerTable,
PlotStyle -> {PointSize[.02],OrchidDark}];

In[404]:=

field = PlotVectorField[{1,f[x,y]},
{x,a,b},{y,c,d},
PlotLabel -> "Euler Approximations",
AxesLabel -> {"x","y"},
Axes -> True,
PlotPoints -> 20,
Prolog -> ManganeseBlue,
Epilog -> {PointSize[0.02],
OrchidDark,Map[Point,eulerTable],
Red,Map[Point,pts]}];

Slope Field for dy/dx = -x/y

Hughes-Hallett, Gleason, et al, Exercise 9.3.5, page 494

In[405]:=

Clear[f,x,y,a,b,c,d];

f[x_,y_] := -x/y;

a = 0; b = 1.0;      (* a <= x <= b *)
c = 0.5; d = 1.5;    (* c <= y <= d *)

pts = {{0,1}};

field = PlotVectorField[{1,f[x,y]},
{x,a,b},{y,c,d},
PlotLabel -> "dy/dx = -x/y",
Axes -> True,
AxesLabel -> {"x","y"},
PlotPoints -> 20,
Prolog -> ManganeseBlue,
Epilog -> {Red,PointSize[0.02],
Map[Point,pts]}];

Take n = 10, so that deltaN = 0.1.

In[411]:=

Clear[alpha,n,eulerTable,approx,field];

alpha = 1;
n = 10;

eulerTable = euler[f,a,b,n,alpha];
TableForm[eulerTable,
TableSpacing -> {0,3}]

Out[415]//TableForm=

 xi wi Euler 0. 1. 0.1 1. 0.2 0.99 0.3 0.969798 0.4 0.938864 0.5 0.896259 0.6 0.840472 0.7 0.769083 0.8 0.678066 0.9 0.560083 1. 0.399392

In[416]:=

approx = ListPlot[eulerTable,
PlotStyle -> {PointSize[.02],OrchidDark}];

In[417]:=

field = PlotVectorField[{1,f[x,y]},
{x,a,b},{y,c,d},
PlotLabel -> "Euler Approximations",
AxesLabel -> {"x","y"},
Axes -> True,
PlotPoints -> 20,
Prolog -> ManganeseBlue,
Epilog -> {PointSize[0.02],
OrchidDark,Map[Point,eulerTable],
Red,Map[Point,pts]}];

Euler's Method for dbalance/dt = 0.05 balance

Hughes-Hallett, Gleason, et al, Exercise 9.3.9, page 495

Take deltaT = 1 and n = 1.

In[418]:=

Clear[f,t,balance,a,b,c,d,n];

f[t_,balance_] := 0.05 balance;

a = 0; b = 1;             (* a <= t <= b *)
n = 1;                    (* n = number of steps *)
initialBalance = 1000;    (* initialBalance = initial balance *)

eulerTable = euler[f,a,b,n,initialBalance];
TableForm[eulerTable,
TableSpacing -> {0,3}]

Out[424]//TableForm=

 t_i balance_i Euler 0. 1000. 1. 1050.

Take deltaT = 0.5 and n = 2.

In[425]:=

Clear[f,t,balance,a,b,c,d,n];

f[t_,balance_] := 0.05 balance;

a = 0; b = 1;             (* a <= t <= b *)
n = 2;                    (* n = number of steps *)
initialBalance = 1000;    (* initialBalance = initial balance *)

eulerTable = euler[f,a,b,n,initialBalance];
TableForm[eulerTable,
TableSpacing -> {0,3}]

Out[431]//TableForm=

 t_i balance_i Euler 0. 1000. 0.5 1025. 1. 1050.63

Slope Field for dy/dx = x + y

Hughes-Hallett, Gleason, et al, Exercise 9.3.5, page 494

In[432]:=

Clear[f,x,y,a,b,c,d];

f[x_,y_] := x + y;

a = -2; b = 3;    (* a <= x <= b *)
c = -1; d = 4;    (* c <= y <= d *)

pts = {{-2,1.5}};

field = PlotVectorField[{1,f[x,y]},
{x,a,b},{y,c,d},
PlotLabel -> "dy/dx = x + y",
Axes -> True,
AxesLabel -> {"x","y"},
PlotPoints -> 20,
Prolog -> ManganeseBlue,
Epilog -> {Red,PointSize[0.02],
Map[Point,pts]}];

First, take n = 2.

In[438]:=

alpha = 1.5;
n = 10;

eulerTable = euler[f,a,b,n,alpha];
TableForm[eulerTable,
TableSpacing -> {0,3}]

Out[441]//TableForm=

 xi wi Euler RowBox[{-, 2.}] 1.5 RowBox[{-, 1.5}] 1.25 RowBox[{-, 1.}] 1.125 RowBox[{-, 0.5}] 1.1875 0. 1.53125 0.5 2.29688 1. 3.69531 1.5 6.04297 2. 9.81445 2.5 15.7217 3. 24.8325

These values get too large to plot on our picture, so let's make a smaller table of Euler dots.

In[442]:=

a = -2; b = 1;    (* a <= x <= b *)
c = -1; d = 4;    (* c <= y <= d *)

alpha = 1.5;
n = 6;

smallEulerTable = euler[f,a,b,n,alpha];
TableForm[smallEulerTable,
TableSpacing -> {0,3}]

Out[447]//TableForm=

 xi wi Euler RowBox[{-, 2.}] 1.5 RowBox[{-, 1.5}] 1.25 RowBox[{-, 1.}] 1.125 RowBox[{-, 0.5}] 1.1875 0. 1.53125 0.5 2.29688 1. 3.69531

In[448]:=

approx = ListPlot[smallEulerTable,
PlotStyle -> {PointSize[.02],OrchidDark}];

In[449]:=

a = -2; b = 3;    (* a <= x <= b *)
c = -1; d = 4;    (* c <= y <= d *)

field = PlotVectorField[{1,f[x,y]},
{x,a,b},{y,c,d},
PlotLabel -> "Euler Approximations",
AxesLabel -> {"x","y"},
Axes -> True,
PlotPoints -> 20,
Prolog -> ManganeseBlue,
Epilog -> {PointSize[0.02],
OrchidDark,Map[Point,smallEulerTable],
Red,Map[Point,pts]}];

Hughes-Hallett, Gleason, et al, Exercise 9.3.5, page 494

In[452]:=

First, take n = 2.

In[457]:=

alpha = -0.5;
n = 8;

eulerTable = euler[f,a,b,n,alpha];
TableForm[eulerTable,
TableSpacing -> {0,3}]

Out[460]//TableForm=

 xi wi Euler RowBox[{-, 2.}] RowBox[{-, 0.5}] RowBox[{-, 1.5}] 1.375 RowBox[{-, 1.}] 1.55469 RowBox[{-, 0.5}] 0.846161 0. 0.613167 0.5 0.42518 1. 0.459791 1.5 0.854087 2. 1.61435

In[461]:=

approx = ListPlot[eulerTable,
PlotStyle -> {PointSize[.02],OrchidDark}];

In[462]:=

field = PlotVectorField[{1,f[x,y]},
{x,a,b},{y,c,d},
PlotLabel -> "Euler Approximations",
AxesLabel -> {"x","y"},
Axes -> True,
PlotPoints -> 20,
Prolog -> ManganeseBlue,
Epilog -> {PointSize[0.02],
OrchidDark,Map[Point,eulerTable],
Red,Map[Point,pts]}];

Hughes-Hallett, Gleason, et al, Exercise 9.3.5, page 494

In[463]:=

In[467]:=

alpha = -0.5;
n = 8;

eulerTable = euler[f,a,b,n,alpha];
TableForm[eulerTable,
TableSpacing -> {0,3}]

Out[470]//TableForm=

 xi wi Euler RowBox[{-, 1.}] RowBox[{-, 0.5}] RowBox[{-, 0.75}] RowBox[{-, 0.125}] RowBox[{-, 0.5}] 0.046875 RowBox[{-, 0.25}] 0.0976563 0. 0.0888672 0.25 0.0666504 0.5 0.0656128 0.75 0.11171 1. 0.224407

In[471]:=

approx = ListPlot[eulerTable,
PlotStyle -> {PointSize[.02],OrchidDark}];

In[472]:=

field = PlotVectorField[{1,f[x,y]},
{x,a,b},{y,c,d},
PlotLabel -> "Euler Approximations",
AxesLabel -> {"x","y"},
Axes -> True,
PlotPoints -> 20,
Prolog -> ManganeseBlue,
Epilog -> {PointSize[0.02],
OrchidDark,Map[Point,eulerTable],
Red,Map[Point,pts]}];

Created by Mathematica  (April 25, 2004)