Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

M20_CROF5939_04_SE_C20.

QXD 9/27/18 12:45 PM Page 1011

Computational approaches to differential equations


BLOCK 4

4.1 Introduction

Computer software packages are readily available that can solve differential equations.
You may recall that when using software to perform integration the results produced
fall into two categories, symbolic and numerical. This is also true when we use
computers to solve differential equations.
For many equations it is possible for the computer to obtain a symbolic answer in
terms of common functions, and we shall illustrate this shortly. However, for many
equations it is impossible to obtain a symbolic answer and so techniques, known as
numerical methods, exist for obtaining approximate solutions. One technique for
solving first-order equations is Euler’s method. In general, numerical techniques are
laborious to perform by hand and so are best implemented using a computer. We
shall be content to illustrate Euler’s method using simple examples, and then rely
upon software to tackle more substantial problems.

4.2 Use of symbolic algebra packages to solve first-order differential


equations exactly

Many software packages designed for tackling mathematical problems have the
facility to solve first-order equations. You should enquire whether the package(s) to
which you have access can solve differential equations symbolically. You may need
to refer to local documentation. Some of the packages require additional software
to be loaded. For example, the standard version of Matlab does not come with a
symbolic processor, but requires a Symbolic Math Toolbox.

Example 4.1 Finding an exact, general solution


dy
Use a computer algebra package to solve the differential equation - xy = 0.
dx
This is a first-order equation, which can be solved using the method of separation of
variables.
M20_CROF5939_04_SE_C20.QXD 9/27/18 12:45 PM Page 1012

20 1012 Block 4 Computational approaches to differential equations

Solution

Maple
In Maple the command dsolve can be used to find the general solution of a differential
equation with dependent variable y and independent variable x. y must be input as y(x),
and the derivative term is input as diff(y(x), x), which stands for the derivative of y(x)
dy
with respect to x. So, to solve the equation - xy = 0 the Maple command is
dx
dsolve(diff(y(x),x)⫺x*y(x) ⫽ 0);
and Maple outputs
y(x) = —C1eA2x B
1 2

The output —C1


1 2
is the arbitrary constant, and so we can interpret this general solution as
simply y = Ae2x .

Matlab
Assuming the Symbolic Math Toolbox is available, the function dsolve computes symbolic
solutions to differential equations. In Matlab the symbol D is used to denote the derivative.
The command
dsolve('Dy-x*y=0','x')
generates the result
C1*exp(1>2*xN2)
Note the requirement to put the differential equation and the independent variable
between apostrophes. Clearly the output is not as user-friendly as that provided by
Maple, although another Matlab command, pretty, can be applied to generate more
familiar output.

The power of software packages lies in their ability to handle symbolic constants in
problems. Consider the following example.
Example 4.2 Electrical Engineering – RL circuit with sinusoidal input
In question 2, End of block exercises, Block 3 the integrating factor method was
used to solve the equation
di
iR + L = sin vt
dt
where R, L and v are constants, in order to find the current i(t) in an RL circuit. Use
a computer algebra package to obtain the general solution of this equation.
M20_CROF5939_04_SE_C20.QXD 9/27/18 12:45 PM Page 1013

4.2 Use of symbolic algebra packages to solve first-order differential equations exactly 1013 20
Solution

Maple
In Maple the relevant command is
dsolve(i(t)*R+L*diff(i(t),t) = sin(w*t));
and Maple produces the following output:

Rt -L w cos(wt) + R sin(wt)
i(t) = e( - L ) –C1 +
R2 + w2L2

where – C1 represents the arbitrary constant in the general solution. Compare this
solution with the one stated analytically in question 2 on page 1010.

Matlab
In Matlab the symbol i is used to denote 2 -1 and so should not be used here. Instead
we use y as the dependent variable. The command
dsolve('y*R+L*Dy = sin(w*t)','t')
results in the output
(-L*w*cos(w*t) + R*sin(w*t) + exp(-1>L*R*t)*C1*R^2 +
exp(-1>L*R*t)*C1*w^2*L^2)>(R^2 + w^2*L^2)
This is equivalent to that produced by Maple, although the format is not so user-friendly.

