MATH1251 Calculus
Chapter 2: Oridinary Differential Equations
Numerical methods
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)
Example 2
Notation
Motivation
Rather than
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
d 2x
= n2 x.
dt 2
More important equations:
5
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
= 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
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.
10
Solution to an ODE
Implicit solution
Definition 6
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
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
y = xy
y(1) = 2
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
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
y = f (x, y)
y(x0 ) = y0
We will also learn how to solve second order ODEs with constant
coefficients.
17
Separable ODEs
18
Definition 14
A separable equation has the form
dy
= g(x)H(y),
dx
h(y)
(2)
Separable ODEs
dy
= g(x).
dx
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
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
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=
(3)
and N =
F
,
y
(4)
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
It turns out that the above condition is also sufficient for F to exist.
27
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
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
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)
Exact ODEs
35
36
Example 26
Solve
y
dy
=g
?
dx
x
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
39
40
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.
41
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
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.
MATH1231/1241 Notes
dP
= kP for k > 0 (Malthus 1798);
Exponential growth:
dt
Bounded growth:
y + ay + by = 0
where y = y(x) is an unknown function, a and b are constants.
dP
= k(Pc P);
dt
Logistic growth:
dP
= kP(Pc P) (Verhulst 1838).
dt
49
50
51
52
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)
53
y1 = e(+i)x = ex eix
y1 + y2
eix
= ex
2
ix
y1 y2
x e
z2 =
=e
2
A, B R;
z1 =
Let
y = Aex + Bxex ,
54
and y2 = e2 x
y = Ae1 x + Be2 x ,
+ eix
= ex cos x
2
eix
= ex sin x.
2
A, B R.
55
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
57
Example 36
58
Example 37
y + 6y + 9y = 0,
y(0) = 0, y (0) = 5.
59
60
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
62
Example 41
If an (NE) has right-hand side
f (x) = xe2x + 4x cos(5x)
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
64
Example 42
Solve y + y = x 2 + x.
65
66
67
68
Example 43
Solve y + 4y + 4y = e2x .
Example 44
Solve y + y = cos x such that y(0) = 3 and y (0) = 0.
69
70
Example 45
x (t) + 2 x(t) = 0
x (0) = 0.
2 + 2 = 0,
which has two complex solutions = i.
x(0) = 4,
71
sin t
yp = C cos t + D sin t.
Hence
73
2 sin t
yp = 2
.
2
74
y = yh + yp = A cos t + B sin t + yp .
2
sin t,
2 2
2
cos t.
y = A sin t + B cos t + 2
2
y = A cos t + B sin t +
yp = Ct cos t + Dt sin t.
2
.
2 2
75
76
Case 2: = . Then
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
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
77
y=0
Characteristic equation:
2 + + 2 = 0,
shock
absorber
spring
y (t) = Ae+ t + Be 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.
road surface
d 2 y(t)
2 4 2
.
2
a(t)
wheel
78
y(t)
mass
2
4
79
80
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
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) =
T (y) = y + x 2 y,
T (y) = y + ay + by
Second order ODEs
83
(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
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
= T (y1 ) + T (y2 )
and
T (y1 ) = (y1 ) + a(y1 ) + b(y1 )
85
86
(17)
In this case,
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 ).
87
88
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.
89
Eulers method
Consider the IVP
90
Example 49
(
y = f (x, y),
y(x0 ) = y0 .
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,
y0 = 1
y1 = y0 + hf (x0 , y0 ),
xn
0
0.1
0.2
0.3
k = 0, 1, 2, . . . ,
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
h = (b-a)/N;
x = a:h:b;
d = length(y0);
y0 = y0(:); % convert y0 to a column array
%
%
%
%
%
%
%
%
%
%
%
%
%
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.
Numerical methods
93
Numerical methods
94
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))
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
Numerical methods
96
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 |
0.0545
0.0259
Numerical methods
97
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.)
h = 0.2
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
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?
Numerical methods
103
Numerical methods
102