Sie sind auf Seite 1von 26

Outline

MATH1251 Calculus
Chapter 2: Oridinary Differential Equations

First order ODEs

Separable ODEs
First order linear ODEs
Exact ODEs
Substitutions for ODEs
Modelling with first order ODEs

Second order ODEs

The homogeneous case
The non-homogeneous case
Modelling with second order ODEs

Linear ODEs: a theoretical view

Numerical methods

A/Prof Thanh Tran

School of Mathematics and Statistics
The University of New South Wales
Sydney, Australia
Red Centre Room 4061
Email: thanh.tran@unsw.edu.au

What is an ordinary differential equation (ODE)?

General form
Definition 1
An ordinary differential equation or ODE is an equation involving one
independent real variable (e.g. x R), one dependent real variable
(e.g. u : R R) and finitely many of its derivatives. Thus it is of the
form
F (x, u(x), u (x), . . . , u (n) (x)) = 0.
(1)

A differential equation is an equation that relates an unknown

function with one or more of its derivatives.
When only ordinary derivatives, rather than partial derivatives, are
involved, the equations are called ordinary differential equations
(ODEs).

Example 2

Differential equations involving partial derivatives are called partial

differential equations (PDEs). They are not studied in this course.

The following are examples of ODEs

u (x) = 2u(x) + 3x
u (x) = u (x) + u 2 (x) + x 3

Notation

Motivation

Rather than

Engineering and Science are built on models of the physical

world. Mostly, these models are mathematical models which allow
prediction of future or distant quantities.

u (x) = u (x) + xu 2 (x) + x 3

we can write
y (x) = y (x) + xy 2 (x) + x 3

At school you studied the solutions of (at least) two ordinary

differential equations. These were the growth-decay equation,

y = y + xy 2 + x 3
d 2y
dy
+ xy 2 + x 3
=
dx
dx 2
= u + tu 2 + t 3
u

dP
= kP
dt
and the simple harmonic motion equation

Whether we use x or t, y or u or f does not matter. What matters is the

structure of the equation. Thus the general form (1) can also be written
as
F (x, y, y , . . . , y (n) ) = 0.
ODEs and IVPs

d 2x
= n2 x.
dt 2
More important equations:
5

ODEs and IVPs

Order of an ODE
Definition 3
Black and Scholes equation (1973).
V
1
2V
V
+ 2 S 2 2 + rS
rV = 0
t
2
S
S

Example 4

dy
dx
y + 2xy
 2
dy
+y
dx
y + xy
 2 3
d y
+ y 5y
dx 2

~2 2

(~x , t) + V (x, t)(~x , t)

i~ (~x , t) =
t
2m
Nobel Prize for Physics (1933).

ODEs and IVPs

= y + x 2,
= 0,
= 0,
= sin y,
= ex .
8

Linear ODEs
Which equations in Example 4 are linear?
Linear first order ODEs can always be solved by using an integrating
factor. This will be discussed in the next section.

Definition 5
An nth order ODE is said to be linear if it is a linear combination of the
first n derivatives of the dependent function

If a0 , a1 , . . . are constants (independent of x) then the general

solution may be found if one can solve a certain nth degree
polynomial equation; see Section 3.

d ny
d n2 y
dy
d n1 y
+ an1 (x) n1 + an2 (x) n2 + . . . + a1 (x)
+ a0 (x)y = f (x).
dx n
dx
dx
dx
If f (x) 0 the ODE is homogeneous.

Second order linear ODEs with non-constant coefficients a0 and a1

are studied in MATH2130.

ODEs and IVPs

10

Solution to an ODE

Implicit solution

Definition 6

Example 8

given equation.

Show that the function y given by

Example 7
Show that y =

y + tan1 y =
x4
2
5x 2 is a solution to y = y + x 3 .
2
x

is a solution to
y =

11

x2
2

x(1 + y 2 )
.
2 + y2

12

Initial value problems (IVPs)

Example 10
Solve y = ex given y(0) = 3 and y (0) = 2.

