Preface
This is a tutorial made solely for the purpose of education and it was designed for students taking Applied Math 0330. It is primarily for students who have very little experience or have never used Mathematica before and would like to learn more of the basics for this computer algebra system. As a friendly reminder, don't forget to clear variables in use and/or the kernel.
Finally, the commands in this tutorial are all written in bold black font, while Mathematica output is in normal font. This means that you can copy and paste all commands into Mathematica, change the parameters and run them. You, as the user, are free to use the scripts for your needs to learn the Mathematica program, and have the right to distribute this tutorial and refer to this tutorial as long as this tutorial is accredited appropriately.
Return to computing page for the second course APMA0340
Return to Mathematica tutorial for the first course APMA0330
Return to Mathematica tutorial for the second course APMA0340
Return to the main page for the course APMA0330
Return to the main page for the course APMA0340
Return to Part III of the course APMA0330
Euler's Methods

We start with the first numerical method for solving initial value problems that bears Euler's name. Leonhard Euler was born in 1707, Basel, Switzerland and passed away in 1783, Saint Petersburg, Russia. In 1738, he became almost blind in his right eye. Euler was one of the most eminent mathematicians of the 18th century, and is held to be one of the greatest in history. He is also widely considered to be the most prolific mathematician of all time. He spent most of his adult life in Saint Petersburg, Russia, except about 20 years in Berlin, then the capital of Prussia.
In 1768, Leonhard Euler (St. Petersburg, Russia) introduced a numerical method that is now called the Euler method or the tangent line method for solving numerically the initial value problem:
To start, we need mesh or grid points, that is, the set of discrete points for independent variable at which we find approximate solutions. In other words, we will find approximate values of the unknow solution at these mesh points. For simplicity, we use uniform grid with fixed step length h; however, in practical applications, it is almost always not a constant. For convenience, we subdivide the interval of interest [a,b] (where usually \( a= x_0 \) is the starting point) into m equal subintervals and select the mesh/grid points
There are three main approaches (we do not discuss others in this section) to derive the Euler rule: either use finite difference approximation to the derivative or transfer the initial value problem to the Volterra integral equation and then truncate it, or apply Taylor series.
- We start with finite difference method. If we approximate the derivative in the left-hand side of the differential equation y' = f(x,y) by the
finite difference
\[ y' (x_n ) \approx \frac{y_{n+1} - y_n}{h} \]on the small subinterval \( [x_{n+1} , x_n ] , \) we arrive at the Euler's rule when right-hand side is evaluated at x = xn.\begin{equation*} y_{n+1} = y_n + (x_{n+1} - x_n ) f( x_n , y_n ) \qquad \mbox{or} \qquad y_{n+1} = y_n + h f_n , \end{equation*}where the following notations are used: \( h=x_{n+1} - x_n \) is the step length (which is assumed to be constant for simplicity), \( f_n = f( x_n , y_n ) \) is the value of slope function at mesh point, and \( y_n \) denotes the approximate value of the actual solution \( y=\phi (x) \) at the point \( x_n \) (\( n=1,2,\ldots \) ).
- Integrating the identity y'(x) = f(x,y(x)) between xn and xn+1, we obtain
\[ y(x_{n+1}) - y(x_n ) = \int_{x_n}^{x_{n+1}} f(t, y(t))\,{\text d} t . \]Approximating the integral by the left rectangular (very crude) rule for numerical integration (length of interval times value of integrand at left end point) and identifying \( y(x_n ) \) with yn, we again obtain the Euler formula.
- We may assume the possibility of expanding the solution in a Taylor series around the point xn:
\[ y(x_{n+1}) = y(x_n +h) = y(x_{n}) + h\, f(x_n, y(x_n )) + \frac{1}{2}\, h^2 \,y'' (x_n ) + \cdots . \]The Euler formula is the result of truncating this series after the linear term in h.
To avoid the instability and to prevent the appearance of ghost solutions, many refine numerical procedures to integrate the ordinary differential equations have been proposed. In practical calculations, the central difference scheme is used when evaluations of slope function in intermediate points (as, for instance, required by Runge--Kutta methods) are not permitted. We mention two remedies to remove ghost solutions.
Consider the following mixed difference scheme parameterized by μ, 0 <μ<1, for the first order differential equation:Let φ be a real-valued function on \( \mathbb{R} \) that satisfies the property:
- nonstandard explicit Euler method given by
\[ \frac{y_{k+1} - y_k}{\phi (hq)/q} = f(y_k ), \qquad y_0 = y(0), \quad k=0,1,2,\ldots . \]
- nonstandard implicit Euler method given by
\[ \frac{y_{k+1} - y_k}{\phi (hq)/q} = f(y_{k+1} ), \qquad y_0 = y(0), \quad k=0,1,2,\ldots . \]
Example: Consider the initial value problem for the logistic equation
h = 0.1
y[1] = y[0] + h*y[0]*(1 - y[0])
Do[y[n] = y[n - 2] + 2*h*y[n - 1]*(1 - y[n - 1]), {n, 2, 500}]
ListPlot[Table[{n*h, y[n]}, {n, 0, 500}]]

Another approach is based on transferring the given IVP \( y' = f(x,y) , \quad y(x_0 ) = y_0 \) to equivalent integral equation
Example: We demonstrate how the Euler method works on an example of a linear equation when all calculations become transparent. Note that Mathematica attempts to solve the ODE using its function. A solution may or may not be possible using this function. In case when a solution may not be known or determined, Euler's method provides an alternative to approximate the solution numerically. Consider the initial value problem:
x0 := 0 (* starting point in x *)
y0 := 0 (* starting value for y *)
xf=2.0; (* Value of x at which y is desired *)
h = (xf - x0)/5.0 (* step size in x *)
plot = Plot[Evaluate[y[x] /. soln], {x, x0, xf},
PlotStyle -> {{Thickness[0.01], RGBColor[1, 0, 0]}}]
X[1] = X[0] + h
Y[1] = Y[0] + f[X[0], Y[0]]*h
plot1 = ListPlot[{{X[0], Y[0]}, {X[1], Y[1]}}, Joined -> True,
PlotStyle -> {Thickness[0.005], RGBColor[0, 0, 1]}, DisplayFunction -> Identity]
Y[2] = Y[1] + f[X[1], Y[1]]*h
plot2 = ListPlot[{{X[0], Y[0]}, {X[1], Y[1]}, {X[2], Y[2]}}, Joined -> True,
PlotStyle -> {Thickness[0.005], RGBColor[0, 0, 1]}, DisplayFunction -> Identity]
Y[3] = Y[2] + f[X[2], Y[2]]*h
plot3 = ListPlot[{{X[0], Y[0]}, {X[1], Y[1]}, {X[2], Y[2]}, {X[3], Y[3]}}, Joined -> True,
PlotStyle -> {Thickness[0.005], RGBColor[0, 0, 1]}, DisplayFunction -> Identity]
Y[4] = Y[3] + f[X[3], Y[3]]*h
plot4 = ListPlot[{{X[0], Y[0]}, {X[1], Y[1]}, {X[2], Y[2]}, {X[3], Y[3]}, {X[4], Y[4]}}, Joined -> True,
PlotStyle -> {Thickness[0.005], RGBColor[0, 0, 1]}, DisplayFunction -> Identity]

In the above codes, a special option DisplayFunction was used. Actually, you can either remove this option or replace with a standard one: DisplayFunction -> $DisplayFunction. All Mathematica graphics functions such as Show and Plot have an option DisplayFunction,which specifies how the Mathematica graphics and sound objects they produce should actually be displayed. The way this works is that the setting you give for DisplayFunction is automatically applied to each graphics object that is produced.
DisplayFunction -> Identity generate no display
DisplayFunction -> f apply f to graphics objects to produce display
Within the Mathematica kernel, graphics are always represented by graphics objects involving graphics primitives. When you actually render graphics, however, they must be converted to a lower-level form which can be processed by a Mathematica front end, such as a notebook interface, or by other external programs. The standard low-level form that Mathematica uses for graphics is PostScript. The Mathematica function Display takes any Mathematica graphics object, and converts it into a block of PostScript code. It can then send this code to a file, an external program, or in general any output stream.
We combine all steps into subroutine:h=(xf-x0)/n;
Y=y0;
For[i=0,i<n,i++,
Y=Y+f[x0+i*h,Y]*h]; Y]
n is number of steps
x0 = initial condition for x
y0 = initial condition for y
xf = final value of x at which y is desired.
f = slope function for the differential equation dy/dx = f(x,y)
Do[
Nn[i]=2^i;
H[i]=(xf-x0)/Nn[i];
AV[i]=EulerMethod[2^i,x0,y0,xf,f];
Et[i]=EV-AV[i];
et[i]=Abs[(Et[i]/EV)]*100.0;
If[i>0,
Ea[i]=AV[i]-AV[i-1];
ea[i]=Abs[Ea[i]/AV[i]]*100.0;
sig[i]=Floor[(2-Log[10,ea[i]/0.5])];
If[sig[i]<0,sig[i]=0];
] ,{i,0,nth}];
AV = approximate value of the ODE solution using Euler's Method by calling the module EulerMethod
Et = true error
et = absolute relative true error percentage
Ea = approximate error
ea = absolute relative approximate error percentage
sig = least number of significant digits correct in approximation
The following code uses Euler's Method to calculate intermediate step values for the purpose of displaying the solution over the range specified. The number of steps used is the maximum value, n (in this example, n=128).
X[0]=x0;
Y[0]=y0;
h=(xf-x0)/n;
For[i=1,i<=n,i++,
X[i]=x0+i*h;
Y[i]=Y[i-1]+f[X[i-1],Y[i-1]]*h; ];
plot2 = ListPlot[data, Joined -> True,
PlotStyle -> {Thickness[0.005], RGBColor[0, 0, 1]}];

Now we compare the value at the final point (denoted by xf) with a step size
plot3 = ListPlot[data, Joined -> True,
PlotStyle -> {Thickness[0.006], RGBColor[0.5, 0.5, 0]}, PlotLabel ->
"Approximate value of the solution of the ODE\nat x = xf as a \ function of step size"]

plot4 = ListPlot[data, Joined -> True,
PlotStyle -> {Thickness[0.006], RGBColor[0, 0.5, 0.5]}, PlotLabel -> "Approximate value of the solution of the ODE \n at x = xf as a \ function of number of steps "]

EV = (y /. First[soln])[xf]
data = Table[{Nn[i], Et[i]}, {i, 0, nth}]; plot5 = ListPlot[data, Joined -> True,
PlotStyle -> {Thickness[0.006], RGBColor[0.5, 0, 0.5]}, PlotLabel ->
"True error as a function of number of steps"]

plot6 = ListPlot[data, AxesOrigin -> {0, 0},
Joined -> True, PlotStyle -> {Thickness[0.006], RGBColor[0.3, 0.3, 0.4]},
PlotLabel -> "Absolute relative true error percentage \n as a function of number of steps"]

plot7 = ListPlot[data, AxesOrigin -> {0, 0},
Joined -> True, PlotStyle -> {Thickness[0.006], RGBColor[0.7, 0.3, 0]},
PlotLabel -> "Approximate error \nas a function of number of steps"]

plot9 = ListPlot[data, AxesOrigin -> {0, 0},
Joined -> True, PlotStyle -> {Thickness[0.006], RGBColor[0, 0.3, 0.7]},
PlotLabel -> "Absolute relative approximate error percentage \nas a function of \ number of steps"]

Example: To demonstrate the latter approach, we consider the initial value problem for the integro-differential equation
For the general step in Euler's rule, we have
T[0] := h^2 *50^2
y[0] := 50
y[1] := y[0] + 2.3*h*50 - 0.01*h*50^2 - 0.1*h^2*50^2
Do[ T[k] = T[k - 1] + h*(y[k] + y[k - 1])/2;
y[k + 1] = y[k] + 2.3*h*y[k] - 0.01*h*(y[k])^2 - 0.1*y[k]*h*T[k], {k, 1, 2000}]
ListPlot[Table[{k*h, y[k]}, {k, 1, 2000}]]

There are several ways to implement the Euler numerical method for solving initial value problems. We demonstrate its implementations in a series of codes. To start, define the initial point and then the slope function:
{x0, y0} = {0, 1}
f[x_, y_] = x^2 + y^2 (* slope function f(x,y) = x^2 + y^2 was chosen for concreteness *)
Next, define the step size:
Plot with some options: Another way is to make a loop. Clear[y] First of all, it is always important to clear all previous
assignment to all the variables that we are going to use, so we have to type: Clear[y] The basic structure of the loop is: First, we start with output of our program, which is, perhaps, the most important
part of our program design. You'll notice that in the program
description, we are told that the "solution that it
produces will be returned to the user in the form of a list of
points. The user can then do whatever one likes with this output, such
as create a graph, or utilize the point estimates for other purposes. Next, we must decide upon the information that must be passed to the
program for it to be able to achieve this. In programming jargon,
these values are often referred to as parameters. So what parameters
would an Euler program need to know in order to start solving the
problem numerically? Quite a few should come to mind: The slope function f(x, y) To code these parameters into the program, we need to decide on actual
variable names for each of them. Let's choose variable names as
follows: f, for the slope function f(x, y) There are many ways to achieve this goal. However, it is natural to
have the new subroutine that looks similar to build-in Mathematica's commands. So our program
might look something like this: In order to use Euler's method to generate a numerical solution to an
initial value problem of the form: We have to decide upon what interval, starting at the initial point x0, we desire to find the
solution. We chop this interval into small subdivisions of length h, called step size. Then, using the
initial condition \( (x_0,y_0) \) as our starting point, we generate the rest of the
solution by using the iterative formulas: If you would like to use a built-in Euler’s method, unluckily there is none. However, we can define it
ourselves. Simply copy the following command line by line:
Now we have our euler function: euler[f(x,y), {x,x0,x1},{y,y0},steps]
or
y[0]=1; f[x_,y_]=x^2 +y^2 ;
Do[y[n + 1] = y[n] + h f[1+.01 n, y[n]], {n, 0, 99}]
y[10]
Do[some expression with n, {n, starting number, end number}]
or with option increment, denoted by k:
Do[some expression with n, {n, starting number, end number, k}]
The function of this Do loop is to repeat the expression, with n taking values from “starting number” to “ending number,” and therefore repeat the expression (1+ (ending number) - (starting number)) many times. For our example, we want to iterate 99 steps, so n will go from 0, 1, 2, ...,
until 99, which is 100 steps in total. There is just one technical issue: we must have n as an
integer. Therefore we let y[n] denote the actual value of y(x) when x = n*h. This way everything is an
integer, and we have our nice do loop.
Finally put y[10] , which is actualy y(0.1), then shift+return, and we have our nice answer.
The initial x-value, x0
The initial y-value, y0
The final x-value,
The number of subdivisions to be used when chopping the solution
interval into individual jumps or the step size.
x0, for the initial x-value, x0
y0, for the initial y-value, y0
xn, for the final x-value
Steps, for the number of subdivisions
h, for step size
Block[{ xold = x0, yold = y0, sollist = {{x0, y0}}, h },
h = N[(xn - x0) /Steps]; (* or n = (xn - x0) /Steps//N; *)
Do[ xnew = xold + h;
ynew = yold + h * (f /. {x -> xold, y -> yold});
sollist = Append[sollist, {xnew, ynew}];
xold = xnew;
yold = ynew,
{Steps}
];
Return[sollist]
]
Then this script will solve the differential equation y’=f(x,y), subject to the initial condition y(x0)=y0, and generate all values
between x0 and x1. The number of steps for the Euler’s method is specified with steps.
To solve the same problem as above, we simply need to input:
Example: Consider the initial value problem \( y' = x+y, \quad y(0) =1. \)
1.72102}, {0.6, 1.94312}, {0.7, 2.19743}, {0.8, 2.48718}, {0.9, 2.8159}, {1., 3.18748}}
We can plot the output as follows

Or we can plot it as
1.72102}, {0.6, 1.94312}, {0.7, 2.19743}, {0.8, 2.48718}, {0.9,
2.8159}, {1., 3.18748}}

