Numerical methods

The following sections are concerned with the theory underlying the numerical solution of ODEs such as numerical differentiation and Euler’s method.

Numerical differentiation

Recall from the previous unit ‘Basic calculus in MATLAB’ that the derivative of a function y with respect to the variable x can be approximated by

dydx(x)y(x)y(xδx)δx, the backward difference formula; or

dydx(x)y(x+δx)y(x)δx, the forward difference formula.

where δx is small. The smaller δx is, the better the approximation. The notation here is slightly different from unit 3, since we are using h=δx.

Euler’s method for solving first order ODEs

We wish to solve the first order ODE dydx=f(x,y), subject to the initial condition y(a)=ya, on the domain axb.

We will do this using Euler’s method and proceed as follows:

  • The first step is to divide the domain up into n equally sized intervals of size δx=ban.

  • Then we define x0=a, xi=a+iδx and xn=b, and let y0=ya.

  • We now want to find a sequence of numbers y1yn such that yiy(xi). Using the approximations for the derivative from above we can approximate the first order initial value problem by yi+1yiδx=f(x,y).

If we know f(x,y), then rearranging this gives a formula for calculating yi+1 given yi.

There are various options for evaluating f(x,y) which are discussed now.

Forward vs backward Euler

Evaluating f(x,y) at xi gives yi+1yiδx=f(xi,yi), which can be rearranged to give an explicit formula for yi+1, yi+1=yi+δxf(xi,yi), which is known as the forward Euler method.

Evaluating f(x,y) at xi+1 gives yi+1yiδx=f(xi+1,yi+1), which can be rearranged to give an implicit formula for yi+1, yi+1δxf(xi+1,yi+1)=yi. which is known as the backward Euler method. In general, this system is non-linear in yi+1.

It is usually simpler to work out the forward Euler than the backward Euler approximation, but it is often possible to use a coarser mesh with the backward Euler method, since it remains stable for a larger step size.

Euler method for systems of ODEs

We can extend this to solve systems of first-order ODEs by treating y as a vector (y) and f as a vector of functions (f).