Numerical Methods
Numerical Methods
Introduction
ax2 + bx + c = 0
Numerical methods are methods for solving problems using √
−b ± b2 − 4ac
computers. Given a mathematical problem, there are two ⇒ x= .
2a
ways to get the solution to the problem.
It gives a solution in closed form. However, in most
• Analytical methods problems we do not know the analytical methods, for
example, solve the following for x
• Numerical methods
numerical 6
method
• Numerical methods – Using computer to generate Any value of t would determine the value of y(t).
‘approximate’ solution to the required precision.
On the other hand, numerical method gives y(t) at specific
values of t, for example,
Numerical solution to a problem is quite different from the
analytical solution. As an example, consider the problem
y(0.1) = 1.1787, y(0.2) = 1.3105, y(0.3) = 1.3900, ···.
d2 y
+ 4y = 0, y(0) = 1, y ′(π) = 2. Note that these values are only approximations of the true
dt2
values given by (1).
The analytical solution is
first 3 terms
ϵ 6
a − ã
r= = . (3)
a a
PJY Wong 13 PJY Wong 14
|ϵ| = 0.00033... < 0.5 × 10−3 = 0.0005 |ϵ| = 0.003 < 0.5 × 10−2 = 0.005
0.00033... 0.003
|r| = ≈ 0.001 < 0.5 × 101−3 = 0.005 |r| = ≈ 0.00013 < 0.5 × 101−4 = 0.0005
a a
one
number the
-
Consider the equation ave
one number f (a) · f (b) < 0.
O f (x) = 0. (1)
Then, f has at least
- -
a zero in [a, b].
O ∗
We say that x is a zero of f (or a root of equation (1)) if
-
Summary:
f (x∗) = 0.
20
f (a) · f (b) < 0 =⇒ f (x) = 0 has a root in [a, b]
--
-
absolute error = |x̃ − x∗| ≤ < 0.5 × 10−n
m
holds
PJY Wong 23 PJY Wong If inequality , 24
means it is correct to
n decimal place
.
How to get m? 2. Fixed-Point Iteration Method
|f ′(x)| ≥ m > 0 for all x ∈ [a, b].
• First, we transform the equation f (x) = 0 algebraically into
* To get m, there are 2 ways: the form
-
(i) Find the minimum of |f ′(x)| for x ∈ [a, b], take rewrite x = g(x). (4)
-
(ii) Find a lower bound of |f ′(x)| for x ∈ [a, b], take and in general
m = this lower bound.
-
e .
S .
x = g(x ), n = 0, 1, 2, · · · . (5)
(f'()(
n+1 n
= x+1 , x z(i , b]
bounds :
*
PJY Wong
lower
25 PJY Wong will it converse to 26
bourd)
1 Subsc =
min If'()) = 1 +) = 2
(largest lower
x[ [ 3]
,
Note that we may get several different forms of (4), Theorem 2.1. (Convergence of fixed-point iteration)
and the behaviour of corresponding iterative sequences of Let x∗ be a solution of x = g(x) and suppose that g is
x0, x1, · · · may differ accordingly, in particular, with respect continuously differentiable in some interval J containing x∗.
to divergence/convergence and how fast it converges. A Then, if |g ′(x)| ≤ L < 1 in J , the iteration process defined
sufficient condition for convergence is given by the following by (5) converges for any x0 in J .
theorem.
Summary: J is an interval containing a root x∗. If
|g ′(x)| ≤ L < 1 for all x ∈ J ,
then the iteration process xn+1 = g(xn) converges for any x0
in J .
* To get L, there are 2 ways: Step 1. Find an interval [a, b] that contains a root of
f (x) = 0. (Note: f (a)f (b) < 0)
(i) Find the maximum of |g ′(x)| for x ∈ [a, b], take
′ Step 2. Change f (x) = 0 to the form x = g(x).
D L = max |g (x)|. x∈[a,b]
* There are many different g(x) possible. How to make sure that the g(x)
′ chosen is such that xn+1 = g(xn) converges to a solution of f (x) = 0?
(ii) Find an upper bound of |g (x)| for x ∈ [a, b], take
e
L = this upper bound.
C ′
* Theorem 2.1. If |g (x)| ≤ L < 1 in [a, b], then x n+1 = g(xn) converges
.
S .
for any x0 in [a, b].
(g'x)) = 3 -
j ,
x[[1 2) ,
check if converge
PJY Wong 29 PJY Wong 30
upper bounds :
bounds
1g' ( >) / < 3 01
.
:
-
Max 1g'ki)
Submi = 2 3 (smallest upper bound
xE[1 2)
.
Step 3. Check that Step 4. Start the iteration process xn+1 = g(xn) with any
|g ′(x)| ≤ L < 1 for all x ∈ [a, b]. x0 ∈ [a, b].
Step 5. When to stop? Suppose we need the answer to be
* To get L, there are 2 ways:
correct up to 3 decimal places. There are 2 ways:
D (i) Find the maximum of |g ′(x)| for x ∈ [a, b], take (i) Stop when the first 3 decimals of xn stop varying.
L = max |g ′(x)|.
x∈[a,b] (Not so good ...)
(ii) Find an upper bound of |g ′(x)| for x ∈ [a, b], take *(ii) Use Theorem 1.1. First, obtain m such that
L = this upper bound. |f ′(x)| ≥ m > 0 for all x ∈ [a, b].
* LC)
PJY Wong
IN Radians 33 PJY Wong 34
x = g1(x) = 13 (x2 + 1) x + 1
3x =
[
gives the iterative scheme x = j
(x + 1)
<= (2)
g ,
1
xn+1 = (x2n + 1). (6)
3
PJY Wong 35 PJY Wong 36
E((5 + 1) =
5(1+ 1) 5 = = 0 . 0667
((d)
=
(2
=
g
,
j
Check
0 66066067
Step 4. (Stopping criterion) Suppose we require the solution converges to the smaller solution. .
-
xi
0 1
# If kil I
- -
′
m = min |f (x)| = min |3 − 2x| = min(3 − 2x) = 1. x
=
92(x) 1 0.66666667 0.56 m
0
36
1) +
.
6666667
x∈J x∈J x∈J
5(
- +
Subsc
=
1
=
2 0.48148148 0.21
5(0 --
.
= 0
48148148 3 0.41060814
.
0.06
|f (xn)| 4 0.38953301 0.017
= |f (xn)| < 0.5 × 10−2. (7)
m 5 0.38391199 0.00435 < 0.5 × 10−2
Step 5. Choose x0 = 1 ∈ J . Using (6) and (7), we get xn x5 = 0.38 is correct to 2 d.p.
-
↓
jeto 1
PJY Wong 37 PJY Wong 38
,
f(x) x
1
=
-
3x +
1)
g(x) 5(x"
= +
I
x0 = 3.000, x1 = 3.333, x2 = 4.037, x3 = 5.766,
&
"
x= 1
38391199 40
f(x) 32 3x + 1
-
<" >(2) + 10 ve
L :
converge
f(z)
- = - -
O
!1! 1 1
Step 2. Rewrite the given equation as L = max |g2(x)| = max !! 2 !! = max 2 = < 1.
′
x∈J x∈J x x∈J x 4
Make &"
converge
<" 3x + 1 =
1
0)
-
j x
,
interval [2 , 3]
Step 4. (Stopping criterion) Suppose we require the solution Step 5. Choose x0 = 3 ∈ J . Using (8) and (9), we get xn
to be correct to 3 decimal places. With J = [2, 3], let us find converges to the larger solution.
m.
n Godp
x
n |f (xn)|/m
Jo
m = min |f ′(x)| = min |3 − 2x| = min(2x − 3) = 1. 0 3
x∈J x∈J x∈J
1 2.66666667 0.11
Hence, the stopping criterion is 2 2.62500000 0.015
|f (xn)| 3 2.61904762 0.0023
= |f (xn)| < 0.5 × 10−3. (9)
m 4 2.61818182 0.00033 < 0.5 × 10−3
I
=
x= 1 -
+
x=
>2+ /
"sesion
1
x = g1(x) =
1 + x2
so that
1
xn+1 = . (10)
1 + x2n
PJY Wong 45 PJY Wong 46
-
to be correct to 2 decimal places. Let us find m. Clearly,
n Ostp
xn |f (xn)|/m
of
converge
to pet x = g2(x) = 1 − x3
i intersection so that
|g2′ (x)| = 3x2.
-
Since |g2′ (x)| is greater than 1 near the solution x∗ =
·
0.682328..., we cannot guarantee convergence for the
iterative scheme
xn+1 = 1 − x3n.
A
This method is commonly used. Advantages include
TR
TR
simplicity and fast convergence. The underlying idea is that
the graph of f is approximated by suitable tangents.
EX
EX
PJY Wong 53 PJY Wong 54
Step 1. tan β1 = f ′(x0) = f (x 0 ) If it happens that f ′(xn) = 0 for some n, then try another
A
x0−x1
f (x 0 ) x0. Newton-Raphson method converges provided x0 is close
⇒ x1 = x0 − f ′ (x 0 ) enough to x∗. From the figure below we see that if x0 is
TR
Step 2. tan β2 = f ′(x1) = f (x 1 ) chosen from approximately the interval [a, b], then the method
x1−x2
f (x 1 )
should converge.
⇒ x2 = x1 − f ′ (x 1 )
So find a, b such that f (a)f (b) < 0, and pick x0 ∈ [a, b].
Step (n + 1). Newton-Raphson method
EX
f (xn)
xn+1 = xn − , n = 0, 1, 2, · · · . (12)
f ′(xn)
A
Solve f (x) = 0.
TR
TR
Step 1. Find an interval [a, b] that contains a root of
f (x) = 0. (Note: f (a)f (b) < 0)
Step 2. Pick x0 ∈ [a, b] that is close to the root. Start the
iteration process
f (xn)
EX
EX
xn+1 = xn − ′ , n = 0, 1, 2, · · · .
f (xn)
Step 3. When to stop? Same stopping criterion as fixed-
point iteration method (see step 5).
A
the interval [0, 1].
TR
Since f (1) > 0 and f (0) < 0, the equation has a root in
[a, b] = [0, 1].
From (12) we have
x3n + xn − 1
EX
xn+1 = xn − .
3x2n + 1
(Steps 1–2 considered done)
E
A
to 4 decimal places. For x ∈ [0, 1],
n xn f (xn) f ′(xn) |f (xn)|/m
TR
0 1
′ 2
|f (x)| = |3x + 1| ≥ 1 ≡ m. 1 0.75 ··· ··· 0.17
2 0.68604651 ··· ··· 0.009
Hence, the stopping criterion is 3 0.68233958 ··· ··· 0.000028 < 0.5 × 10−4
EX
Hence, x3 = 0.6823 is correct to 4 decimal places.
|f (xn)|
= |f (xn)| < 0.5 × 10−4.
m
PJY Wong 61 PJY Wong 62
Example 3.2. Find the positive solution of 2 sin x = x. (Stopping criterion) Suppose we want the solution to correct
A
$ %
to 4 decimal places. For x ∈ π2 , 2 ,
Solution Setting f (x) = x − 2 sin x, we have f ′(x) = 1 −
" #
2 cos x. Since f π2 < 0 and f (2) > 0, the equation has a π
TR
$ % |f ′(x)| = |1 − 2 cos x| ≥ 1 − 2 cos = 1 ≡ m.
root in [a, b] = π2 , 2 . 2
EX
1 − 2 cos xn |f (xn)|
= |f (xn)| < 0.5 × 10−4.
(Steps 1–2 considered done) m
A
n xn f (xn) f ′(xn) |f (xn)|/m
TR
0 2
1 1.90099559 ··· ··· 0.009
2 1.89551165 ··· ··· 0.0000285 < 0.5 × 10−4
EX
Hence, x3 = 1.8955 is correct to 4 decimal places.
A
formula gives
The quality of an iteration method can be judged by its
TR
TR
speed of convergence. xn+1 = g(xn)
= g(x∗ + xn − x∗)
Let xn+1 = g(xn) define an iteration method. Let xn
= g(x∗) + g ′(x∗)(xn − x∗) + 12 g ′′(x∗)(xn − x∗)2 + · · ·
approximate a solution x∗ of x = g(x). Then,
= g(x∗) − g ′(x∗)ϵn + 12 g ′′(x∗)ϵ2n + · · · . (13)
EX
EX
xn = x∗ − ϵn,
The exponent of ϵn in the first non-vanishing term after
where ϵn is the error of xn. g(x∗) is called the order of the iteration process defined by
g. The order measures the speed of convergence.
PJY Wong 67 PJY Wong 68
Subtract x∗ (= g(x∗)) from both sides of equation (13), of convergence. Thus,
A
we have
ϵn+1 ≈ g ′(x∗)ϵn in the case of first order
TR
TR
1
xn+1 − x∗ = −g ′(x∗)ϵn + g ′′(x∗)ϵ2n + · · ·
2 1
ϵn+1 ≈ − g ′′(x∗)ϵ2n in the case of second order.
2
1
− ϵn+1 = −g ′(x∗)ϵn + g ′′(x∗)ϵ2n + · · ·
⇒ (14)
2
EX
EX
where ϵn+1 is the error of xn+1.
On the RHS of (14), the remaining expression approximately
equals its first nonzero term because |ϵn| is small in the case
PJY Wong 69 PJY Wong 70
A
f ′′(x∗)
g ′′(x∗) =
f (x) f ′(x∗)
g(x) = x − which will not be zero in general. Hence,
f ′(x)
TR
and by differentiation, 1 f ′′(x∗)
ϵn+1 ≈ − g ′′(x∗)ϵ2n = − ′ ∗ ϵ2n.
2 2f (x )
f ′(x)2 − f (x)f ′′(x) f (x)f ′′(x)
g ′(x) = 1 − = .
f ′(x)2 f ′(x)2
EX
Since f (x∗) = 0, we get g ′(x∗) = 0. Thus Newton-Raphson’s
method is at least of second order.
&
C. Polynomial Interpolation of x between the points x0, x1, · · · , xn?
1. Lagrange Interpolation In polynomial interpolation, we find a polynomial pn(x)
of degree n (or less) that assumes the values f0, f1, · · · , fn
Suppose we are given a set of (n + 1) points x0, x1, · · · , xn at x0, x1, · · · , xn, i.e.,
and corresponding values f0, f1, · · · , fn of a function f at
pn(x) = anxn + an−1xn−1 + · · · + a1x + a0 (1)
these points.
x x0 x1 · · · xn fulfills
pn(x0) = f0, pn(x1) = f1, · · · , pn(xn) = fn
f (x) f0 f1 · · · fn
Theorem 1.1. If the points x0, x1, · · · , xn are distinct, then Note that (2) yields a system of (n+1) equations in (n+1)
there exists a unique polynomial pn(x) of degree n (or less) unknowns an, an−1, · · · , a0. By solving the system, we can
passing through the points (x0, f0), (x1, f1), · · · , (xn, fn). obtain the constants an, an−1, · · · , a0 and hence pn(x).
A
Each of l0(x), l1(x), · · · , ln(x) is of degree n, and pn(x0) = f0, pn(x1) = f1, · · · , pn(xn) = fn.
TR
TR
⎧ ⎫
⎨ 1, if i = j ⎬ Therefore, pn(x) given in (4) is indeed the interpolating
lj (xi) = 0 ≤ i, j ≤ n. (3)
⎩ 0, if i ̸= j ⎭ polynomial of f with respect to the nodes x0, x1, · · · , xn.
Since pn(x) given in (4) is in terms of Lagrange
The interpolating polynomial pn(x) can be expressed as polynomials, it is called the Lagrange interpolating
EX
EX
,n polynomial of f .
pn(x) = f0l0(x) + f1l1(x) + · · · + fnln(x) = k=0 fklk(x).
(4)
What is the expression of Lagrange polynomial lj (x), 0 ≤ Theorem 1.2. (Error estimate) Suppose f is (n + 1) times
A
j ≤ n? continuously differentiable. Then,
(x−x0)(x−x1)···(x−xj−1)(x−xj +1)···(x−xn)
zoi
TR
k ̸= j
min{x, x0, x1, · · · , xn} ≤ ξ ≤ max{x, x0, x1, · · · , xn}.
(7)
Moreover, from (6) we have the error inequality
PJY Wong 79 PJY Wong 80
! n !
!- 1 ! Summary
! !
A
|f (x) − pn(x)| ≤ ! (x − x k !×
)
-
-
(n + 1)!
-
-
! ! -
-
TR
max (8) interpolating polynomial pn(x) can be expressed as (4)
min{x,x0,x1,···,xn}≤ξ≤max{x,x0,x1,···,xn}
=
-
-
,n
pn(x) = f0l0(x) + f1l1(x) + · · · + fnln(x) = k=0 fklk(x)
** Think 1.1. If f is a polynomial of degree n, then what can
you observe from (6)? where the Lagrange polynomials lj (x) have the expression
EX
(5).
** Think 1.2. What do you think of the error |f (x) −
pn(x)| if x ∈ [x0, xn] (interpolation) and if x ̸∈ [x0, xn] 2. The error inequality is given by (8).
(extrapolation)?
PJY Wong 81 PJY Wong 82
A
ln 9.0 = 2.1972 and ln 9.5 = 2.2513,
p1(x) = f0l0(x)+f1l1(x) = 2.1972×2(9.5−x)+2.2513×2(x−9).
find the interpolating polynomial of ln x. Hence, compute an
TR
TR
approximate value of ln 9.2.
Further,
Solution From the data, we have
ln 9.2 ≈ p1(9.2) = 2.1972 · 2(9.5 − 9.2) + 2.2513 · 2(9.2 − 9)
n = 1, x0 = 9.0, x1 = 9.5, f0 = 2.1972, f1 = 2.2513.
= 2.2188.
EX
EX
We write down the Lagrange polynomials
(x−x1)
l0(x) = (x0−x1)
= x− 9.5
−0.5
= 2(9.5 − x),
(x−x0)
l1(x) = (x1−x0) = x− 9.0
0.5
= 2(x − 9).
A
point l0(x) = (x−9.5)(x−11)
,
(9.0−9.5)(9.0−11.0)
ln 9.0 = 2.1972, ln 9.5 = 2.2513, ln 11.0 = 2.3979, (x−9.0)(x−11)
l1(x) = ,
TR
TR
(9.5−9.0)(9.5−11.0)
compute an approximate value of ln 9.2 and obtain an error (x−9.0)(x−9.5)
l2(x) = (11.0−9.0)(11.0−9.5)
.
estimate.
The interpolating polynomial p2(x) is
Solution From the date, we have
EX
EX
n = 2, x0 = 9.0, x1 = 9.5, x2 = 11.0, p2(x) = f0l0(x) + f1l1(x) + f2l2(x).
Therefore, p2(9.2) = 2.2192 is correct to 4 decimal places. What are divided differences?
• Second order divided difference of f ⇒ f (x) = f (x0) + (x − x0)f [x, x0]. (9)
f (z1,z2)−f (z0,z1)
f [z0, z1, z2] = z2−z0
f [x0,x1]−f [x,x0]
Next, f [x, x0, x1] = x1−x
• Third order divided difference of f
f (z1,z2,z3)−f (z0,z1,z2)
f [z0, z1, z2, z3] = z3−z0
⇒ f [x, x0] = f [x0, x1] + (x − x1)f [x, x0, x1]. (10)
-
= f (x0) + (x − x0)f [x0, x1] (12)
+(x − x0)(x − x1)f [x0, x1, x2] + · · · and
+(x − x0)(x − x1) · · · (x − xn−1)f [x0, x1, · · · , xn]
Interpolating Polynomail
pn(x)
+ (x − x0)(x − x1) · · · (x − xn)f [x, x0, x1, · · · , xn] = f (x0) + (x − x0)f [x0, x1]
-
-
+(x − x0)(x − x1) · · · (x − xn−1)f [x0, x1, · · · , xn].
-
(13)
Divided
PJY Wong 91 PJY Wong
& differences
92
S
It can be shown that pn(xi) = f (xi), i = 0, 1, · · · , n,
A
therefore pn(x) given in (13) is indeed the interpolating
polynomial of f with respect to the nodes x0, x1, · · · , xn.
TR
Since pn(x) given in (13) is in terms of divided Lagrange vs Newton
differences, it is called the Newton’s divided difference
interpolating polynomial of f .
EX
Remark 2.1. Is (13) different from (4)? NO, they are the
same pn(x) expressed in different forms. After simplifying,
the final expression is the same.
Remark 2.2. Suppose we add one more point (xn+1, fn+1) Remark 2.3. The computations of the finite differences can
to the original (n + 1) points (xi, fi), i = 0, 1, · · · , n. Then, be simplified by using the following table.
in (13) the first (n + 1) terms remain the same and we just
need to add one more term
(x − x0)(x − x1) · · · (x − xn)f [x0, x1, · · · , xn+1].
This is of great advantage over Lagrange’s representation (4)
where we need to re-calculate all the l0(x), l1(x), · · · , ln(x), in
addition to ln+1(x). Thus, if we seek better approximation by
adding more points, we can still use our previous calculations
in Newton’s representation (13).
I
,
i
Summary
xi f [·] f [·, ·] f [·, ·, ·] f [·, ·, ·, ·]
3,
-
6 x To
-
-
8
pn(x)
-
-
x2 ③f [x2] f [x1, x2] f [x0, x1, x2] = f (x0) + (x − x0)f [x0, x1]
No
-B ⑰ -6
-
xy xz -
Jy-
3,
x3 ④f [x3] f [x2, x3] f [x1, x2, x3] f [x0, x1, x2, x3] +(x − x0)(x − x1) · · · (x − xn−1)f [x0, x1, · · · , xn].
2. The error inequality is given by (8). Solution The divided difference table is as follows.
3. Newton’s representation of pn(x) is better than Lagrange’s i xi f [·] f [·, ·] f [·, ·, ·] f [·, ·, ·, ·]
representation when new data are added. 0 −1 6
·1
1 0 1 0 -(
-
1) −5
Example 2.1. With a divided difference table, find the third
degree interpolating polynomial p3(x) passing through the
-
-
2
3
2
5
3
66 s 21
2
5 4
- (1) 2
* =
1
3
I
points (−1, 6), (0, 1), (2, 3), (5, 66).
↓ and order
T . 1-15
pts it order eg s -
21
y-coordinates
-
2
ald
x-coordinates di
VI
-
2
5 -
From (13), the interpolating polynomial is D. Least Squares Approximation
1. What is Least Squares Approximation?
p3(x) = f (x0) + (x − x0)f [x0, x1]
+ (x − x0)(x − x1)f [x0, x1, x2] It is not always advisable to construct interpolation
+ (x − x0)(x − x1)(x − x2)f [x0, x1, x2, x3] polynomial passing through all the points (xi, yi), i =
= 6 + (x + 1)(−5) + (x + 1)(x − 0)(2) 0, 1, · · · , n, in particular when n is large, or when the data is
+ (x + 1)(x − 0)(x − 2) 13 likely to contain error. In such a case, to find a best fit curve
through a set of points is the method of least squares.
= 6 − 5(x + 1) + 2x(x + 1) + 13 x(x + 1)(x − 2).
Here, we shall construct a polynomial pm of degree at most In other words, we plan to minimize the sum of the square of
A
A
m (where m < n), errors at the points (xi, yi), i = 0, 1, · · · , n. Hence, the term
‘least squares’. The polynomial pm is called the least-square
TR
TR
m
.
m m−1
polynomial of degree m.
pm(x) = amx + am−1x + · · · + a1 x + a0 = aj xj ,
j =0
(1)
2. Normal Equations
such that the following sum is minimized To minimize the sum of the square of errors S (see (2)) at
EX
EX
the points (xi, yi), i = 0, 1, · · · , n, we differentiate S w.r.t.
⎛ ⎞2 ∂S
n
. n
. m
. ak, k = 0, 1, · · · , m, and set ∂a = 0.
aj xji − yi⎠ .
k
S= [pm(xi) − yi]2 = ⎝ (2)
i=0 i=0 j =0
A
∂S
=2 ⎝ aj xji − yi⎠ xki = 0, 0 ≤ k ≤ m. (3) ⎧
∂ak ⎪ s0 a 0 + s1 a 1 + · · · + sm a m = v 0
i=0 j =0 ⎪
⎪
⎪
⎪
TR
Using the notations ⎨ s a + s a + ··· + s a =v
n n 1 0 2 1 m+1 m 1
. . (5)
sk = xki, vk = yixki, ⎪
⎪ ·········
⎪
⎪
i=0 i=0 ⎪
⎩ s a +s
m 0 m+1a1 + · · · + s2mam = vm.
the system (3) can be written as
These equations are called normal equations.
EX
m
.
a j sj + k − v k = 0
j =0 (4)
,m Note: s0 = n + 1 = the no. of points given
⇒ j =0 aj sj +k = vk , 0 ≤ k ≤ m
There are two things to be shown: (i) To show that the system (4) has a unique solution
A
A
(i) show that the system (4) (or equivalently (5)) has a unique This is equivalent to showing that the homogeneous system
TR
TR
solution a∗j , j = 0, 1, · · · , m;
m
.
(ii) show that this solution indeed minimizes S. aj sj +k = 0, 0 ≤ k ≤ m (6)
j =0
EX
has only the trivial solution, i.e., aj = 0, j = 0, 1, · · · , m.
There are altogether (m + 1) unknowns aj , j = 0, 1, · · · , m
in (m + 1) normal equations, solving the equations for aj ’s Suppose that NOT all aj ’s are zero, say ak ̸= 0, for some
will give the least-square polynomial pm (see (1)). k ∈ {0, 1, · · · , m}. Multiply the kth equation of (6) by ak
PJY Wong 107 PJY Wong 108
and sum all equations, to obtain Equation (7) implies that
A
⎛ ⎞ 4 n 5 m
.
m m m .
m
. . . . j +k akxki = 0 = pm(xi)
TR
TR
⎝ a k a j sj + k ⎠ = ak aj xi k=0
k=0 j =0 k=0 j =0 i=0
4 5⎛ m ⎞
n
. m
. . for all 0 ≤ i ≤ n, i.e., the polynomial pm has (n + 1) zeros.
= akxk ⎝
i aj xji ⎠ Since n > m, this is impossible! Hence, the homogeneous
i=0 k=0 j =0
system has only the trivial solution.
EX
EX
n
4 m
52
. .
= akxki =0 (7)
i=0 k=0
(ii) To show that the unique solution of system (4) Then, from (2)
A
A
⎡ ⎤2
indeed minimizes S n
!
⎣
m
! ∗ j
m
! ∗ j
S = aj xi − yi + ( a j − aj ) x i ⎦
a∗j ,
TR
TR
i=0
Let 0 ≤ j ≤ m be the unique solution of system (4). ⎧j⎛
=0 j =0
⎞2 ⎡ ⎤2
!n ⎪⎨ ! m !m
⎝ ∗ j ∗ j
= a j x i − yi ⎠ + ⎣ ( aj − aj ) x i ⎦
⎪
i=0 ⎩ j =0 j =0
⎛ ⎞⎡ ⎤⎫
!m !m ⎬
∗ j ∗ j
+ 2⎝ a j x i − yi ⎠ ⎣ ( aj − a j ) x i ⎦
⎭
j =0 j =0
⎛ ⎞2 ⎛ ⎞1 2
EX
EX
!n m
! !n !m !m
⎝ ∗ j ⎠ ⎝ ∗ j ⎠ ∗ k
≥ aj xi − yi +2 aj xi − yi ( ak − a k ) x i
i=0 j =0 i=0 j =0 k=0
⎛ ⎞2 ⎛ ⎞
n
! m
! m
! n
! m
!
⎝ ∗ j ∗ ∗ j k
= aj x i − yi ⎠ + 2 (ak − ak ) ⎝ aj x i − yi ⎠ x i .
i=0 j =0 k=0 i=0 j =0
(8)
PJY Wong 111 PJY Wong 112
Note that the second term in (8) is zero because of (3). Summary
A
Hence, (8) gives
1. Given (n + 1) points (xi, yi), i = 0, 1, · · · , n. The least-
⎛ ⎞2
TR
TR
n
. m
. square polynomial pm(x) of degree at most m (where
S≥ ⎝ a∗j xji − yi⎠ , m < n) is (1)
i=0 j =0
EX
2. The coefficients a0, a1, · · · , am are obtained by solving (m+
1) normal equations (5)
A
⎪
⎪ s0 a 0 + s1 a 1 + · · · + sm a m = v 0
⎪
⎪ line to the following points:
⎪
⎨ s a + s a + ··· + s
1 0 2 1 m+1am = v1 (−1.0, 1.000), (−0.1, 1.099), (0.2, 0.808), (1.0, 1.000).
TR
⎪
⎪ ·········
⎪
⎪
⎪
⎩ s a +s Solution Here m = 1. The least-square straight line is
m 0 m+1a1 + · · · + s2mam = vm.
Here, y = a0 + a1x.
n
. n
.
sk = xki, vk = yixki.
The normal equations are
EX
i=0 i=0
⎧
(s0 = the no. of points given) ⎨ s a +s a =v
0 0 1 1 0
⎩ s1 a 0 + s2 a 1 = v 1
A
. .
s0 = 4, s1 = xi = 0.1, s2 = x2i = 2.05, y = a0 + a1x = 0.9773 − 0.0224x.
TR
TR
. .
v0 = yi = 3.907, v1 = xiyi = 0.0517.
Example 2.2. Fit a parabola (degree 2 polynomial) to the data
Hence, the normal equations become
x 1 2 3 4 5 6 7 8 9
⎧
⎨ 4a + 0.10a = 3.9070
EX
EX
0 1 y 2 6 7 8 10 11 11 10 9
⎩ 0.1a0 + 2.05a1 = 0.0517
. .
Solution Usual method: Here, m = 2. The least-square sk = xki, vk = yixki.
A
A
parabola is i i
TR
substitute into (9) and solve for a0, a1, a2.
The normal equations are
(Do this as an exercise)
⎧
⎪
⎪
⎨ s0 a 0 + s 1 a 1 + s 2 a 2 = v 0
⎪
s1 a 0 + s2 a 1 + s3 a 2 = v 1 (9)
EX
EX
⎪
⎪
⎪
⎩ s2 a 0 + s3 a 1 + s4 a 2 = v 2
where
PJY Wong 119 PJY Wong 120
Substitution method: We use the substitution
u w uw u2 u2 w u3 u4
A
u = x − 5, w = y − 11.
−4 −9
The least-square parabola is
TR
TR
−3 −5
−2 −4
w = a 0 + a 1 u + a 2 u2 .
−1 −3
The normal equations are as in (9), where 0 −1
. .
ui k , w i ui k .
EX
EX
sk = vk = 1 0
i i
2 0
We shall calculate the coefficients sk and vk before solving
the system (9). 3 −1
4 −2
PJY Wong 121 PJY Wong 122
A
s0 = 9, s1 = 0, v0 = −25, v1 = 51,
s2 = 60, v2 = −249, s3 = 0, s4 = 708. w = a 0 + a 1 u + a 2 u2
TR
TR
Substituting these values into the normal equations (9) gives
⎧ ⇒ y − 11 = a0 + a1(x − 5) + a2(x − 5)2
⎪
⎪
⎨ 9a0 + 0 + 60a2 = −25
⎪
0 + 60a1 + 0 = 51 ⇒ y = −1 + 3.55x − 0.27x2.
⎪
⎪
⎪
EX
EX
⎩ 60a0 + 0 + 708a2 = −249
1.1. Basic Terminology • The general solution of a nth order ODE contains n
arbitrary constants. These constants can be determined
We only deal with ordinary differential equations (ODE).
if n initial/boundary conditions are given.
• An ODE is a differential equation in which an unknown
function y (dependent variable) depends on 1 independent • Initial conditions (IC) – conditions specified at the same
variable x. For example value of x. For example
• boundary conditions (BC) – conditions specified at the satisfy BC. For example
boundary points of a solution interval. For example
y ′′ − y = 0, y(0) = 5, y ′(1) = −3.
y(0) = 1, y(π) = 2; y(0) = 1, y ′(π) = 3.
y(x) = ex − x − 1, h2 h3
ϵln+1 = y ′′(xn) + y ′′′(xn) + · · ·
2! 3!
from which we obtain the values of the exact solution y(xn).
h2
Notice that the error increases as the computational process ≈ y ′′(xn) if h is sufficiently small
2!
is continued to higher values of x.
The total truncation error or global truncation error ϵgn for 2. Improved Euler Method (Heun’s Method)
n steps is
.n
Idea behind Improved Euler Method
ϵg n = ϵlk+1 (4a)
k=1 From the IVP (1): y ′ = f (x, y), y(x0) = y0, it is clear that
-
= n × O(h ) 2 6 x1 6 x1
′
y (x)dx = f (x, y(x))dx
x0 x0
xn − x 0 2 6
= × O(h ) x1
h ⇒ y(x1) − y(x0) = f (x, y(x))dx
= O(h). (4b) To x0
6 x1
Hence, Euler method is of first order. ⇒ y(x1) = y0 + f (x, y(x))dx
x0
↓
PJY Wong 141 PJY Wong 142
Let us use trapezoidal rule to approximate the integral Using Euler method gives
7 x1
x0
f (x, y(x))dx. Then,
6 x1 y(x1) ≈ y0 + hf (x0, y0).
x 1 − x0
f (x, y(x))dx ≈ [f (x0, y(x0)) + f (x1, y(x1))] -
x0 2
h It follows that
= [f (x0, y0) + f (x1, y(x1))].
2
6 x1
h
Notice that the expression above involves the yet to be f (x, y(x))dx ≈ [f (x0, y0) + f (x1, y(x1))]
x0 2 -
Y f (x y]
=
, Y (3) =
Yo
For simplicity, we denote Example 2.1. Apply the improved Euler method to Example
1.2, choosing again h = 0.2.
a = hf (xn, yn), f (32 y) y ′ = x + y,
, = y(0) = 0. %= 0 CLo
0 =
(7)
∗ Solution We have n (Xn + Yn)
b = hf (xn+1, yn +1) = hf (xn+1, yn + a)
a = hf (xn, yn) = 0.2(xn + yn),
so that the improved Euler method can be written as b = hf (xn+1, yn + a) = 0.2(xn+1 + yn + a)
↓ (Sn+ + Ynta)
1 so that
yn+1 = yn + (a + b). (8)
2 1
yn+1 = yn + (a + b), n = 0, 1, · · · .
2 -
Yntl
=
Yn + = Catb) ,
n = 0, 1
,
2 ...
x0 =
Yo = 0
-
~Y+Yeta)
, = YotE (a+b)
Y
Example 3.2. Apply the fourth order Runge-Kutta method to Solution We have f (x, y) = (y − x − 1)2 + 2. Hence,
the IVP
1)" + 2 c = hf xn + 12 h, yn + 12 b
0
= 0.2[(yn + 0.5b − xn − 0.5h − 1)2 + 2]
2Lo =
Yo = d = hf (xn + h, yn + c)
= 0.2[(yn + c − xn − h − 1)2 + 2]
~
↓
(16)
Notation of
• - which dependent variable
• - at which grid point
y•,•
at
cn
&h
except
n
ỹn+1 = ỹn + 12 (ã + b̃), (20)
ã = hf˜(xn, ỹn),
(21)
b̃ = hf˜(xn+1, ỹn + ã).
d)
where
where
a = hf (xn, yn),
-except ã = hf˜(xn, ỹn),
" #
" # b̃ = hf˜ xn + 12 h, ỹn + 12 ã ,
8 9 (27)
b = hf xn + 12 h, yn + 12 a ,
" # (25) c̃ = hf˜ xn + 12 h, ỹn + 12 b̃ ,
c = hf xn + 12 h, yn + 12 b ,
d˜ = hf˜(xn + h, ỹn + c̃).
d = hf (xn + h, yn + c).
1
yi,n+1 = yi,n + (ai + 2bi + 2ci + di) (28)
6
where
eq (14)
↓z
J
Example 4.1. Solve the system Use Runge-Kutta method (28), (29). Here, m = 2,
the 32d y1 → y, y2 → z.
dy
dx O
= 2z − 1, y(0) = 0.5; 2 times
argurement From f1(x, y, z) = 2z − 1, we get
&
-
dz ~ a1 = 0.1(2zn − 1), If (Xm Yn En)
= 2y + 1, z(0) = 3.5 , ,
, ,
1)
Solution We have f1(x, y, z) = 2z − 1, f2(x, y, z) = 2y + 1,
-
x0 = 0, y0 = 0.5, z0 = 3.5.
h step size
,
I
,
En) =
Given an mth order differential equation, it is first rewritten Then, (30) becomes a system of m first order equations
as a system of m first order differential equations by ⎧
⎪
⎪ y1′ = y2
appropriate substitutions. The resultant system is then solved ⎪
⎪
⎪
⎪
⎪
⎪ y2′ = y3
by any of the methods discussed in Sections 4.2–4.4. ⎪
⎪
⎪
⎨ ······
Consider the mth order IVP ⎪ ′ 1) 1)
y(m
2 +
⎪ ym− 1 = ym
-
y(m
-
⎧ ⎪
⎪ Ym +
= =
You =
=
= =
, , . . . .
⎪
⎪ f (x =
Y, Y Ym)
S
⎩ y(x0) = K1, y ′(x0) = K2, · · · , y (m−1)(x0) = Km. ⎩ y1(x0) = K1, y2(x0) = K2, , · · · , ym(x0) = Km. ,
,
. . .,
(30) (31)
PJY Wong 176 PJY Wong 177
We can solve (31) by the methods discussed in previous Solution First convert the second order ODE into a system of
sections. 2 first order ODE. Let let Y Y Y =
,
=
Y = Y Yz
Example 4.2. Consider the IVP
=
- y1 = y, y2 = y ′ .
Yi =
y" =
-
y = -
Y,
′′ ′
y = −y, y(0) = 0, y (0) = 1. (32) Then, (32) is equivalent to Y 10) =
,
0
,
"(0) =
1
11
- -
-
Y Ll
⎧ 1 o
Ye , o
Using -
h = 0.1, find an approximate value of y(0.2) to 3 ⎪
⎪ ′
- - ⎨ y 1 = y2
⎪
decimal places. y2′ = −y1 (33)
-
⎪
⎪
⎪
⎩ y1(0) = 0, y2(0) = 1.
E
(
x0 = 0, y1,0 = 0, y2,0 = 1. 1 0.1 0.1 .
Y
1
=
Y 0thY2
,
,
0
1 Ye 1
=
Ye 0-h Y,
,
0
,
Method 1 Solve (33) by the Euler method (17). We have 2 0.2 0.2 Y , z
=
Y ,,, thYo,
0.99 Ye ,
2
=
Ye ,
1
he Y
, ,
= y1,n + hy2,n, Method 2 Solve (33) by the Runge-Kuttar method (28), (29).
Here,
′
yn +1 = y2,n+1 = y2,n + hf2(xn, y1,n, y2,n) yn+1 = y1,n+1 = y1,n + 16 (a1 + 2b1 + 2c1 + d1),
= y2,n − hy1,n. 4 ′
yn 1
+1 = y2,n+1 = y2,n + 6 (a2 + 2b2 + 2c2 + d2),
same
where n xn yn = y1,n ′
yn = y2,n a1 a2
0 0 0 1 0.1 0
f1(x, y1, y2) = y2, f2(x, y1, y2) = −y1, 1 0.1 0.09985 0.9950 0.0995 -0.009985
a1 = 0.1y2,n, a2 = −0.1y1,n, 2 0.2 0.1987
b1 = 0.1(y2,n + 0.5a2), b2 = −0.1(y1,n + 0.5a1), b1 b2 c1 c2 d1 d2
c1 = 0.1(y2,n + 0.5b2), c2 = −0.1(y1,n + 0.5b1), 0.1 -0.005 0.0998 -0.005 0.0995 -0.00998
d1 = 0.1(y2,n + c2), d2 = −0.1(y1,n + c1) 0.09900 -0.01496 0.09875 -0.01494 0.09801 -0.01986
Hence, we have y(0.2) ≈ 0.199.
The table of computation is shown below.
Do this as an exercise.