Example 4.3 Finding an exact, particular solution


dy
Find the particular solution of - x2y = 0 that satisfies the initial condition
dx
y(0) = 3.

Solution
Software packages will have the facility to handle initial conditions.

Maple
In Maple the appropriate command is
dsolve({diff(y(x),x) - x^2*y(x) = 0,y(0) = 3});
and Maple outputs the particular solution\
y(x) = 3e13x 2
1 3
M20_CROF5939_04_SE_C20.QXD 9/27/18 12:45 PM Page 1014

20 1014 Block 4 Computational approaches to differential equations

Matlab
The command
dsolve('Dy-x^2*y=0','y(0) = 3','x')
results in the output
3*exp(1>3*x^3)

Computer and calculator exercises

1 Use a computer algebra package to find the 2 Use a computer algebra package to find the
general solutions of the following differential exact, particular solution of the following
equations: equations:
dy xy du
(a) = 2 (a) = -ku, u(0) = u0
dx x + 1 dt
dv di
(b) m = -v where m is a constant (b) iR + L = E, i(0) = 0, where R, L
dt dt
and E are constants

Solutions to exercises

1 (a) y(x) = C2x2 + 1 E Ee(-Rt>L)


2 (a) u(t) = u0 e-kt (b) i(t) = -
(b) v(t) = Ce-t>m R R

4.3 Euler’s method

Although symbolic algebra packages are extremely powerful, there are nevertheless
some differential equations that they cannot solve exactly. In such cases it may be
possible to use the computer to obtain an approximate solution. There are many
sophisticated techniques available for doing this, the details of which can be found in
textbooks on numerical methods. The simplest technique is Euler’s method, which
we explain here.
Euler’s method is a numerical technique for finding approximate solutions of
differential equations having the form

dy
= f (x, y), y(x0) = y0
dx
M20_CROF5939_04_SE_C20.QXD 9/27/18 12:45 PM Page 1015

4.3 Euler’s method 1015 20


Note that to use this method we need to be given an initial condition y(x0) = y0.
dy
When solving this equation numerically we use the fact that is the gradient of the
dx
tangent to y(x). We know from the given equation that the point (x0, y0) lies on
dy
the graph of the solution. We also know that the gradient of the solution is , that is
dx
f (x, y). So, at the point (x0, y0) the graph of the solution has a tangent line with gradi-
ent f (x0, y0). This is illustrated in Figure 4.1.

Figure 4.1
y
A tangent line is
used to obtain an
estimate of the true True solution
solution.
y (x1)
y1 Tangent line approximation

y0

h
x0 x1 x

The point (x0, y0) lies both on the solution curve and on the tangent line. Consider
moving to a nearby point on the tangent line. Let the nearby point be (x1, y1), where
x1 = x0 + h. The value of h and hence x1 is known. Then (x1, y1) is on the tangent
line, but not on the solution curve. When x = x1 the value of the solution is y(x1). We
do not know the value of y(x1). We can, however, use y1 as an approximation to y(x1).
y1 - y0
Referring to Figure 4.1 we note that the gradient of the tangent line is , and so
h
y1 - y0
= f (x0, y0)
h
It follows by rearrangement that y1 = y0 + hf (x0, y0). So, knowing the values
of x0, y0, the function f (x, y) and the value of h we can calculate y1. Note from
Figure 4.1 that y(x1) is the true, but unknown, solution, whereas y1 is now the known,
but approximate, solution.
We can use the point (x1, y1) as a new starting point to find an approximate solu-
tion at x2 = x1 + h, and using an identical argument this is y2 = y1 + hf(x1, y1).
The process is repeated using the general formula yn + 1 = yn + hf (xn, yn). The
result is a sequence of values, yn, which approximates the true solution y(x) at the
points where x = xn.

Key point Euler’s method


dy
An approximate solution of = f (x, y), subject to the initial condition
dx
y(x0) = y0, is given by
yn + 1 = yn + h f (xn, yn), y0 = y (x0)
where h is the step-size, or increment, in x.

You might also like