Definition 9
An initial value problem is an ODE of order n together with a set of
values of the solution and its derivatives up to its first (n 1)
derivatives at some fixed point. These values are called the initial
conditions of the IVP. More precisely, an IVP reads as: Find a function
y satisfying
F (x, y, y , . . . , y (n) ) = 0,
y(x0 ) = C0 ,

y (x0 ) = C1 ,

...,

13

14

Example 12
Example 11

1

y = xy

y = x 2 /2 + 1;

y(1) = 2

and y = x 3 ;

xy = y 1 and y(0) = 1, which has infinitely many solutions,

namely, y = 1 + cx where c is an arbitrary constant;

ODEs and IVPs

Let f (x, y) = xy. Then the slope of the solution curve at the point (1, 2)
is f (1, 2) = 2. The slope of the solution curve at a point (x, y) is f (x, y).
This helps to get an idea how the solution curve looks like near the
point (1, 2). Graph: . . .

15

16

(cont.)

Solving ODEs and IVPs

Remarks
There is no universal method for solving ODEs. For some ODEs it
may not even be possible to find an implicit solution. In this case
one has to resort to numerical methods to find an approximate
solution. Two simple numerical methods will be presented in the
last section. More efficient methods are topics of higher year
courses (e.g., MATH2301).

Theorem 13
Assume that f : R2 R is continuous. If on some rectangle S
containing (x0 , y0 ) there is an M > 0 such that
|f (x, y1 ) f (x, y2 )| M|y1 y2 | for all (x, y1 ) S, (x, y2 ) S,
then the IVP

In the sequel we will learn solution techniques for three particular

first order ODEs, namely, separable equations, linear equations,
and exact equations.

y = f (x, y)
y(x0 ) = y0

We will also learn how to solve second order ODEs with constant
coefficients.

17

Separable ODEs

18

Solving a separable equation

Letting h(y) = 1/H(y), we can rewrite (2) as

Definition 14
A separable equation has the form
dy
= g(x)H(y),
dx

h(y)
(2)

i.e., we can separate x and y.

Example 15
Which of the following equations is separable?
q
dy
= x 1 y2
dx
dy
= ex+y
dx
dy
= ln(xy).
dx
First order ODEs

Separable ODEs

dy
= g(x).
dx

Integrate both sides with respect to x:

Z
Z
dy
h(y)
dx = g(x) dx
dx
or
Z
Z
h(y) dy = g(x) dx.
Carry out the integrations and try to find an explicit expression of y
as a function of x if possible.
If we have the IVP defined with y(x0 ) = y0 then we obtain
Z y
Z x
h(s) ds =
g(t) dt.
y0

19

x0

Separable ODEs

20

Example 17

Example 16
Solve y = x

Solve

1 y 2.

y =

Separable ODEs

21

Example 19

Solve

dy
+ f (x)y = g(x),
dx

ey
,
x2 + 1

y(0) = 1.

Separable ODEs

22

24

dy
+ 3y = ex .
dx

where f and g are given functions.

Solution technique
Calculate the integrating factor: h(x) = e
constant of integration).

f (x) dx

