Professional Documents
Culture Documents
خوارزميات عددية كاملة
خوارزميات عددية كاملة
1
Contents
1. Introduction
1.1 Errors
1.2 Computational and Errors
1.3 Taylor Series Expansions
1.4 Maclaurin Series
1.1 Errors
Computations generally yield approximations as their output. This output may be an approximation to a
true solution of an equation, or an approximation of a true value of some quantity. Errors are commonly
measured in one of two ways: absolute error and relative error as the following definition.
Note that if the true value happens to be zero, 𝑥 = 0, the relative error is regarded as undefined. The
relative error is generally of more significance than the absolute error.
Example 1.3. Consider two different computations. In the first one, an estimate 𝑥𝐴 = 0.003 is obtained for
the true value 𝑥𝑇 = 0.004. In the second one, 𝑦𝐴 = 1238 for 𝑦𝑇 = 1258. Therefore, the absolute errors are
𝑨𝒆𝒓𝒓 𝑥𝐴 = 𝑥𝑇 − 𝑥𝐴 = 0.001, 𝑨𝒆𝒓𝒓(𝑦𝑇 ) = | 𝑦𝑇 − 𝑦𝐴 | = 20
𝑨𝒆𝒓𝒓(𝑥𝐴 ) 0.001
The corresponding relative errors are 𝒓𝒆𝒍(𝑥𝐴 ) = = = 0.25,
xT 0.004
𝑨𝒆𝒓𝒓(𝑦𝐴 ) 20
𝒓𝒆𝒍(𝑦𝐴 ) = = = 0.0159
𝑦𝑇 1258
We notice that the absolute errors of 0.001 and 20 can be rather misleading, judging by their magnitudes. In
other words, the fact that 0.001 is much smaller than 20 does not make the first error a smaller error relative
to its corresponding computation. In fact, looking at the relative errors, we see that 0.001 is associated with
a 25% error, while 20 corresponds to 1.59% error, much smaller than the first. Because they convey a more
specific type of information, relative errors are considered more significant than absolute errors.
Ammar Alrammahi 2023 6
1.2 Computational and Errors
Numerical methods are procedures that allow for efficient solution of a mathematically formulated
problem in a finite number of steps to within an arbitrary precision. Computers are needed in most
cases. A very important issue here is the errors caused in computations. A numerical algorithm consists of a
sequence of arithmetic and logical operations which produces an approximate solution to within any
prescribed accuracy. There are often several different algorithms for the solution of any one problem. The
particular algorithm chosen depends on the context from which the problem is taken. In economics, for
example, it may be that only the general behaviour of a variable is required, in which case a simple, low
accuracy method which uses only a few calculations is appropriate. On the other hand, in precision
engineering, it may be essential to use a complex, highly accurate method, regardless of the total amount of
computational effort involved. Once a numerical algorithm has been selected, a computer program is
usually written for its implementation. The program is run to obtain numerical results, although this may
not be the end of the story. The computed solution could indicate that the original mathematical model
needs modifying with a corresponding change in both the numerical algorithm and the program. Although
the solution of ’real problems’ by numerical techniques involves the use of a digital computer or calculator,
Determination of the eigenvalues of large matrices, for example, did not become a realistic proposition
until computers became available because of the amount of computation involved.
Ammar Alrammahi 2023 7
Nowadays any numerical technique can at least be demonstrated on a microcomputer, although there are
some problems that can only be solved using the speed and storage capacity of much larger machines. There
exist three possible sources of error:
1. Errors in the formulation of the problem to be solved.
(a) Errors in the mathematical model. For example, when simplifying assumptions are made in the
derivation of the mathematical model of a physical system. (Simplifications).
(b) Error in input data. (Measurements).
2. Approximation errors
(a) Discretization error.
(b) Convergence error in iterative methods.
(c) Discretization/convergence errors may be estimated by an analysis of the method used.
3. Roundoff errors: This error is caused by the computer representation of numbers.
(a) Roundoff errors arise everywhere in numerical computation because of the finite precision arithmetic.
(b) Roundoff errors behave quite unorganized.
4. Truncation error: Whenever an expression is approximated by some type of a mathematical method. For example,
suppose we use the Maclaurin series representation of the sine function:
(𝑛−1) 𝑚−1
(−1) 2 1 1 (−1) 2
𝑠𝑖𝑛 𝛼 = σ∞ 𝑛=𝑜𝑑𝑑 = 𝛼 − 3! 𝛼 3 + 5! 𝛼 5 − ⋯ . + 𝛼 𝑚 + 𝐸𝑚 where 𝐸𝑚 is the tail end of the expansion,
𝑛! 𝑚!
neglected in the process, and known as the truncation error.
Ammar Alrammahi 2023 8
1.3 Taylor Series Expansions
Ever wondered
• How a pocket calculator can give you the value of sine (or cos, or cot) of any angle ?.
• How it can give you the square root (or cube root, or 4th root) of any positive number ?.
• How it can find the logarithm of any (positive) number you give it ?.
Does a calculator store every answer that every human may ever ask it ?.
Actually, no. The pocket calculator just remembers special polynomials and substitutes whatever you give
it into that polynomial. It keeps substituting into terms of that polynomial until it reaches the required
number of decimal places. It then displays the answer on the screen.
Conditions
In order to find such a series, some conditions have to be in place:
• The function 𝑓(𝑥) has to be infinitely differentiable (that is, we can find each of the first derivative,
second derivative, third derivative, and so on forever).
• The function 𝑓(𝑥) has to be defined in a region near the value (𝑥 = 𝑎). Let’s see what a Taylor Series is
all about with an example.
Example 1.5. Find the Taylor Expansion of 𝑓(𝑥) = 𝑙𝑛(𝑥) near 𝑥 = 10.
Our aim is to find a good polynomial approximation to the curve
in the region near 𝑥 = 10. We need to use the Taylor Series with
𝑎 = 10. The first term in the Taylor Series is 𝑓(𝑎). In this example,
𝑓(𝑎) = 𝑓(10) = 𝑙𝑛(10) = 2.302585093.
Homework 1.6. By the same procedure we can find the Taylor series of log(𝑥) near 𝑥 = 1
∞ 𝑛+1 𝑛 2 3 4
−1 𝑥−1 𝑥−1 𝑥−1 𝑥−1
𝑙𝑜𝑔 𝑥 ≈ = 𝑥−1 − + − +⋯
𝑛 Ammar Alrammahi 2023
2 3 4 12
𝑛=1
1.4 Maclaurin Series
Maclaurin Series is a particular case of Taylor Series, in the region near x = 0. Such a polynomial is called the
Maclaurin Series. The infinite series expansion for f(x) about x = 0 becomes:
𝑥 2 𝑥 3 𝑥 𝑛
𝑓 𝑥 = 𝑓 0 + 𝑓 ′ 0 𝑥 + 𝑓 ′′ 0 + 𝑓 ′′′ 0 + ⋯+ 𝑓 𝑛 0 +⋯
2! 3! 𝑛!
∞ (𝑛) 𝑥𝑛
We can write this using summation notation as: 𝑓 𝑥 = σ𝑛=0 𝑓 0
𝑛!
Example 1.8. Find the Maclaurin Series expansion for f(x) = sin(x). We need to find the first, second, third,
etc derivatives and evaluate them at x = 0.
Starting with: for f(x) = sin (x) → f(0) = sin(0) = 0. Now for the derivatives:
𝑠𝑖𝑛′ 𝑥 = cos(x) → 𝑠𝑖𝑛′ 0 = cos(0) = 1. 𝑠𝑖𝑛′′ 𝑥 = − sin(x) → 𝑠𝑖𝑛′′ (0) = − sin(0) = 0.
𝑠𝑖𝑛′′′ 𝑥 = − cos(x) → 𝑠𝑖𝑛′′′ 0 = − cos(0) = − 1. 𝑠𝑖𝑛𝑖𝑣 𝑥 = sin(x) → 𝑠𝑖𝑛𝑖𝑣 (0) = sin(0) = 0.
We observe that this pattern will continue forever. Now to substitute the values of these derivatives into the
Maclaurin Series:
𝑥2 𝑥 3 𝑥𝑛
𝑓 𝑥 = 𝑓 0 + 𝑓 ′ 0 𝑥 + 𝑓 ′′ 0 + 𝑓 ′′′ 0 + ⋯+ 𝑓 𝑛
0 +⋯
2! 3! 𝑛!
We have
𝑥2 𝑥3 𝑥𝑛
𝑠𝑖𝑛 𝑥 = 𝑠𝑖𝑛 0 + 𝑠𝑖𝑛′ 0 𝑥+ 𝑠𝑖𝑛′′ 0 ′′′
+ 𝑠𝑖𝑛 0 + ⋯ + 𝑠𝑖𝑛 𝑛 0 +⋯
2! 3! 𝑛!
Ammar Alrammahi 2023 13
This gives us:
∞
𝑥3 𝑥5 𝑥7 𝑥9 𝑥 11
−1 𝑛 2𝑛+1
𝑠𝑖𝑛 𝑥 = 𝑥 − + − + − +⋯ = 𝑥
3! 5! 7! 9! 11! 2𝑛 + 1 !
𝑛=0
Homework 1.10. Use the same procedure as in previous Example 1.8 to check the following Maclaurin series:
1
1. = σ∞ 𝑛 2 3
𝑛=0 𝑥 = 1 + 𝑥 + 𝑥 + 𝑥 + ⋯ (when −1 < x < 1)
1−𝑥
𝑥𝑛 𝑥2 𝑥3
2. 𝑒𝑥 = σ∞
𝑛=0 𝑛! =1+𝑥+ + +⋯
2! 3!
−1 𝑛 2𝑛 𝑥2 𝑥4 𝑥6
3. 𝑐𝑜𝑠 𝑥 = σ∞
𝑛=0 𝑥 =1− + − 6! +⋯
2𝑛 ! 2! 4! Ammar Alrammahi 2023 16
2 Solutions of Equations in One Variable
One of the fundamental problems of mathematics is that of solving equations of the form
𝑓 𝑥 =0 2.1
where 𝑓 is a real valued function of a real variable x. Any number α satisfying equation (2.1) is called a root
of the equation or a zero of 𝑓.
Most equations arising in practice are non-linear and are rarely of a form which allows the roots to be
determined exactly. Consequently, numerical techniques must be used to find them. Graphically, a solution,
or a root, of equation (2.1) refers to the point of intersection of 𝑓 𝑥 and the x-axis. Therefore, depending on
the nature of the curve of 𝑓 𝑥 in relation to the x-axis, Equation (2.1) may have a unique solution, multiple
solutions, or no solution. A root of an equation can sometimes be determined analytically resulting in an exact
solution.
1
For instance, the equation 𝑒 2𝑥 − 3 = 0 can be solved analytically to obtain a unique solution 𝑥 = 𝑙𝑛(3) . In
2
most situations, however, this is not possible and the root(s) must be found using a numerical procedure.
Example 2.1. The function 𝑓 𝑥 = 𝑥 2 − 𝑥 − 1 has a root in [1, 2], because 𝑓(1) = −1 and 𝑓(2) = 1 the
Intermediate Value Theorem ensures that this continuous function has a root in [1, 2]. Use Bisection method
to find an approximation root with 𝜖 = 0.003 and then write the Matlab code for this example.
𝑎1 + 𝑏1 1 + 2
𝑝1 = = = 1.5 → 𝑓 1.5 = (1.5)2 −1.5 − 1 = −0.25
2 2
𝑘 𝑎𝑘 𝑏𝑘 𝑎𝒌 + 𝑏𝒌 𝑓(𝑎𝑘) 𝑓(𝑝𝑘)
𝑝𝑘 =
2
1 1 2 1.5 −1 − 0.25
2 1.5 2 1.75 − 0.25 0.3125
3 1.5 1.75 1.625 − 0.25 0.015625
4 1.5 1.625 1.5625 − 0.25 − 0.12109375
5 1.5625 1.625 1.59375 − 0.12109375 − 0.0537109375
6 1.59375 1.625 1.609375 − 0.0537109375 − 0.019287109375
7 1.609375 1.625 1.6171875 − 0.0192871094 − 0.00189208984375
Hence the approximation of the root is 1.6171875 since | f(pk ) | = | − 0.00189208984375 | < 0.003
Ammar Alrammahi 2023 19
Matlab Code 2.2. Bisection Method
clc
clear
f = @(x) x.^2-x-1;
fprintf('k ak bk pk f(ak) f(pk) \n' );
fprintf('__ ________ _________ ________ _________ __________ \n' );
a = 1; b = 2; p = (a+b)/2; t = 0.003;
fprintf('%6.f %10.8f %10.8f %10.8f %10.10f %10.10f \n',1,a,b,p,f(a),f(p));
k = 2;
while abs(f(p)) > t
if f(p)*f(a) < 0
b = p;
else
a = p;
end
p = (a+b)/2;
fprintf('%6.f %10.8f %10.8f %10.8f %10.10f %10.10f \n',k,a,b,p,f(a),f(p));
k = k+1;
end
fprintf('The approximated root is p= %10.10f \n', p)
Ammar Alrammahi 2023 20
2.2 MATLAB built-In Function fzero
The fzero function in MATLAB finds the roots of f(x) = 0 for a real function f(x). FZERO Scalar nonlinear
zero finding. X = FZERO(FUN, X0) tries to find a zero of the function FUN near X0, if X0 is a scalar. For
Example 2.1 use the following Matlab code:
Matlab Code 2.3. Function fzero
clc; clear
fun = @(x) x.^3 + 4*x.^2 - 10; % function
X0 = 2; % initial point
X = fzero(fun,X0)
Theorem 2.4. Suppose that 𝑓 ∈ 𝐶[𝑎, 𝑏] and 𝑓(𝑎)𝑓(𝑏) < 0. The Bisection method generates a sequence
𝑝𝑛 ∞
𝑏−𝑎
𝑛=1 approximating a zero p of f with 𝑝𝑛 − 𝑝 < 𝑛 , n ≥ 1.
2
It is important to realize that Theorem 2.4 gives only a bound for approximation error and that this bound
might be quite conservative. For example, this bound applied to the problem in Homework 2.5 (ii) ensures
2−1
only that 𝑝9 − 𝑝 < 9 = 0.001953125 ≈ 2 × 10−3 but the actual error is much smaller:
2
Homework 2.5. Use the same procedure as in previous Example 2.1 to find the approximate root for the first
three functions with 𝜖 = 0.0001
2 −2)
i. 𝑓 𝑥 = (𝑥 + 1)2 𝑒 (𝑥 − 1 has a root in [0, 1] because 𝑓(0) < 0 and f(1) > 0.
ii. 𝑓 𝑥 = 𝑥 3 + 4𝑥 2 − 10 has a root in [1, 2] because𝑓(1) < 0 and𝑓(2) > 0.
iii. 𝑓 𝑥 = 𝑥𝑙𝑜𝑔10(𝑥) − 1 has a root in [1, 3] because 𝑓(1) < 0 and 𝑓(3) > 0.
iv. Use the Bisection method to find p3 for 𝑓 𝑥 = 𝑥 − cos 𝑥 on [0, 1].
Ammar Alrammahi 2023 22
2.3 Fixed Point Iteration Method
The bisection method we have described earlier depends on our ability to find an interval in which the root
lies. The task of finding such intervals is difficult in certain situations. In such cases we try an alternate
method called Fixed Point Iteration Method.
The first step in this method is to rewrite the equation 𝑓(𝑥) = 0 as
𝑥 = 𝑔(𝑥) (2.1)
Any solution of this equation is called a fixed point of g (fixed point of a function g is a point p such that
𝑔(𝑝) = 𝑝) . Once an iteration function is chosen. our next step is to take a point x0. close to the root, as the
initial approximation of the root.
Starting with 𝑥0, we find the first approximation 𝑥1 as 𝑥1 = 𝑔(𝑥0). Then we find the next approximation as
𝑥2 = 𝑔(𝑥1). Similarly we find the successive approximations 𝑥2, 𝑥3, 𝑥4 . . . . as 𝑥3 = 𝑔(𝑥2), 𝑥4 = 𝑔(𝑥3) . . . . 𝑥𝑛 + 1 = 𝑔(𝑥𝑛).
Each computation of the type 𝑥𝑛 + 1 = 𝑔(𝑥𝑛) is called an iteration. Now, two questions arise (i) when do we
stop these iterations? (ii) Does this procedure always give the required solution?
To ensure this we make the following assumptions on 𝑔(𝑥):
Assumption * : The derivative 𝑔′(𝑥) of 𝑔(𝑥) exists, 𝑔′(𝑥) is continuous and satisfies |𝑔′(𝑥)| < 1 in an
interval containing 𝑥0. (That would mean that we require |𝑔′(𝑥𝑖)| < 1 at all iterates 𝑥𝑖.) The iteration is usually
stopped whenever |𝑥𝑖 + 1 – 𝑥𝑖| is less than the accuracy (the tolerance 𝜖 > 0 ) required. Now we shall illustrate
this method with the following example. Ammar Alrammahi 2023 23
Example 2.6: Find an approximate root of the equation 𝑥 2 − 2𝑥 − 8 = 0 using fixed point iteration method,
starting with 𝑥0 = 5. Stop the iteration whenever |𝑥𝑖 + 1 − 𝑥𝑖| < 0.001.
2𝑥+8
Solution: Three possible rearrangements of this equation are (1) 𝑥 = 2𝑥 + 8 (2) 𝑥 =
𝑥
𝑥2− 8
(3) x =
2
Case 1: Let us see whether Assumption (*) is satisfied for this g(x) for the first form. Here 𝑔(𝑥) = 2𝑥 + 8.
1 1/2 > 1. For any positive real number x, we see
We have 𝑔′(𝑥) = 1/2 . Then |g'(x)| < 1 whenever (2𝑥 + 8)
(2𝑥+8)
that the inequality (2𝑥 + 8)1/2 > 1 is satisfied. Therefore, we consider any interval on the positive side of
x-axis. Since the starting point is x0 = 5, we may consider the interval I = [3,6]. This contains the point 5.
Now, 𝑔(𝑥) satisfies the condition that 𝑔′(𝑥) exists on I, 𝑔′(𝑥) is continuous on I and |𝑔′(𝑥)| < 1 for every x
in the interval [3,6]. Now we apply fixed point iteration method to 𝑔(𝑥). We get
Since | 𝑥6 − 𝑥5 | = | 4.0002 − 4.0009 | = | − 0.0006 | = 0.0006 < 0.001, we conclude that an approximate
value of a root of 𝑓(𝑥) = 0 is 4.0002. Ammar Alrammahi 2023 24
2𝑥+8 2𝑥+8 −8
Case 2: Let us consider the second form, 𝑥 = . Here 𝑔(𝑥) = and 𝑔′ 𝑥 = 2 . The |𝑔′(𝑥)| < 1 for
𝑥 𝑥 𝑥
any positive real number 𝑥 ≥ 3. Hence g(x) satisfies Assumption (*) in the interval [3, 6]. Now we leave it
as an exercise for you to complete the computations.
𝑥 2 −8 𝑥 2 −8
Case 3: Here we have 𝑥 = .
Then 𝑔(𝑥) = and g ′ 𝑥 = 𝑥. In this case |g ′ (𝑥)| < 1 only if | 𝑥 | < 1 i.e.
2 2
if 𝑥 lies in the interval [− 1, l]. But this interval does not contain 𝑥0 = 5. Therefore 𝑔(𝑥) does not satisfy the
Assumption (*) in any interval containing the initial approximation. Hence, the iteration method cannot
provide approximation to the desired root.
Example 2.7. Find the approximate solution for the equation 𝑓 𝑥 = 𝑥 4 − 𝑥 − 10 by fixed point iteration
method starting with 𝑥0 = 1.5 with |𝑥𝑖 + 1 − 𝑥𝑖| < 0.009.
Solution: The function 𝑓(𝑥) has a root in the interval (1, 2), why?, rearrange the equation as
−3
4 1
𝑥 = 𝑔 𝑥 = 𝑥 + 10 then 𝑔′
𝑥 = + 10) . The |𝑔′ (𝑥)| < 1 for any positive real number 𝑥 ≥ 0.
(𝑥 4
4
Hence 𝑔(𝑥) satisfies Assumption (*) in the interval [1, 2]. Then we get the solution sequence
{𝑥0 = 1.5, 𝑥1 = 1.8415, 𝑥2 = 1.8550, 𝑥3 = 1.8556, . . . . }. Since |1.8550 − 1.8556| = 0.0006 < 0.009, we
conclude that an approximate value of a root of 𝑓(𝑥) = 0 is 1.8556
Ammar Alrammahi 2023 25
Matlab Code 2.8. Fixed Point Iteration Method
clc
clear
g = input('Enter your function:'); %@(x) sqrt(2*x+8);
x0 = input('Enter initial approximation, x0:'); %5
n = input('Enter no. of iterations, n:’); %30
t = input('Enter tolerance, t:'); %0.001
fprintf(' i xi \n' );
fprintf(' __ ________ \n');
for i = 1:n
x = g(x0);
fprintf('\n %11.0f %11.5f \n\n', i, x)
if abs(x - x0) < t
break
end
x0 = x;
end
fprintf('\n Approximate solution x = %11.5f \n\n', x);
Homework 2.9. Use an appropriate fixed point iteration to find the root of (i) x − cos x = 0
(ii) 𝑥 2 − ln(𝑥 + 1) = 0 starting in each case with 𝑥0 = 1. Stop when |𝑥𝑖 + 1 − 𝑥𝑖| < 0.0005
Ammar Alrammahi 2023 26
2.4 Newton Raphson Method
The Newton Raphson method is for solving equations of the form 𝑓(𝑥) = 0. We make an initial guess for the
root we are trying to find, and we call this initial guess 𝑥0. The sequence 𝑥0, 𝑥1, 𝑥2, 𝑥3, . . . generated in the
manner described below should converge to the exact root.
To implement it analytically we need a formula for each approximation in terms of the previous one, i.e. we
need 𝑥𝑛 + 1 in terms of 𝑥𝑛. The equation of the tangent line to the graph 𝑦 = 𝑓(𝑥) at the point (𝑥0, 𝑓(𝑥0)) is
𝑦 − 𝑓(𝑥0) = 𝑓 ′ (𝑥0)(𝑥 − 𝑥0)
The tangent line intersects the x-axis when y = 0 and x = x1, so
− 𝑓(𝑥0) = 𝑓 ′ (𝑥0)(𝑥1 − 𝑥0)
Solving this for x1 gives
𝑓(𝑥0 )
𝑥1 = 𝑥0 − ′
𝑓 (𝑥0 )
and, more generally,
𝑓(𝑥𝑛 )
𝑥𝑛 + 1 = 𝑥𝑛 − ′
𝑓 (𝑥𝑛 )
2𝑥1 −5𝑥1+2
𝑥2 = 𝑥 1 −
2𝑥1 𝑙𝑜𝑔2−5
2𝑥2 −5𝑥2+2
𝑥3 = 𝑥 2 −
2𝑥2 𝑙𝑜𝑔2−5
STEP 1: The first step eliminates the variable x1 from the second and third equations. This can be done by
𝑎 𝑎
subtracting multiples 𝑚21 = 21 and 𝑚31 = 31 of row 1 from rows 2 and 3, respectively, producing the
𝑎11 𝑎11
equivalent system
STEP 2: The second step eliminates the variable x2 from the third equation. This can be done by subtracting
(2)
𝑎32
a multiple 𝑚32 = (2) from row 2 and 3, producing the equivalent upper triangular system
𝑎22
𝑎11 𝑎12 𝑎13 𝑏1
𝑥1
(2) (2) (2)
0 𝑎22 𝑎23 𝑥2 = 𝑏2 ,
0 0
(3)
𝑎33 𝑥3 (3)
𝑏3
(3) (2) (2) (3) (2) (2)
where 𝑎33 = 𝑎33 − 𝑚32 𝑎23 and 𝑏3 = 𝑏3 − 𝑚32 𝑏2 . Since these row operations are reversible, the original
system and the upper triangular system have the same solution. The upper triangular system is solved using back
(3)
𝑏3
substitution. The last equation implies that 𝑥3 = (3) . This number can then be substituted into the second equation and
𝑎33
(2) (2)
𝑏2 − 𝑎23 𝑥3
the value of x2 obtained from 𝑥2 = (2) .
𝑎22 Ammar Alrammahi 2023 33
Finally, the known values of x2 and x3 can be substituted into the first equation and the value of x1 obtained
from
𝑏1 − 𝑎12 𝑥2 − 𝑎13 𝑥3
𝑥1 =
𝑎11
(j)
It is clear from previous equations that the algorithm fails if any of the quantities 𝑎jj are zero, since these
numbers are used as the denominators both in the multipliers 𝑚𝑗𝑗 and in the back substitution equations.
These numbers are usually referred to as pivots. Elimination also produces poor results if any of the
multipliers are greater than one in modulus. It is possible to prevent these difficulties by using row
interchanges. At step j, the elements in column j which are on or below the diagonal are scanned. The row
containing the element of largest modulus is called the pivotal row. Row j is then interchanged (if
necessary) with the pivotal row.
(j) (j) (j)
It can, of course, happen that all of the numbers 𝑎jj , 𝑎(j+1)j , · · · , 𝑎nj are exactly zero, in which case the
coefficient matrix does not have full rank and the system fails to possess a unique solution.
clc
clear
A = input('Enter your coefficient matrix:');
% [0.61 1.23 1.72; 1.02 2.15 -5.51; -4.34 11.2 -4.25];
b = input('Enter source vector:')
% [0.792 12 16.3]';
C = [A b]; %Augmented Matrix
n = size(C,1); %number of equations/variables
x = zeros(n,1); %variable matrix [x1 x2 ... xn] coulmn
for i = 1:n-1
for j = i+1:n
m = C(j,i)/C(i,i);
C(j,:) = C(j,:) - m*C(i,:);
end
end
x(n) = C(n,n+1)/C(n,n);
for k = n-1:-1:1
x(k) = (C(k,n+1) - C(k,k+1:n)*x(k+1:n))/C(k,k);
end
x'
Ammar Alrammahi 2023 37
Homework 3.3. Solve the following systems of linear equations using Gauss elimination
(i) without pivoting (ii) with partial pivoting.
1) 0.005x1 + x2 + x3 = 2
x1 + 2x2 + x3 = 4
− 3x1 − x2 + 6x3 = 2
2) x1 − x2 + 2x3 = 5
2x1 − 2x2 + x3 = 1
30x1 − 2x2 + 7x3 = 20
1 1 1 | 5 R2 = R2 − 2 R1 1 1 1 | 5 R3 = R3 + 4 R2
2 3 5 | 8 0 1 3 | −2
4 0 5 | 2 𝑅3 =𝑅3 − 4 𝑅1 0 −4 1 | −18
1 1 1 | 5 R3 = 1 R3 1 1 1 | 5 R2 = R2 − 3 R3
13
0 1 3 | −2 0 1 3 | −2
0 0 13 | −26 0 0 1 | −2
1 1 1 | 5 R1 = R1 − R3 1 1 0 | 7 R1 = R1 − R2 1 0 0 | 3
0 1 0 | 4 0 1 0 | 4 0 1 0 | 4
0 0 1 | −2 0 0 1 | −2 0 0 1 | −2
From this final matrix, we can read the solution of the system. It is 𝑥 = 3, 𝑦 = 4, 𝑧 = − 2.
Ammar Alrammahi 2023 40
Example 3.5. Solve the following system of equations using the Gauss Jordan elimination method.
4y + z = 2
2x + 6y − 2z = 3
4x +8y − 5z = 4
0 4 1 | 2
Solution: The extended matrix of the system is the following. 2 6 −2 | 3
4 8 −5 | 4
use the row operations as following:
0 4 1 | 2 R1 ՞ R2 2 6 −2 | 3 R3 =R3 − 2 R1 2 6 −2 | 3
2 6 −2 | 3 0 4 1 | 2 0 4 1 | 2
4 8 −5 | 4 4 8 −5 | 4 0 −4 −1 | −2
2 6 −2 | 3 2 6 −2 | 3
R3 =R3 +R2 R2 = 14 R2 1 1 R1 = R1 − 6 R2
0 4 1 | 2 0 1 ቚ
4 2
0 0 0 | 0 0 0 0 | 0
−7 −7
2 0 | 0 1 0 | 0
2 R1 = 12 R1 4
1 1 1 1
0 1 ฬ 0 1 ฬ
4 2 4 2
0 0 0 | 0 0 0 0 | 0
Any value of the parameter t gives us a solution of the system. For example:
1
𝑡 = 4 gives the solution (𝑥, 𝑦, 𝑧) = (7, − , 4)
2
7
𝑡 = − 2 gives the solution (𝑥, 𝑦, 𝑧) = (− , 1, −2)
2
clc
clear
A = input('Enter your coefficient matrix:');
% [1 1 1; 2 3 5;4 0 5];
b = input('Enter source vector:')
% [5 8 2]';
C = [A b]; %Augmented Matrix
n = length(b); %number of unknown/variables
x = zeros(n,1); %variable matrix [x1 x2 ... xn] coulmn
for j = 1:n %for columns
C(j,:) = C(j,:)/C(j,j)
for i = 1:n %for rows
if i ~= j
m = C(i,j);
C(i,:) = C(i,:) - m*C(j,:)
end
end
end
1)
x1 + x2 + 2x3 = 1
2x1 − 2x2 + + x4 =−2
x1 − x2 − x3 − 2x4 = 2
2x1 − x2 + 2x3 − x4 = 0
2)
30x1 − 2x2 + 7x3 = 20
x1 − x2 + 2x3 = 5
2x1 − 2x2 + x3 = 1
𝑥𝑖 𝑥1 𝑥2 ……… 𝑥𝑛
𝑦𝑖 𝑦1 𝑦2 ……… 𝑦𝑛
𝑥 −1 0 1 2
𝑦 1.4 0.8 1.7 2
by a function of the form
𝑓 𝑥 = 𝑎1 𝑒 −𝑥 + 𝑎2 + 𝑎3 𝑒 𝑥 + 𝑎4 𝑒 2𝑥
In this case the basis functions are
𝑓1 𝑥 = 𝑒 −𝑥 , 𝑓2 𝑥 = 1, 𝑓3 𝑥 = 𝑒 𝑥 , 𝑓4 𝑥 = 𝑒 2𝑥
Now the problem is to determine the coefficients 𝑎1 , 𝑎2 , 𝑎3 and 𝑎4 . The basis matrix is
Homework 4.5. Determine the equation of the polynomial whose graph passes through the points:
x 0 0.5 1 1.5 2 3
y 0 − 1.40625 0 1.40625 0 0
Ammar Alrammahi 2023 52
Matlab Code 4.6. Polynomial Interpolation
clc
clear
close all
xi = input('Enter your xi data values:');
%[-1 0 1 2]
yi = input('Enter your yi data values:')
% [1.4 0.8 1.7 2]
p = polyfit(xi,yi,3)
x2 = -1:0.1:2;
y2 = polyval(p,x2);
plot(xi,yi,'o',x2,y2,'linewidth',2)
title ('Plot using Polynomial Interpolation')
grid on
𝑝𝑛 𝑥 = 𝑦𝑘 𝐿𝑘 (𝑥) = 𝑦0 𝐿0 𝑥 + 𝑦1 𝐿1 𝑥 + ⋯ + 𝑦𝑘 𝐿𝑘 (𝑥)
𝑘=0
where
𝑛
𝑥 − 𝑥𝑖
𝐿𝑘 𝑥 = ෑ (4.2)
𝑥𝑘 − 𝑥𝑖
𝑖=0,𝑖≠𝑘
or
𝑥 − 𝑥0 𝑥 − 𝑥1 ⋯ (𝑥 − 𝑥𝑘−1 )(𝑥 − 𝑥𝑘+1 ) ⋯ (𝑥 − 𝑥𝑛 )
𝐿𝑘 𝑥 =
(𝑥𝑘 − 𝑥0 )(𝑥𝑘 − 𝑥1 ) ⋯ (𝑥𝑘 − 𝑥𝑘−1 )(𝑥𝑘 − 𝑥𝑘+1 ) ⋯ (𝑥𝑘 − 𝑥𝑛 )
Note that
1 𝑖=𝑘
𝐿𝑘 𝑥𝑖 = ቊ
0 𝑖≠𝑘
Ammar Alrammahi 2023 54
this lead to
𝑝𝑛 𝑥 = 𝑦0 𝐿0 𝑥𝑖 + ⋯ + 𝑦𝑖−1 𝐿𝑖−1 𝑥𝑖 + 𝑦𝑖 𝐿𝑖 𝑥𝑖 + 𝑦𝑖+1 𝐿𝑖+1 𝑥𝑖 + ⋯ + 𝑦𝑛 𝐿𝑛 𝑥 = 𝑦𝑖
Theorem 4.7. Let 𝑥0, 𝑥1,· · · , 𝑥𝑛, be 𝑛 + 1 distinct numbers, and let 𝑓(𝑥) be a function defined on a domain
containing these numbers. Then the polynomial defined by
𝑛
Example 4.8. We will use Lagrange interpolation to find the polynomial 𝑝𝑛 𝑥 , of degree 3 or less, that agrees
with the following data
𝑥 −1 0 1 2
𝑦 3 −4 5 −6
In other words, we must have a polynomial 𝑝(𝑥) satisfy 𝑝(−1) = 3, 𝑝(0) = − 4, 𝑝(1) = 5 and 𝑝(2) = − 6.
First, we construct the Lagrange polynomials 𝐿𝑘 (𝑥) 3𝑘=0 using the formula (4.2).
Ammar Alrammahi 2023 55
This yields
𝑥 − 𝑥1 𝑥 − 𝑥2 (𝑥 − 𝑥3 ) 𝑥 − 0 𝑥 − 1 (𝑥 − 2) −1
𝐿0 𝑥 = = = (𝑥 3 − 3𝑥 2 + 2𝑥)
(𝑥0 − 𝑥1 )(𝑥0 − 𝑥2 )(𝑥0 − 𝑥3 ) (−1 − 0)(−1 − 1)(−1 − 2) 6
𝑥 − 𝑥0 𝑥 − 𝑥2 (𝑥 − 𝑥3 ) 𝑥 + 1 𝑥 − 1 (𝑥 − 2) 1 3
𝐿1 𝑥 = = = (𝑥 − 2𝑥 2 − 𝑥 + 2)
(𝑥1 − 𝑥0 )(𝑥1 − 𝑥2 )(𝑥1 − 𝑥3 ) (0 + 1)(0 − 1)(0 − 2) 2
𝑥 − 𝑥0 𝑥 − 𝑥1 (𝑥 − 𝑥3 ) (𝑥 + 1) 𝑥 − 0 (𝑥 − 2) −1
𝐿2 𝑥 = = = (𝑥 3 − 𝑥 2 − 2𝑥)
(𝑥2 − 𝑥0 )(𝑥2 − 𝑥1 )(𝑥2 − 𝑥3 ) (1 + 1)(1 − 0)(1 − 2) 2
𝑥 − 𝑥0 𝑥 − 𝑥1 (𝑥 − 𝑥2 ) (𝑥 + 1) 𝑥 − 0 (𝑥 − 1) 1
𝐿3 𝑥 = = = (𝑥 3 − 𝑥)
(𝑥3 − 𝑥0 )(𝑥3 − 𝑥1 )(𝑥3 − 𝑥2 ) (2 + 1)(2 − 0)(2 − 1) 6
By substituting xi for x in each Lagrange polynomial Lk (x), for k = 0, 1, 2, 3, it can be verified that
1 𝑖=𝑘
𝐿𝑘 𝑥𝑖 = ቊ
0 𝑖≠𝑘
Homework 4.10. Find the polynomial 𝑓(𝑥) by using Lagrange’s formula and then find 𝑓(−1) for
𝑥 −2 0 1 2
𝑦 −9 1 0 11