Numerical Analysis With - Matlab
Numerical Analysis With - Matlab
with MATLAB
Lecture Notes
Mohammad Sabawi
Department of Mathematics
College of Education for Women
Tikrit University
Email: [email protected]
March 2019
1
List of Tables
2
Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1 Introduction 6
1.1 Numerical Analysis: An Introduction . . . . . . . . . . . . . . . . . 6
1.2 Numbers Representation in Computer . . . . . . . . . . . . . . . . 7
1.2.1 Floating-Point Numbers . . . . . . . . . . . . . . . . . . . . 8
1.3 Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.3.1 Error Analysis . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.3.2 Sources of Error in Numerical Computations . . . . . . . . 9
1.3.3 Absolute and Relative Errors . . . . . . . . . . . . . . . . . 10
1.3.4 Roundoff and Truncation Errors . . . . . . . . . . . . . . . 11
1.4 Stable and Unstable Computations: Conditioning . . . . . . . . . . 11
1.5 Convergence and Order of Approximation . . . . . . . . . . . . . . 13
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3
3.3.3 Gaussian Elimination Method . . . . . . . . . . . . . . . . . 46
3.3.4 Gauss-Jordan Elimination Method . . . . . . . . . . . . . . 51
3.4 LU and Cholesky Factorisations . . . . . . . . . . . . . . . . . . . 54
3.5 Iterative Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
3.5.1 Jacobi Method . . . . . . . . . . . . . . . . . . . . . . . . . 59
3.5.2 Gauss-Siedel Method . . . . . . . . . . . . . . . . . . . . . . 61
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
4
Preface
The aim of these class notes is to cover the necessary materials in a stan-
dard numerical analysis course and it s not intended to add to the plethora
of Numerical Analysis texts. We tried our best to write these notes in con-
cise, clear and accessible way, to make them more attractive to the readers.
These lecture notes cover the basic and fundamental concepts and principles
in numerical analysis and it is not a comprehensive introduction to numer-
ical analysis. We emphasise in these notes on the mathematical principles
via explaining them by the aid of numerical software MATLAB. The pre-
requisite material for this course are a course in Calculus, Linear Algebra
and Differential Equations. A basic knowledge in MATLAB is helpful but
it is not necessary. There is a glut of numerical software nowadays, among
these we chose to use MATLAB because of its wide capabilities in scientific
computing.These notes consist of ten chapters and each chapter ends with a
set of exercises address the topics covered in each chapter.
Introduction
5. Numerical Differentiation.
6. Numerical Integration.
6
CHAPTER 1. INTRODUCTION
7. Numerical Optimisation.
k = m × 10n ,
where m is any real number and the exponent n is an integer. This notation is
called the scientific notation or scientific form and sometimes referred
to as standard form.
1. 0.00000834.
2. 25.45879.
3. 3400000.
4. 33.
5. 2, 300, 000, 000.
Solution:
1. 0.00000834 = 8.34 × 10−6 .
2. 25.45879 = 2.545879 × 101 .
3. 3400000 = 3.4 × 106 .
4. 33 = 3.3 × 101 .
5. 2.3 × 109 .
b = ±0.d1 d2 d3 · · · dk × 10n , 1 ≤ d1 ≤ 9, 0 ≤ di ≤ 9,
for each i = 2, · · · , k. These numbers are called k-digit decimal ma-
chine numbers.
1.3 Errors
Occurrence of error is unavoidable in the field of scientific computing. In-
stead, numerical analysts try to investigate the possible and best ways to
minimise the error. The study of the error and how to estimate and min-
imise it are the fundamental issues in error analysis.
e = x − x∗ .
ê = |x − x∗ |.
ê |x − x∗ |
ẽ = = , x 6= 0.
|x| |x|
Example 2. Let x = 3.141592653589793 is the value of the constant ratio π
correct to 15 decimal places and x∗ = 3.14159265 be an approximation of x.
Compute the following quantities:
a. The error.
Solution:
a. The error
1. x1 = 1.34579. 4. x4 = 3.34379.
2. x2 = 1.34679.
3. x3 = 1.34479. 5. x5 = 2.34579.
Solution:
|xf 0 (x)|
Cond(f (x)) = , f (x) 6= 0.
|f (x)|
Note: Condition number of a function f at x in its domain sometimes
denoted by Cf (x).
cond(A) = kAkkA−1 k,
where
kAxk
kAk = max ,
x6=0 kxk
and x is a m × 1 column vector.
√
Example 4. Find the condition number of the function f (x) = x.
Solution:
√ 1
f (x) = x =⇒ f 0 (x) = √ , x 6= 0,
2 x
implies that
|xf 0 (x)| | 2√x x | 1
cond(f (x)) = = √ = .
|f (x)| | x| 2
This indicates that the small changes in the input date lead to changes in the
output data of half size the changes in the input data.
Example 5. Let
1 −1 1
A = 1 0.5 3 ,
0.1 1 0.3
the inverse of A can be computed by using MATLAB command inv(A) to
obtain
4.7500 −2.1667 5.8333
A−1 = 0.5000 −0.3333 1.6667 .
−3.2500 1.8333 −4.1667
Also, the condition number of A and its inverse can be computed using MAT-
LAB commands cond(A) and cond(inv(A)) to have cond(A) = 37.8704
and cond(A−1 ) = 37.8704. We notice that the matrix A and its inverse have
the same condition number.
Remark 2. Note that the accuracy and precision are different and they are
not related. The problem maybe very accurate but imprecise and vice versa.
Note that the convergence gets more rapid as q gets larger and larger.
Example 6. Consider the sequence { n1 }, where n is a positive integer. Ob-
serve that n1 → 0 as n → ∞, it follows that
1
lim = 0.
n→∞ n
Exercises
1. 23.123. 4. 39776444.
3. 0.000001573. 6. −23000000.
Exercise 2. Evaluate error, absolute error and relative error of the following
values and their approximations:
2. y = 0.00012887765, y ∗ = 0.00012897766.
3. z = 9776.96544, z ∗ = 9775.66544.
1. 1.98876. 3. 8.98879.
2. 33.87654. 4. 2.88778.
1. f (x) = cos(x).
Numerical Solutions of
Nonlinear Equations
2.1 Introduction
Nonlinear algebraic equations are wide spread in science and engineering
and therefore their solutions are important scientific applications. There are
a glut of numerical methods for solving these equations, and in these lecture
notes, we study the most commonly used ones such as bisection, secant and
Newton methods. Locating positions of roots of nonlinear equation is a topic
of great importance in numerical mathematical analysis. The problem under
consideration maybe has a root or has no root at all.
16
CHAPTER 2. NUMERICAL SOLUTIONS OF NONLINEAR
EQUATIONS
Theorem 5 (Simple Zero Theorem). Assume that f ∈ C 1 [a, b]. Then, f has
a simple zero at r ∈ (a, b) if and only if f (r) = 0 and f 0 (r) 6= 0.
Solution:
Hence,
a1 +b1
To begin, set a1 = a and b1 = b and let c1 = 2
be the midpoint of the
interval [a1 , b1 ] = [a, b].
(i). If f (a1 )f (c1 ) < 0 then r lies in [a1 , c1 ], and set a2 = a1 and b2 = c1 ,
i.e. [a2 , b2 ] = [a1 , c1 ].
(ii). If f (c1 )f (b1 ) < 0 then r lies in [c1 , b1 ], and set a2 = c1 and b2 = b1 ,
i.e. [a2 , b2 ] = [c1 , b1 ].
a2 +b2
(iii). Compute c2 = 2
the midpoint of the interval [a2 , b2 ].
(iv). Then, we proceed in this way until we reach the nth interval [an , bn ]
and then compute its midpoint cn = an +b2
n
.
• Finally, construct the interval [an+1 , bn+1 ] which brackets the root and
its midpoint cn+1 = an+ +b
2
n+1
will be an approximation to the root r.
Remark 3. (a). The interval [an+1 , bn+1 ] is wide as half as the interval [an , bn ]
i.e. the width of each interval is as half as the width of the previ-
ous interval. Let {`n } be a sequence of widths of intervals [an , bn ], i.e.
`n = bn −a
2
n
, n = 1, 2, · · · . Hence limn→∞ `n = 0, where is the preas-
signed value of the error (tolerance) i.e.
|rn+1 − rn | ≤ , n = 0, 1, · · · ,
a0 ≤ a1 ≤ a2 ≤ · · · ≤ an ≤ · · · ≤ r ≤ · · · ≤ b n ≤ · · · ≤ b 2 ≤ b 1 ≤ b 0 .
Theorem 6 (Bisection Method Theorem). Let f ∈ C[a, b] such that f (a)f (b) <
0 and that there exists a number r ∈ [a, b] such that f (r) = 0, and {cn } a
sequence of the midpoints of intervals [an , bn ] constructed by the bisection
method, then the error in approximating the root r in the nth step is:
b−a
|en | = |r − cn | ≤ , n = 0, 1, · · · .
2n+1
Hence, the sequence {cn } is convergent and its limit is the root r, i.e.
lim cn = r.
n→∞
Proof. For Proof see the References or have a look in any standard numerical
analysis text.
Remark 4. Remark
|b0 − a0 |
|bn − an | = .
2n
(b) Evaluate the number of computations N required to ensure that the error
is less than the preassigned value (error bound) = 0.001.
Solution:
(a) We start with initial interval [a0, b0] = [0.5, 1.5] and compute f (0.5) =
−0.76028723 and f (1.5) = 0.49624248. We notice that f (a0 ) and f (b0 ) have
opposite signs and hence, there is a root in the interval [0.5, 1.5]. Compute
the midpoint c0 = a0 +b
2
0
= 0.5+1.5
2
= 1 and f (1) = −0.15852902. The function
changes sign on [c0 , b0 ] = [1, 1.5], so, we set [a1 , b1 ] = [c0 , b0 ] = [1, 1.5], and
compute the midpoint c1 = a1 +b 2
1
= 1+1.5
2
= 1.25 and f (1.25) = 0.18623077.
Hence, the root lies in the interval [a1 , c1 ] = [1, 1.25]. Set [a2 , b2 ] = [a1 , c1 ] =
[1, 1.25] and continue until we compute c10 = 1.11376953125. The details are
explained in Table 2.1.
(b)
f (b) − f (a)
m= .
b−a
Now, compute the slope of line SL between the two points (c, f (c)) = (c, 0)
and (b, f (b)):
f (b) − f (c) f (b) − 0 f (b)
m= = = .
b−c b−c b−c
Example 11. Use Newton’s method to find the positive root accurate to
within 10−5 for f (x) = 3x − ex = 0. Start with the initial guess r0 = 1.5.
Next, we compute r2 ,
n rn f (rn ) f 0 (rn )
0 1.50000000 0.01831093 −1.48168907
1 1.51235815 −0.00034364 −1.53741808
2 1.51213463 −0.00000011 −1.53640399
3 1.51213455 −0.00000000 −1.53640365
4 1.51213455 0 −1.53640365
√
Note that this equation has two roots x = ± B. Now, find the derivative
of f , f 0 (x) = 2x and use the Newton’s fixed point formula
B
f (x) x2 − B x2 + B x+ x
x = g(x) = x − 0 =x− = = .
f (x) 2x 2x 2
n rn
0 1
1 2
2 1.75
3 1.732142857142857
4 1.732050810014727
5 1.732050807568877
6 1.732050807568877
f1 (x1 , x2 , · · · , xN ) = 0
f2 (x1 , x2 , · · · , xN ) = 0
..
.
fN (x1 , x2 , · · · , xN ) = 0.
Using vector notation, this system can be written in this concise form
F (X) = 0,
where
F = [f1 , f2 , · · · , fN ]T
X = [x1 , x2 , · · · , xN ]T .
f1 (x1 , x2 , x3 ) = 0
f2 (x1 , x2 , x3 ) = 0
f3 (x1 , x2 , x3 ) = 0.
0
where F (X (0) ) is the Jacobian matrix at the initial guess X (0) = (x01 , x02 , x03 ),
∂f1 ∂f1 ∂f1
∂x1 ∂x2 ∂x3
0 ∂f2 ∂f2 ∂f2
F (X (0) ) = ∂x1 ∂x2 ∂x3 ,
∂f3 ∂f3 ∂f3
∂x1 ∂x2 ∂x3
The next iteration after correction X (1) = X (0) + H (0) is closer to the root
than X (0) . Hence, Newton’s formula for the first iteration is
0
X (1) = X (0) − [F (X (0) )]−1 F (X (0) ).
Consequently, the general form of Newton’s method for solving the non-
linear system is:
0
X (k+1) = X (k) − [F (X (k) )]−1 F (X (k) ), k = 0, 1, ... .
x1 + x2 + x3 = 2
x21 + x22 + x23 = 2
ex1 + x1 x2 − x1 x3 = 1.
∂f1 ∂f1 ∂f1
1 1 1
0 ∂x
∂f
1 ∂x2
∂f2
∂x3
∂f2
F (X) = ∂x21 ∂x2 ∂x3 = 2x1 2x2 2x3 .
∂f3
∂x1
∂f3
∂x2
∂f3
∂x3
ex1 + x2 − x3 x1 −x1
∂f1 ∂f1 ∂f1
∂x1 ∂x2 ∂x3 1 1 1 1 1 1
0
F (X (0) ) =
∂f2
∂x1
∂f2
∂x2
∂f2
∂x3 = 2x01 2x02 2x03 = 2 0 0 .
(0) (0) (0) (0) (0)
∂f3
∂x1
∂f3
∂x2
∂f3
∂x3
ex1 + x2 − x3 x1 −x1 e1 1 −1
we get
0
1 1 1 h1 −1
2 0 0 h02 = − −1 ,
e1 1 −1 h03 e1 − 1
implies that
0.5000
H (0) = −1.2887 .
1.7887
Hence,
1 0.5000 1.5000
X (1) = X (0) + H (0) = 0 + −1.2887 = −1.2887 .
0 1.7887 1.7887
Now, compute the Jacobian matrix for X (1) .
1 1 1 1.0000 1.0000 1.0000
0
F (X (1) ) = 2x11 2x12 2x13 = 3.0000 −2.5774 3.5774 .
(1) (1) (1) (1) (1)
e x1 + x2 − x3 x1 −x1 1.4043 1.5000 −1.5000
f1 (x1 , x2 , x3 ) 0
F (X (1) ) = f2 (x1 , x2 , x3 ) = 5.1102 .
f3 (x1 , x2 , x3 ) −1.1344
1
1.0000 1.0000 1.0000 h1 0
3.0000 −2.5774 3.5774 h12 = − 5.1102 ,
1.4043 1.5000 −1.5000 h13 −1.1344
implies
−0.5172
H (1) = 0.8788 .
−0.3616
1.5000 −0.5172 0.9828
X (2) = X (1) + H (1) = −1.2887 + 0.8788 = −0.4099 .
1.7887 −0.3616 1.4271
Continuing as before, until we reach the required results.
The slope of the secant line relating these three points (r0 , f (r0 )), (r1 , f (r1 ))
and (r2 , f (r2 )) is:
Example 14. Find the root of equation x − cos(x) = 0 using the secant
method and the two initial guesses r0 = 0.5 and r1 = 0.6.
f (r2 )(r2 − r1 )
r3 = r2 − = 0.738791967963291, f (r3 ) = −0.000490613128583.
f (r2 ) − f (r1 )
n rn f (rn )
0 0.500000000000000 −0.377582560000000
1 0.600000000000000 −0.225335610000000
2 0.748006655882730 0.014960500949714
3 0.738791967963291 −0.000490613128583
4 0.739084558312839 −0.000000962163319
5 0.739085133252381 0.000000000062293
6 0.739085133215161 0
7 0.739085133215161 0
Example 15. Find the fixed points of the function g(x) = 2 − x2 and verify
that they are the solutions to the equation f (x) = x − g(x) = 0.
Solution : The fixed points of g are the points satisfying the fixed point
equation x = g(x), so intersect the graph of y = g(x) with the graph of the
straight line y = x
x = g(x) = 2 − x2 ,
which implies that
1. If g(x) ∈ [a, b] for all x ∈ [a, b], then g has a at least one fixed point r
in [a, b].
2. If also, g 0 (x) existed and defined on (a, b) and there exists a positive
constant K < 1 such that |g 0 (x)| ≤ K < 1, for all x ∈ (a, b), then g
has a unique fixed point r in [a, b].
1. g(x) ∈ [a, b] for all x ∈ [a, b], then g has a fixed point r in [a, b].
2. g 0 (x) existed and defined over (a, b) and there exists a positive constant
K < 1 such that |g 0 (x)| ≤ K < 1, for all x ∈ (a, b), then g has a unique
fixed point r in [a, b].
3. If g satisfies the conditions (1) and (2) , then for any number r0 in [a, b]
the sequence {rn }∞ n=0 of iterations generated by fixed point iteration
rn+1 = g(rn ), n = 0, 1, · · · , converges to the unique fixed point r in
[a, b].
|r − rn | ≤ K n max |r − r0 |,
and
Kn
|r − rn | ≤ max |r1 − r0 |, for all n ≥ 1.
1−K
Example 16. Use the fixed point method to find the zero of the function
f (x) = x3 − 3x2 + 2 in [0, 2], start with r0 = 1.5.
For example, to obtain g1 (x) just add x to both sides of the equation
−f (x) = 0 and this is the simplest way to write the problem as a fixed point
form
3 2 2 3 x3 + 2
2
x − 3x + 2 = 0, so 3x = x + 2, and x = ,
3
implies that
x3 + 2 1/2 x3 + 2 1/2 x3 + 2 1/2
x=± , so g2 (x) = , and g3 (x) = − .
3 3 3
Note that it is important to check that the fixed point of each derived function
g is a solution to the problem f (x) = 0. For example, because the solution is
positive and lies between 0 and 2, so we choose the positive function g2 (x),
since the negative function g3 (x) is not a choice here. The results are outlined
in Tables 2.6 and 2.7 below.
n g6 (x) g7 (x)
0 1.5 1.5
1 1.68098770 1.77951304
2 1.86406700 2.05295790
3 2.03474597 2.27698696
4 2.18422416 2.43979654
5 2.30913228 2.54944095
6 2.40992853 2.61989258
7 2.48919424 2.66388582
8 2.55035309 2.69088731
9 2.59688496 2.70728880
10 2.63192563 2.71718971
11 2.65811433 2.72314423
12 2.67757909 2.72671736
13 2.69198765 2.72885862
14 2.70262171 2.73014079
15 2.71045296 2.73090817
16 2.71621092 2.73136731
17 2.72043954 2.73164198
18 2.72354236 2.73180628
19 2.72581768 2.73190456
20 2.72748542 2.73196334
21 2.72870741 2.73199849
f (x)
x = g(x) = x − ,
f 0 (x)
Note that the zero function P (x) = 0 is a polynomial but has no de-
gree.There are several techniques for finding zeros of polynomials in the liter-
ature such as Müller method, Laguerre’s method, Bairstow method,
Brent’s method and Jenkins-Traub method, and these methods are
beyond the scope of this lecture notes and interested readers can see the
references.
Exercises
Exercise 10. Solve Example 10 using the bisection method and compare the
solution with false position method’s solution of the same problem.
Exercise 11. Repeat solving Example 9 using the false position method and
compare the results with the solution of the bisection method for the same
problem.
Exercise 13. Use the secant method to find the solution accurate to within
10−5 to the following problem x sin(x) − 1 = 0, 0 ≤ x ≤ 2.
Exercise 14. Use the fixed point method to locate the root of f (x) = x−e−x =
0, start with an initial guess of x = 0.1.
Exercise 15. Let f (x) = x2 − 5 and r0 = 1.5. Use bisection, false position,
secant, fixed point, Newton’s and modified
√ Newton’s methods to find r7 the
approximation to the positive root r = 5.
Exercise 16. Use modified (accelerated) Newton’s method to solve the equa-
tion x2 − 3x − 1 = 0 in the interval [−1, 1].
3.1 Introduction
Many phenomena and relationships in nature and real life applications are
linear, meaning that results and their causes are proportional to each other.
Solving linear algebraic equations is a topic of great importance in numerical
analysis and other scientific disciplines such as engineering and physics. So-
lutions to Many problems reduced to solve a system of linear equations. For
example, in finite element analysis a solution of a partial differential equation
is reduced to solve a system of linear equations.
39
CHAPTER 3. SOLVING SYSTEMS OF LINEAR EQUATIONS
Remark 5. Note that when n = 1 both norms reduce to the absolute value
function of real numbers.
Example 18. Determine the l1 norm, l2 norm and l∞ norm of the vector
x = (1, 0, −1, 2, 3)0 .
Solution: The required norms of vector x = (1, 0, −1, 2, 3)0 in R5 are:
5
X
kxk1 = |xi | = |x1 | + |x2 | + |x3 | + |x4 | + |x5 | = |1| + |0| + | − 1| + |2| + |3| = 7,
i=1
5
X 1/2 1/2
kxk2 = x2i = x21 + x22 + x23 + x24 + x25
i=1
1/2 1/2
= (1)2 + (0)2 + (−1)2 + (2)2 + (3)2 = 15 ,
and
or, alternatively
kAk = max kAxk.
kxk=1
where σi are the eigenvalues of AT A, which are called the singular values of
A and the largest eigenvalue in absolute value (|σmax |) is called the spectral
radius of A.
Definition 29 (l∞ Matrix Norm). Let A is a n×n matrix and x = (x1 , x2 , · · · , xn )0 .
Then the l∞ (maximum)matrix norm is defined by
n
X
kAk∞ = max kAxk∞ = max |aij |.
kxk∞ =1 1≤i≤n
j=1
for i = 3, we get
3
X
|a3j | = |a31 | + |a32 | + |a33 | = | − 1| + |6| + | − 4| = 11.
j=1
Consequently,
3
X
kAk∞ = max |aij | = max{4, 8, 11} = 11.
1≤i≤3
j=1
a11 a12 · · · a1n x1 b1
a21 a12 · · · a1n x2 b2
.. .. = .. ,
.. .. ..
. . . . . .
an1 a12 · · · a1n xn bn
bn
xn = .
ann
(2) Substitute xn in the next-to-last ((n − 1)th) equation and solve it for
xn−1 :
bn−1 − an−1n xn
xn−1 = .
an−1n−1
(3) Now, xn and xn−1 are known and can be used to find xn−2 :
br − nj=r+1 arj xj
P
xr = , r = n − 1, n − 2, · · · 1.
arr
Example 20. Solve the following linear system using back substitution method
3x1 + 2x2 − x3 + x4 = 10
x2 − x3 + 2x4 =9
3x3 − x4 =1
3x4 =6
x2 = 9 + x3 − 2x4 = 9 + 1 − 4 = 6.
a11 x1 = b1
a21 x1 + a22 x2 = b2
a31 x1 + a32 x2 + a33 x3 = b3
..
.
an−11 x1 + an−12 x2 + an−13 x3 + · · · + an−1n−1 xn−1 = bn−1
an1 x1 + an2 x2 + an3 x3 + · · · + ann−1 xn−1 + ann xn = bn .
(2) Substitute x1 in the second equation (2nd) equation and solve it for x2 :
b2 − a21 x1
x2 = .
a22
x1 + 2x2 − x3 + 4x4 = 12
2x1 + x2 + x3 + x4 = 10
−3x1 − x2 + 4x3 + x4 = 2
x1 + x2 − x3 + 3x4 = 6
.
Solution: The augmented matrix is
1 2 −1 4 12
2 1 1 1 10
.
−3 −1 4 1 2
1 1 −1 3 6
The first row is the pivotal row, so the pivotal element is a11 = 1 and
is used to eliminate the first column below the diagonal. We will denote
by mr1 to the multiples of the row 1 subtracted from row r for r = 2, 3, 4.
Multiplying the first row by m21 = −2 and add it to the second row to have
1 2 −1 4 12
0 −3 3 −7 −14
.
−3 −1 4 1 2
1 1 −1 3 6
Now, multiply the first row by m31 = 3 and add it to the third row to
obtain
1 2 −1 4 12
0 −3 3 −7 −14
.
0 5 1 13 38
1 1 −1 3 6
Multiplying the first row by m41 = −1 ) and adding it to the fourth row
yields
1 2 −1 4 12
0
−3 3 −7 −14
.
0 5 1 13 38
0 −1 0 −1 −6
Now, the pivotal row is the second row and the pivotal element is a22 =
−3. Multiply the second row by m32 = 53 to have
1 2 −1 4 12
0 −3 3 −7 −14
.
0 0 6 4/3 44/3
0 −1 0 −1 −6
−1
Multiply the the second row by m42 = 3
and add it to the fourth row
to obtain
1 2 −1 4 12
0
−3 3 −7 −14 .
0 0 6 4/3 44/3
0 0 −1 4/3 −4/3
Now, the pivotal row is the third row and the third element is a33 = 6.
Finally, multiply the third row by m43 = 61 to the fourth row to have
1 2 −1 4 12
0 −3 3 −7 −14
.
0 0 6 4/3 44/3
0 0 0 14/9 10/9
Now, note that the coefficient matrix is transformed into an upper trian-
gular matrix and can be solved by backward substitution method. Firstly,
we from the last row we compute
10/9 5
x4 = = .
14/9 7
Use the third row to solve for x3
44/3 − 4/3(5/7) 288/21 16
x3 = = = .
6 6 7
Now, solve the second equation for x2
−14 − 3x3 + 7x4 −14 − 3(16/7) + 7(5/7) 111 37
x2 = = = = .
−3 −3 21 7
Finally, solve the first equation for x1
6
x1 = 12 − 2x2 + x3 − 4x4 = 12 − 2(37/7) + 16/7 − 4(5/7) = .
7
Example 23. Solve the following linear system using Gauss elimination
method by using forward substitution technique
x1 + 2x2 + x3 + 4x4 = 13
2x1 + 0x2 + 4x3 + 3x4 = 28
4x1 + 2x2 + 2x3 + x4 = 20
−3x1 + x2 + 3x3 + 2x4 = 6
.
Solution: We start our solution strategy by transforming this square system
to equivalent lower-triangular system and then solve it by using forward
substitution method. Write the system in augmented matrix form
1 2 1 4 13
2 0 4 3 28
4 2 2 1 20 .
−3 1 3 2 6
Note that now the pivotal row is the fourth row and the pivotal element
is a44 = 2. Multiply the fourth row by the multiple m14 = −2 and it to the
first row to have
7 0 −5 0 1
2 0 4 3 28
4 2 2 1 20 .
−3 1 3 2 6
Multiply the fourth row by m24 = −3
2
and add it to the second row to
obtain
7 0 −5 0 1
13/2 −3/2 −1/2 0 19
.
4 2 2 1 20
−3 1 3 2 6
Now multiply the fourth equation by m34 = −1
2
and add it to the third
row to have
7 0 −5 0 1
13/2 −3/2 −1/2 0 19
.
11/2 3/2 1/2 0 17
−3 1 3 2 6
The pivotal row now is the third row and the pivotal element is a33 = 1/2.
Add the third row to the second row (i.e. multiply it by m23 = 1) to get
7 0 −5 0 1
12 0 0 0 36
11/2 3/2 1/2 0 17 .
−3 1 3 2 6
Now
12 0 0 0 36
11/2 3/2 1/2 0 17
.
7 0 −5 0 1
−3 1 3 2 6
The pivotal row (third row) is used to eliminate elements in the second
1
row and the pivotal element is a33 = −5. Multiply the third row by m23 = 10
to have
12 0 0 0 36
31/5 3/2 0 0 171/10
.
7 0 −5 0 1
−3 1 3 2 6
Now, use forward substitution to solve the lower-triangular matrix. solve
the first equation for x1
36
x1 = = 3.
12
Use the equation to find x2
171/10 − (31/5)3
x2 = = −1.
3/2
Now, solve the third equation for x3
1 − 7(3)
x3 = = 4.
−5
Finally, solve the fourth equation for x4
Example 24. Solve the following linear system using Gauss-Jordan elimi-
nation method
3 4 3 10
1 5 −1 7 .
6 3 7 15
The pivot row is the first row and the pivot element is a11 = 3. Multiply
it by m11 = 1/3 to get
1 4/3 1 10/3
1 5 −1 7 .
6 3 7 15
Subtract the second equation from the first (i.e. multiply it by m21 = −1)
and multiply the fist equation by m31 = −6 and add it to the third equation
to have
1 4/3 1 10/3
0 −11/3 2 −11/3 .
0 −5 1 −5
Now, the pivot row is the second row and the pivot element a22 = −11/3.
Multiply it by m22 = −3/11 to have
1 4/3 1 10/3
0 1 −6/11 1 .
0 −5 1 −5
Multiply the first and third rows by m12 = −4/3 and m32 = 5 to obtain
1 0 19/11 2
0 1 −6/11 1 .
0 0 −19/11 0
The pivot element now is third row and the pivot element is a33 = −19/11.
Multiply it by m33 = −11/19 to get
1 0 19/11 2
0 1 −6/11 1 .
0 0 1 0
Finally, multiply the third row by m13 = −19/11 and m23 = 6/11 and
add it to the first and second rows to have
1 0 0 2
0 1 0 1 .
0 0 1 0
Example 25. Solve the following linear system using Gauss-Jordan elimi-
nation method
−2x1 + x2 + 5x3 = 15
4x1 − 8x2 + x3 = −21
4x1 − x2 + x3 = 7
−4 −24 77/2
x1 = = 2, x2 = = 4 and x3 = = 3.
−2 −6 77/6
A = LU.
or in matrix form
a11 a12 a13 1 0 0 u11 u12 u13
a21 a22 a23 = l21 1 0 0 u22 u23 .
a31 a32 a33 l31 l32 1 0 0 u33
Note that since A is nonsingular matrix this implies that urr 6= 0 for all
r and this is called Doolittle factorisation.
Example 26. Solve the following linear system using LU (Doolittle) decom-
position
2x1 − 3x2 + x3 = 2
x1 + x2 − x3 = −1
−x1 + x2 − x3 = 0
Find the values of the entries of matrices L and U . From the first column
we have
2 = 1u11 =⇒ u11 = 2,
and
1 = 1u13 =⇒ u13 = 1,
and
−1 = l31 u13 + l32 u23 + 1u33 = −0.5(1) + (−0.2)(−1.5) + u33 =⇒ u33 = −0.8.
2 −3 1 1 0 0 2 −3 1
A= 1 1 −1 = 0.5 1 0 0 2.5 −1.5 = LU.
−1 1 −1 −0.5 −0.2 1 0 0 −0.8
1 0 0 2
0.5 1 0 −1 .
−0.5 −0.2 1 0
2 −3 1 x1 2
0 2.5 −1.5 x2 = −2 .
0 0 −0.8 x3 0.6
(b) Then, use the decomposition from part (a) to solve the linear system
2x1 + x2 + 0x3 = −1
x1 + 2x2 + x3 = −4
0x1 + x2 + 2x3 = 2
2
√
2 = l11 =⇒ l11 = 2,
√ 1
1 = l21 l11 + l22 (0) =⇒ 1 = l21 2 =⇒ l21 = √ ,
2
√
0 = l31 l11 + l32 (0) + l33 (0) =⇒ 0 = l31 2 =⇒ l31 = 0.
r √
3 2
1 = l31 l21 + l32 l22 + l33 (0) =⇒ 1 = 0 + l32 =⇒ l32 = √ .
2 3
Finally, from the third column we get
r
2 2 2 2 2 4 2
2 = l31 + l32 + l33 =⇒ 2 = 0 + + l33 =⇒ l33 = =√ .
3 3 3
n
X aij xjk−1 bi
xki = − + , j 6= i, aii 6= 0, for i = 1, · · · , n, k = 1, · · · , n.
j=1
aii aii
2x1 + x2 + x3 = 0
x1 + 3x2 + x3 = 0.5
x1 + x2 + 2.5x3 = 0
−xk2 − xk3
xk+1
1 = ,
2
59 Mohammad Sabawi/Numerical Analysis
CHAPTER 3. SOLVING SYSTEMS OF LINEAR EQUATIONS
Let us start with initial guess P0 = (x01 , x02 , x03 ) = (0, 0.1, −0.1). Substi-
tuting these values in the right-hand side of each equation in above to find
the new iterations
Now, the new point P1 = (x11 , x12 , x13 ) = (0, 0.2, −0.04) is used in the
Jacobi iterative form to find the next approximation P2
The new point P2 = (x21 , x22 , x23 ) = (−0.08, 0.18, −0.08) is closer to the
solution than P0 and P1 and is used to find P3
i−1 n
X aij xkj X aij xk−1
j
b
i
xki = − + − + , j 6= i, aii 6= 0,
j=1
aii j=i+1
aii aii
for i = 1, · · · , n, and k = 1, · · · , n.
2x1 − 4x2 + x3 = −1
x1 + x2 + 6x3 = 1
3x1 + 3x2 + 5x3 = 4
.
Solution: Rearrange the system in above such that the coefficient matrix is
strictly diagonally dominant
.
These equations can be written in the form
4 − 3x2 − 5x3
x1 = ,
3
−1 − 2x1 − x3 1 + 2x1 + x3
x2 = = ,
−4 4
1 − x1 − x 2
x3 = .
6
1 + 2xn+1
1 + xn3
xn+1
2 = ,
4
1 − xn+1
1 − xn+1
2
xn+1
3 = .
6
We start with initial guess P0 = (x01 , x02 , x03 ) = (1, 0.1, −1). Substitute
x02 = 0.1 and x03 = −1 in the first equation and have
Now, we have the now point P1 = (x11 , x12 , x13 ) = (2.9, 1.45, −0.558333333333333)
is used to find the next approximation P2 .
This iteration process generates a sequence of points {Pn } = {(xn1 , xn2 , xn3 )}
that converges to the solution (x1 , x2 , x3 ) = (32/39, 25/39, −1/13) =
(0.820512820512820, 0.641025641025641, −0.076923076923077). The results
are given in the Table 3.2.
Exercises
Exercise 17. Solve Example 22 Using Gauss elimination with forward sub-
stitution method. Compare the solution with solution of the same example.
Exercise 18. Solve Example 23 Using Gauss elimination with backward sub-
stitution method. Compare the solution with solution of the same example.
Exercise 19. Repeat Example 28 with Gauss-Siedel iteration. Compute five
iterations and compare them with Jacobi iterations in the same example.
Exercise 20. Redo Example 29 with Jacobi iteration. Compute five itera-
tions and compare them with Gauss-Siedel iterations in the same example.
Exercise 21. Use Gauss elimination with backward substitution method and
three-digit rounding arithmetic to solve the following linear system
x1 + 3x2 + 2x3 = 5
x1 + 2x2 − 3x3 = −2
x1 + 5x2 + 3x3 = 10
.
Exercise 22. (a) Determine the LU factorisation for matrix A in the linear
system AX = B, where
−1 1 −2 2
A= 2 −1 1 and B = 1 .
−4 1 −2 4
Interpolation and
Extrapolation
4.1 Introduction
In applied sciences and engineering, scientists and engineers often collect a
number of data points of different scientific phenomena via experimentation
and sampling. In many cases they need to estimate (interpolate) a function
at a point its functional value is not in the range of the collected data. Inter-
polation is a branch of numerical analysis studies the methods and techniques
of estimating an unknown value of a function at an intermediate value of its
independent variable. Also, interpolation is used to replace a complicated
function by a simpler one.
(
1 if i = j,
`i (xj ) = δij =
0 6 j.
if i =
.
66
CHAPTER 4. INTERPOLATION AND EXTRAPOLATION
(x − x0 ) (x − x1 ) (x − xi−1 ) (x − xi+1 ) (x − xn )
`i (x) = ··· ··· .
(xi − x0 ) (xi − x1 ) (xi − xi−1 ) (xi − xi+1 ) (xi − xn )
Example 30. Determine the linear Lagrange polynomial that passes through
the points (1, 5) and (4, 2) and use it to interpolate the linear function at
x = 3.
Solution: Writing out the cardinal polynomials
(x − x1 ) (x − 4) −1
`0 (x) = = = (x − 4),
(x0 − x1 ) (1 − 4) 3
and
(x − x0 ) (x − 1) 1
`1 (x) = = = (x − 1).
(x1 − x0 ) (4 − 1) 3
1
X
P1 (x) = `i (x)f (xi ) = `0 (x)f (x0 ) + `1 (x)f (x1 ) =
i=0
−1 1
(x − 4)(5) + (x − 1)(2) = −x + 6.
3 3
67 Mohammad Sabawi/Numerical Analysis
CHAPTER 4. INTERPOLATION AND EXTRAPOLATION
So,
P1 (3) = −(3) + 6 = 3.
Note that
Example 31. Find the Lagrange polynomial that interpolates the following
data
x 1 2 2.5 3 4 5
f (x) 0 5 6.5 7 3 1
Solution: The cardinal polynomials are:
6
X
P5 (x) = `i (x)f (xi ) = `i (x)f (xi ) + `i (x)f (xi ) +
i=0
`i (x)f (xi ) + `i (x)f (xi ) + `i (x)f (xi ) + `i (x)f (xi ) =
1 5 11 4 53 3 221 2 505 25
− x + x − x + x − x+ (0) +
36 24 18 24 36 3
1 5 31 4 61 3 509 2 655
x − x + x − x + x − 50 (5) +
3 6 2 6 6
5000 5 75000 4 425000 3 1125000 2 1370000 600000
− x + x − x + x − x+ (6.5) +
7031 7031 7031 7031 7031 7031
1 5 29 4 79 3 401 2 235
x − x + x − x + x − 50 (7) +
2 4 2 4 2
1 3 137 3 109 2 365 25
− x5 + x4 − x + x − x+ (3) +
9 2 18 6 18 3
1 5 5 55 149
x − x4 + x3 − x 2 + x − 1 (1).
60 24 24 60
After some mathematical manipulation, we have
8 8 2722272566677 3 200167100491 2
P5 (x) = − x5 + x4 − x + x
316395 21093 1266637395197952 35184372088832
10969157106929 187476506320011
− x− .
1583296743997440 8796093022208
f (xj ) − f (xi )
f [xi , xj ] = .
xj − xi
The second finite divided difference is the difference between the two divided
difference, is represented by
f [xj , xk ] − f [xi , xj ]
f [xi , xj , xk ] = .
xk − xi
Likewise, the nth finite divided difference is expressed by
f [xi ] = f (xi ) = fi .
Also, observe that
where
d0 = f [x0 ],
d1 = f [x0 , x1 ],
d2 = f [x0 , x1 , x2 ],
d3 = f [x0 , x1 , x2 , x3 ],
..
.
dn = f [x0 , x1 , · · · , xn ].
Example 33. Use the data from Example 32 to construct Newton’s inter-
polation divided difference formula, and use it to evaluate f (0), f (3), and
f (3.25).
Solution: The Newton’s polynomial of third order for the data in the table
above is
P3 (x) = x3 + 2x2 − x + 1.
Hence,
75
BIBLIOGRAPHY