(ignoring the

Multiply the given equation by h(x) and use the product rule for
differentiation for the left-hand side to rewrite the resulting
equation as

d
h(x)y = g(x)h(x).
dx
Integrate both sides. Dont forget the constant of integration!
First order ODEs

23

First order ODEs

Example 20
Solve

Example 21

y
dy
+ = x subject to y = 4 when x = 3.
dx
x

Solve x

25

Exact equations

dy
+ (x + 1)y = 2.
dx

If
M=

Many first order ODEs can be written in the form

dy
M(x, y) + N(x, y)
= 0.
dx
d
If the left-hand side can be written as
F (x, y(x)) then the
dx
equation has a solution in implicit form F (x, y) = C.

(3)

and N =

F
,
y

(4)

then reversing the above steps, we could conclude that the

solution to (3) was F (x, y) = C.

Recall that if F and all its partial derivatives up to order 2 are

2F
2F
=
. This together with (4) implies
xy
yx

continuous, then

F (x, y) = C,
which defines y implicitly in terms of x. By differentiating with
respect to x we obtain

N
M
=
.
y
x

F
F dy
+
= 0.
x
y dx
Exact ODEs

F
x

26

exists?

When does such a case happen? Given a function F (x, y) and

any constant C, consider the expression

First order linear ODEs

It turns out that the above condition is also sufficient for F to exist.
27

First order ODEs

Exact ODEs

28

Example 23
Solve (2x + y + 1)dx + (2y + x + 1)dy = 0.

Definition 22
An ODE of the form
M(x, y) + N(x, y)
is called exact if

dy
=0
dx

(5)

N
M
=
.
y
x

Note
Equation (5) is also written in the form
M(x, y)dx + N(x, y)dy = 0.

Exact ODEs

29

First order ODEs

Exact ODEs

30

(6)
(7)
(8)
Example 24
(9)

Solve

2xy + 3x 2 y 2
dy
.
= 2
dx
x + 2x 3 y + 1

Note
We can first integrate (7) with respect to y (treating x as a constant),
then compute F /x and compare with (6). Note that the constant (cf.
(8)) is C1 (x).

Exact ODEs

31

First order ODEs

Exact ODEs

32

(10)

Example 25
Solve
(x 2 y + y 3 )

dy
+ xy 2 + sinh(2x) = 0.
dx

(12)

Exact ODEs

33

Exact ODEs

34

(13)
(14)

Substitutions

(15)

If the ODE is not linear, separable or exact then it may be possible to

use a change of variables so that the ODE in the new variables is
linear, separable or exact.
y 
dy
Equation of the form
=g
dx
x
Setting y(x) = xv(x) will lead to a separable ODE.
Proof: Differentiating y = vx with respect to x and substituting into the
given equation yield
dv
x + v = g(v),
dx
or
dv
g(v) v
=
.
dx
x
This is a separable equation.

Exact ODEs

35

36

Example 26
Solve

dy
+ x 2 + y 2 = 0.
2xy
dx

y 
dy
=g
?
dx
x

Homogeneous function of degree 0

To check if the ODE

dy
= F (x, y)
dx
y 
dy
or not, check whether F is homogeneous of
=g
is of the form
dx
x
degree 0, that is,
F (x, y) = F (x, y)

37

Example 27
Solve

R.

38

Example 28

y  y2 + x2
dy
= sin
.
dx
x y2 x2

Show that the substitution v(x) = y 4 (x) transforms the equation

2xy y = 10x 3 y 5
into a linear equation. Then solve the ODE.

39

40

Newtons Law of Cooling

Example 29
A dead body is discovered by a cleaner in a hotel room at 11am. A
forensic scientist arrives at the scene at 11.30am and records the
temperature of the body at 24.5 C. The body temperature is recorded
again an hour later at 24.0 C. Estimate the time of death given that the
hotel room is at constant temperature 20 C and normal body
temperature is 36.5 C.

The rate of decrease of temperature is proportional to the difference

between the temperature of an object and its surroundings
dT
= k (T (t) TA )
dt
where
TA is the ambient temperature in C
T (t) is the temperature of object after time t
k is some unknown proportionality constant
This is widely used in forensics.

41

Modelling with first order ODEs

42

Mixing problems
Example 30
Initially 50 grams of salt is dissolved in a tank holding 300 litres of
water. A brine solution is pumped into the tank at a rate of 3 litres/min
and the well-stirred solution is then pumped out at a slower rate of 2
litres/min. If the concentration of the solution entering is 2 grams/litre,
determine the amount of salt in the tank at any time.

43

44

45

46

47

Modelling with first order ODEs

48

Population models
Example 31
A culture initially has x0 bacteria. At t = 1 hour the number of bacteria
is measured to be 32 x0 . If the rate at which bacteria grows is
proportional to the number of bacteria present, determine the time
necessary for the number of bacteria to triple.

Second order linear ODEs with constant coefficients

MATH1231/1241 Notes
dP
= kP for k > 0 (Malthus 1798);
Exponential growth:
dt

Homogeneous Equation (HE)

Bounded growth:

y + ay + by = 0
where y = y(x) is an unknown function, a and b are constants.

dP
= k(Pc P);
dt

Non-homogeneous Equation (NE)

y + ay + by = f (x)

Logistic growth:

First order ODEs

dP
= kP(Pc P) (Verhulst 1838).
dt

where y = y(x) is an unknown function, a and b are constants, and

f (x) is a given function.

49

50

The homogeneous case

Lemma 32
If y1 and y2 are two solutions of (HE) then so is Ay1 + By2 where A and
B are constants.
Lemma 33
(HE) has two linearly independent solutions, and every solution is a
linear combination of these solutions.
Proof: See e.g. MATH2601, MATH2221.

51

52

How to find two linearly independent solutions of (HE)?

Example 34
Solve y 5y + 6y = 0.

Rewrite (HE): y + ay + by = 0.
Look for a solution that does not change too much when
differentiated so that the terms on the left-hand side cancel each
other out to give zero.
Try y = ex where is a constant to be determined.
Differentiate and substitute into (HE):

ex 2 + a + b = 0,

2 + a + b = 0.

(16)

So, y = ex is a solution to (HE) if and only if is a solution

of (16). This equation is called the characteristic equation of (HE).
Second order ODEs

53

Three different cases

y1 = e(+i)x = ex eix

y1 + y2
eix
= ex
2
ix
y1 y2
x e
z2 =
=e
2

A, B R;

z1 =

see Lemma 33.

Case 2: If the characteristic equation (16) has one double root ,
then y1 = e1 x . It turns out that y2 = xe1 x is another solution to
(HE). Hence, the general solution to (HE) is

Let

y = Aex + Bxex ,

54

We want to write the general solution as a real-valued function.

and y2 = e2 x

y = Ae1 x + Be2 x ,

Case 3: If the characteristic equation (16) has two complex roots

1 = + i and 2 = i, then

Case 1: If the characteristic equation (16) has two distinct real

solutions 1 and 2 , then
y1 = e1 x

+ eix
= ex cos x
2
eix
= ex sin x.
2

Due to Lemma 32, the functions z1 and z2 are two solutions of

(HE). They are linearly independent; hence, the general solution
to (HE) is

y = ex A cos x + B sin x , A, B R.

A, B R.
55

The homogeneous case

56

Summary

Example 35
Solve the IVP
y 4y 5y = 0,

Characteristic equation
2 + a + b = 0
1 6= 2 R
1 = 2 (= )
1 = + i, 2 = i

Second order ODE

y + ay + by = 0
y = Ae1 x + Be2 x
y = Aex + Bxex

y = ex A cos x + B sin x

57

Example 36

58

Example 37

y + 6y + 9y = 0,

Second order ODEs

y(0) = 0, y (0) = 5.

59

60

The non-homogeneous case

Example 40
Solve y 5y + 6y = 6x + 7.

Lemma 38
If y1 and y2 are solutions of (NE) then y1 y2 is a solution of (HE).
Proof: Exercise!
Lemma 39
The general solution of (NE) is given by y = yh + yp where yh is the
general solution of (HE) and yp is a particular solution of (NE).
We have learnt how to find yh .
How to find a particular solution yp of (NE)?

61

The non-homogeneous case

62

Example 41
If an (NE) has right-hand side
f (x) = xe2x + 4x cos(5x)

RHS f (x) of (NE)

P(x) (polynomial of degree n)
P(x)ekx
P(x)ekx cos(x)
P(x)ekx sin(x)

Guess for particular solution yp

Q(x) (polynomial of degree n)
Q(x)ekx

ekx Q1 (x) cos(x) + Q2 (x) sin(x)
ekx Q1 (x) cos(x) + Q2 (x) sin(x)

and
yh = (A + Bx)e2x ,
what is a guess for yp ?

Note
If any term of the guess for yp is part of yh , then multiply that term
by x.
If the new guess is still part of yh , multiply by x again.

63

The non-homogeneous case

64

Example 42
Solve y + y = x 2 + x.

65

66

67

The non-homogeneous case

68

Example 43
Solve y + 4y + 4y = e2x .

The non-homogeneous case

Example 44
Solve y + y = cos x such that y(0) = 3 and y (0) = 0.

69

70

Example 45

If the object is pulled downwards from its equilibrium position by a

distance of 4 units and then released from rest, find x(t) when t 0.

Suppose that a spring is mounted to a fixed point

P and that an object of mass m is suspended
from the spring. Let x denote the vertical
displacement from the equilibrium position of the
object, taking x to be positive if it lies above the
equilibrium position.
It can be shown (by using Newtons second law
of motion and Hookes law) that

The displacement x(t) is the solution to the following IVP:

x (t) + 2 x(t) = 0,

x(t) = A cos t + B sin t,

where A and B are constants.
A = 4. Since

x (t) + 2 x(t) = 0

Modelling with second order ODEs

x (0) = 0.

2 + 2 = 0,
which has two complex solutions = i.

where is a positive constant depending only on

m and the stiffness of the spring. We ignore
gravity, friction, and air resistance.
Second order ODEs

x(0) = 4,

71

the initial condition x (0) = 0 gives B = 0.

B = 0. Therefore x(t) = 4 cos t.
Second order ODEs

72

Step 2: To find a particular solution yp to the given equation we

consider two different cases.
Case 1: 6= . We look for a particular solution in the form

Example 46 (Vibrating support)

If the point P vibrates up and down and the
displacement is given by 2 sin t, then it can be
shown that x obeys the differential equation
x (t) + 2 x(t) = 2 sin t.

sin t

yp = C cos t + D sin t.

By substituting into the given equation we obtain





2 2 C cos t + 2 2 D sin t = 2 sin t.

Describe the motion of the object, given that

x(0) = 4 and x (0) = 0.

Step 1: The general solution to the corresponding homogeneous

equation is yh = A cos t + B sin t; see Example 45.

Hence

73

2 sin t
yp = 2
.
2

Second order ODEs

74

y = yh + yp = A cos t + B sin t + yp .

We impose the initial conditions to find A and B.

Case 1: 6= . Then

2C sin t + 2D cos t = 2 sin t.

2
sin t,
2 2
2
cos t.
y = A sin t + B cos t + 2
2
y = A cos t + B sin t +

t cos t
yp =
.

Step 3: The general solution to the given equation is

yp = Ct cos t + Dt sin t.

2
.
2 2

75

76

Case 2: = . Then

Since y(0) = 4 the first equation gives A = 4.

Since y (0) = 0 the second equation gives

1
t cos t,

1
y = A sin t + B cos t cos t + t sin t.

y = A cos t + B sin t

2
B + 2
=0
2
or
B=
Hence
y = 4 cos t

Since y(0) = 4 the first equation gives A = 4.

1
1
Since y (0) = 0 the second equation gives B = 0 or B = 2 .

Hence


1
t
1
1
cos t.
y = 4 cos t+ 2 sin t t cos t = 2 sin t 4 +

2
.
( 2 2 )

2
2
sin t + 2
sin t.
( 2 2 )
2

This is a resonance. As t increases, the amplitude of cos t grows

without bound and the system is unstable.

77

m

y (t) + y (t) + 2 y(t) = 0, : friction, : natural frequency

y=0

Characteristic equation:
2 + + 2 = 0,

shock
absorber

spring

y (t) = Ae+ t + Be t ,

y (t) = e 2 t (A cos t + B sin t) ,

d
(y(t) a(t))
dt
dt 2
my (t) + cy (t) + ky(t) = ca (t) + ka(t)=: f (t).
m

= k(y(t) a(t)) c

< 0.

d 2 y(t)

2 4 2
.
2

a(t)

wheel

78

y(t)
mass

2
4

Critically damped case: = 2

y (t) = Ae 2 t + Bte 2 t .

79

80

Critical damping is the fastest way to return to equilibrium without oscillations

(overshooting). Car shock absorbers are designed to achieve critical
damping.

y (t) + y (t) + 2 y(t) = cos t

The preceding analysis shows that y = yH + yP yP as t .

1.0

0.8

Particular solution

0.6

yP = a sin t + b cos t

underdamped
critically damped
overdamped

y(t)

0.4

with a =

,
2 + 2 2

b=

,
2 + 2 2

where = 2 2 .

0.2

Amplitude:

0
2

10

81

AMP =

a2 + b 2 =

1
2 + 2 2

82

Linear ODEs and Linear Algebra

Let C(I) denote the space of all continuous functions y : I R,
where I is an interval in R.
For n = 1, 2, . . . , let C n (I) denote the space of all n-time
continuously differentiable functions y : I R, i.e., y, y , y , . . . ,
y (n) are continuous functions defined on I.
Let a0 , a1 , . . . , an1 be continuous functions. We define the map
T that maps a function y in C n (I) into a function in C(I) by (see
Definition 5)

Maximum amplitude: For what forcing frequency (i.e., ) is the steady

state amplitude at a maximum?
d
d AMP
= 0
(2 + 2 2 ) = 0
d
d
2
2
= 2 = max
= 2
2
1
1
.
= AMPmax = q
2

2 4

d n1 y
d ny
d n2 y
dy
+an1 (x) n1 +an2 (x) n2 +. . .+a1 (x) +a0 (x)y.
dx n
dx
dx
dx
T is called a transformation or an operator.
For example:
T (y) =

Conclusion: If is small then max and AMPmax is large. This is

resonance.

T (y) = y + x 2 y,
T (y) = y + ay + by
Second order ODEs

83

Linear ODEs: a theoretical view

(n = 1)
for some fixed a, b R, (n = 2)
84

T is a linear operator

Proof.
For simplicity we prove only the case T (y) = y + ay + by. For all
y1 , y2 C n (I) and R,
T (y1 + y2 ) = (y1 + y2 ) + a(y1 + y2 ) + b(y1 + y2 )

Theorem 47

T (y) =

= (y1 + ay1 + by1 ) + (y2 + ay2 + by2 )

d n2 y
dy
d ny
d n1 y
+ an1 (x) n1 + an2 (x) n2 + . . . + a1 (x)
+ a0 (x)y
dx n
dx
dx
dx

is a linear operator, i.e., for all y1 , y2 C n (I) and R there hold

= T (y1 ) + T (y2 )
and
T (y1 ) = (y1 ) + a(y1 ) + b(y1 )

= T (y1 ).

85

86

ker(T ) and solutions of the homogeneous equation

If yh ker(T ) and yp is any solution of (17), then

T (yh + yp ) = T (yh ) + T (yp ) = T (yp ) = f .

So yh + yp is a solution to (17).

Consider for simplicity second order linear ODEs with constant

coefficients only, i.e.,
y + ay + by = f .

(17)

In this case,

If y and yp are solutions of (17) then

T (y yp ) = T (y) T (yp ) = f f = 0,

T (y) = y + ay + by.

so that y yp ker(T ).
If yh is a solution of the homogeneous equation y + ay + by = 0
then T (yh ) = 0 so that yh ker(T ).

It follows that yh := y yp ker(T ). Hence all solutions of (17) are

of the form yh + yp .

If y is a solution of (17) then T (y) = f so that f is in the image of T .

Linear ODEs: a theoretical view

87

88

Numerical methods for IVPs

Example 48
The general solution of the first order linear equation
y + y/x = x

Consider the IVP y = sin(xy) and y(0) = 0. None of the methods

that we have presented allow you to solve this equation.

is y = C/x + x 2 /3; see Example 20.

In many practical situations, you may find that yo dont even have
an analytic expression for y , but rather just a black box which
allows you to dertermine y numerically at any given (x, y) point
that you enter.

Let T (y) = y + y/x, yh = C/x, and yp = x 2 /3. Then

T (yh ) = 0 and T (yp ) = x.
The general solution to the corresponding homogeneous equation
y + y/x = 0 is yh = C/x.

In situations like these, you can apply numerical methods to

approximate the solutions to such an IVP.

yp = x 2 /3.

89

Eulers method
Consider the IVP

90

Example 49
(
y = f (x, y),
y(x0 ) = y0 .

Use Eulers method with a step size of h = 0.1 to approximate the

solution of the IVP
y = x + y, y(0) = 1.

x > x0 ,

(Note: This IVP can be solved exactly, and the exact solution is
y(x) = 2ex 1 x.)
With h = 0.1, we have

For h > 0 sufficiently small (which is called the step size) we have

x0 = 0,

x2 = 0.2, y2 = y1 + h(t1 + y1 ) = 1.1 + 0.1(0.1 + 1.1) = 1.22

x3 = 0.3, y3 = y2 + h(t2 + y2 ) = 1.22 + 0.1(0.2 + 1.22) = 1.362

the above equation shows that y(x1 ) y1 .

If we define iteratively
yk +1 = yk + hf (xk , yk ),

y0 = 1

x1 = 0.1, y1 = y0 + h(t0 + y0 ) = 1 + 0.1(0 + 1) = 1.1

y1 = y0 + hf (x0 , y0 ),

xn
0
0.1
0.2
0.3

k = 0, 1, 2, . . . ,

then we can approximate y(xk ) by yk . This approach is called

Eulers method.
Numerical methods

Numerical methods

91

Numerical methods

y (xn )
1
1.1103418
1.2428055
1.3997176

yn
1
1.1
1.22
1.362

y (xn ) yn
0
0.0103418
0.0228055
0.0377176

Rel. error
0%
0.94 %
1.87 %
2.77 %
92

function [x,y] = Euler(fstring,a,b,y0,N)

h = (b-a)/N;
x = a:h:b;
d = length(y0);
y0 = y0(:); % convert y0 to a column array

%
%
%
%
%
%
%
%
%
%
%
%
%

Euler(fstring,y0,N) solves the ode

y(x) = f(x,y) for a < x < b,
with initial condition y(a) = y0.

y = zeros(d,N+1);
y(:,1) = y0;
for n = 2:N+1
y(:,n) = y(:,n-1) + h * fstring(x(n-1),y(:,n-1));
end

Inputs:
fstring: name of the function that computes f(x,y).
N: number of intervals in the grid.
Outputs:
x: row vector of length N+1
x(n) = a + (n-1) h, h = (b-a)/N.

To use this function you need a script file, say Eg50.m as on the next
page.

y: row vector of length N+1 containing the approx.

value y(n) of the exact solution y(x(n)).

To run this script, on the M ATLAB command window type

>> Eg50

Numerical methods

93

Numerical methods

94

Errors

As a rough rule of thumb, the absolute error in Eulers method is

proportional to the step size. So if we were to halve the step size we
would expect to roughly halve these errors.

a = 0; b = 1; y0 = 0; N = 20;
func = @(x,y) 1./(1+x.^2) - 2 * y.^2;
y_exact = @(x) x ./ (1 + x.^2);

Example 50
Suppose we use Eulers method to solve the following problem

[t,y] = Euler(func,a,b,y0,N);
xplot = linspace(a,b);
plot(t,y,*-r,xplot,y_exact(xplot),b)

1
y =
2y 2 ,
1 + x2
y(0) = 0,

info = [N = , num2str(N)];
text(0.05,2.5,char(info))

which has as exact solution

xlabel(x), ylabel(y)
title(Example 50)
legend(y_n, y(x), location,NorthWest)
print -depsc Example50.eps
Numerical methods

0 < x < 1,

y(x) =

x
.
1 + x2

The approximate solutions yn , together with the errors

En := y(xn ) yn , are given in the following table.
95

Numerical methods

96

Improved Euler method

Example 50 (cont.)
h = 0.2

h = 0.1

xn
0.2000
0.4000
0.6000
0.8000
1.0000
0.1000
0.2000
0.3000
0.4000
0.5000
0.6000
0.7000
0.8000
0.9000
1.0000

y(xn )
0.1923
0.3448
0.4412
0.4878
0.5000
0.0990
0.1923
0.2752
0.3448
0.4000
0.4412
0.4698
0.4878
0.4972
0.5000

yn
0.2000
0.3763
0.4921
0.5423
0.5466
0.1000
0.1970
0.2854
0.3609
0.4210
0.4656
0.4957
0.5137
0.5219
0.5227

En
-0.0077
-0.0315
-0.0509
-0.0545
-0.0466
-0.0010
-0.0047
-0.0102
-0.0160
-0.0210
-0.0244
-0.0259
-0.0259
-0.0247
-0.0227

maxn |En |

Eulers method to compute y1 can be derived as follows. It is

known that
Z x1
y(x1 ) = y(x0 ) +
y (x) dx.
x0

RIf xwe
1

x0 y (x) dx, i.e., if we approximate y (x) by y (x0 ) for all

x [x0 , x1 ] then (noting that h = x1 x0 )

0.0545

The approximation will be more accurate if we approximate the

integral by the trapezoidal rule
Z x1
y (x0 ) + y (x1 )
y (x) dx h
.
2
x0
This leads to an improved formula to compute y1 :
h
h
y1 = [y (x0 ) + y (x1 )] = [f (x0 , y0 ) + f (x1 , y1 )].
2
2

0.0259

Numerical methods

97

Improved Euler method (cont.)

Numerical methods

Example 51
We solve the same problem as in Example 50 with the Improved Euler
method. In the following table note the convergence of order O(h) for
Eulers method and order O(h2 ) for the Improved Euler method.
(E)
(H)
(En : absolute error by Eulers method; En : absolute error by the
Improved Euler method.)

Since we dont know y1 on the right hand side of the above

formula, we approximate it by its Euler approximation:
u1 = y0 + hf (x0 , y0 )
h
y1 = [f (x0 , y0 ) + f (x1 , u1 )].
2

h = 0.2

Improved Euler algorithm: For k = 0, 1, 2, . . . ,

xk +1 = xk + h
h = 0.1

uk +1 = yk + hf (xk , yk )
h
yk +1 = [f (xk , yk ) + f (xk +1 , uk +1 )].
2
This method is also called Heuns method.
Numerical methods

98

99

tn
0.2000
0.4000
0.6000
0.8000
1.0000
0.1000
...
0.7000
0.8000
0.9000
1.0000

Numerical methods

(E)

En
-0.0077
-0.0315
-0.0509
-0.0545
-0.0466
-0.0010
...
-0.0259
-0.0259
-0.0247
-0.0227

(E)

maxn |En |

0.0545

0.0259

(H)

En
0.0042
0.0082
0.0105
0.0104
0.0089
0.0005
...
0.0023
0.0022
0.0021
0.0019

(H)

maxn |En |

0.0105

0.0023
100

method

method (cont.)

function [t,y] = Heun(fstring, a, b, y0, N)

% Heun(fstring, y0, N) solves the ode
%
y(t) = f(t,y) for a < t < b,
% with initial condition y(a) = y0.
%
% Inputs:
%
fstring: function that compute f(t,y)
%
N: number of intervals
% Outputs:
%
t: row vector of length N+1 containing the points
%
t(n) = a + (n-1)h, h = (b-a)/N.
%
y: row vector of length N+1 containing the approx.
%
values y(n) of the exact solution y(t(n)).

h = (b-a) / N;
t = a:h:b;
d = length(y0);
y0 = y0(:);
y = zeros(N,d);
y(1,:) = y0;
for n = 2:N+1
v = y(n-1) + h * feval(fstring, t(n-1), y(n-1));
y(n) = y(n-1) + ( h / 2 ) * ...
( feval(fstring, t(n-1), y(n-1)) + ...
feval(fstring, t(n), v) );
end

Numerical methods

101

Better methods?

More accurate methods can be derived, e.g. Runge-Kutta methods.

These will be learnt in MATH2301.

Numerical methods

103

Numerical methods

102