The following code, which uses a slightly different programming
paradigm, implements Euler's method for a system of differential equations:
Module[{t, Y, h = (b - a)/Steps//N, i},
t[0] = a; Y[0] = Y0;
Do[
t[i] = a + i h;
Y[i] = Y[i - 1] + h F[t[i - 1], Y[i - 1]],
{i, 1, n}
];
Table[{t[i], Y[i]}, {i, 0, n}]
]
And the usage message is:
euler::usage = "euler[F, t0, Y0, b, n] gives the numerical solution
to {Y' == F[t, Y], Y[t0] == Y0} over the interval\n
[t0, b] by the n-step Euler's method. The result
is in the form of a table of {t, Y} pairs."
Note that this function uses an exact increment h rather than converting
it explicitly to numeric form using N. Of course you can readily change
that or accomplish the corresponding thing by giving an approximate
number for one of the endpoints.
Next we plot the points by joining them with a curve:

Another way without writing a subroutine:
x0 = 0;
y0 = 1;
xend = 1.1;
steps = 5;
h = (xend - x0)/steps // N;
x = x0;
y = y0;
eulerlist = {{x, y}};
For[i = 1, i <= steps, y = f[x, y]*h + y;
x = x + h;
eulerlist = Append[eulerlist, {x, y}]; i++]
Print[eulerlist]
The results can also be visualized by connecting the points:
s = NDSolve[{u'[t] == f[x, u[t]], u[0] == 1}, u[t], {t, 0, 1.1}];
b = Plot[Evaluate[u[x] /. s], {x, 0, 1.1}, PlotStyle -> RGBColor[1, 0, 0]];
Show[a,b]

Next, we demonstrate application of Function command:
Module[{h = (t1 - t0)/n // N, tt, yy},
tt[0] = t0; yy[0] = y0;
tt[k_ /; 0 < k < n] := tt[k] = tt[k - 1] + h;
yy[k_ /; 0 < k < n] :=
yy[k] = yy[k - 1] + h f[tt[k - 1], yy[k - 1]];
Table[{tt[k], yy[k]}, {k, 0, n - 1}]
];
Plot[Interpolation[ty][t], {t, 1, 2}]

Here is another steamline of the Euler method (based on Mathematica function), which we demonstrate in
the following
Example: Consider the initial value problem \( y' = y^2 - 3\, x^2 , \quad y(0) =-1. \)
f[x_, y_] := y^2 - 3*x^2
x[1] = 0; y[1] = -1;
h = 0.25; K = IntegerPart[2.5/h]
Do[ { x[i+1] = x[i]+h,
y[i+1] = y[i] +h*f[x[i],y[i]] } , {i,1,K+1} ]
Now we can plot our results, making sure we only refer to values of x and y that we have defined: Now we are going to repeat the problem, but using Mathematica lists format instead. The idea is still the
same---we set a new x value, compute the corresponding y value, and save the two quantities in two
lists. In particular, in a list we are simply storing values; In previously presented code, we were actually
storing rules, which are more complicated to store and evaluate. We start out with just one value in each
list (the initial conditions). Then we use the Append command to add our next pair of values to the list.
pairs = Table[{x[i], y[i]}, {i, 1, K+1}]
plot5 = ListPlot[pairs, Joined -> True, PlotStyle -> {Thickness[0.005], RGBColor[0, 1, 0]}]
plot1 = Plot[Evaluate[y[x] /. soln], {x, 0, 2.5},
PlotStyle -> {{Thickness[0.008], RGBColor[1, 0, 0]}}];
Show[plot1, plot5]
x = {0.0 };
y = {2.0 };
h = 0.1;
Do [ { x = Append[x,x[[i]]+h],
y = Append[y,y[[i]]+h*f[x[[i]],y[[i]] ] ],
} , {i,1,20} ]
Now we can plot our results:
pairs = Table[ {x[[i]], y[[i]] }, {i,1,21} ]
ListPlot [ pairs, Joined -> True ]
Euler's method is first-order accurate, so that errors occur one order
higher starting at powers of \( h^2 . \)
Euler's method is implemented in NDSolve as ExplicitEuler:
Method -> "ExplicitEuler", "StartingStepSize" -> 1/10]
II. Backward Euler Method
Backward Euler formula:
Example: Consider the following initial value problem:
h = 0.1; (* step size *)
t[0] = 0.; (* starting vaue of the independent value *)
M = Round[0.5/h]; (* number of points to reach the final destination, in our case it is 0.5 *)
toler = h; (* define the tolerance *)
Do[
t[n + 1] = t[n] + h;
eqn = (z == y[n] + h (z^3 - 3 t[n + 1]) );
ans = z /. NSolve[eqn, z, Reals];
indlist = {};
toler = h;
While[ Length[indlist] == 0,
toler = toler*2.;
indlist = Flatten[Position[Map[(Abs[y[n] - #] < toler) &, ans], True]];
];
ind = indlist[[1]];
y[n + 1] = ans[[ind]];
, {n, 0, M}]
Then we plot solution:
y[M]
t[M]

NSolve has to spend time to compute all roots to the equation (which can be computationally
expensive). FindRoot does a pretty fast search looking for only a single root, so it is quick
for complex equations.
If you care about all possible roots, or if you have no clue where the roots of the equation may be,
FindRoot is a terrible choice. If you only care about a single root and have a rough idea of
where it might be, though, FindRoot will find it quickly.
Fixed Point Iteration
Bracketing Methods
Secant Methods
Euler's Methods
Heun Method
Runge-Kutta Methods
Runge-Kutta Methods of order 2
Runge-Kutta Methods of order 3
Runge-Kutta Methods of order 4
Polynomial Approximations
Error Estimates
Adomian Decomposition Method
Modified Decomposition Method
Multistep Methods
Multistep Methods of order 2
Multistep Methods of order 4
Milne Method
Hamming Method
Return to Mathematica page
Return to the main page (APMA0330)
Return to the Part 1 (Plotting)
Return to the Part 2 (First Order ODEs)
Return to the Part 3 (Numerical Methods)
Return to the Part 4 (Second and Higher Order ODEs)
Return to the Part 5 (Series and Recurrences)
Return to the Part 6 (Laplace Transform)