Professional Documents
Culture Documents
APM2613 - 202 - 0 - 2022 (A02 Feedback)
APM2613 - 202 - 0 - 2022 (A02 Feedback)
APM2613
Year module
Note: This is a fully online module and therefore it is only available on myUnisa.
BARCODE
university
Define tomorrow. of south africa
APM2613 Assignment 01
1 Checklist
By this time you should have received the following tutorial matter:
• APM2613/101/0/2022- General information about the module and the assignments
2 Introduction
PLEASE NOTE:
Assignment 02 covers numerical methods for solving linear and nonlinear systems of equations.
In this assignment you get to practise on solving linear systems using direct and indirect(iterative)
methods. The relevant section of the textbook are Chapter 6, 7 and Section 10.1.
You have been told that in working out the following exercises, you can use Matlab/Octave to com-
pute. The solutions given below are based on computations using Matlab/Octave, and they show
the essential steps in the computations. Most of the values given in the process were obtained
using short format of Matlab/Octave, mostly in line with the 4 decimal place requirement.
3 Learning outcomes
After this assignment you should be be able to, among other things:
1. Work more with finite digit/decimal arithmetic accuracy.
• Gaussian elimination without pivoting (read the section on partial pivoting to understand
what without pivoting means).
• Gaussian elimination with scaled partial pivoting.
• Basic LU-decomposition.
• Using the inverse matrix method
Note that Gaussian elimination is the main focus of this module in solving linear systems as
opposed to other linear algebra methods, say, for finding the inverse.
• Jacobi method
• Gauss-Seidel method
2
APM2613/202/0/2022
• SOR method
4. Compare solutions obtained using different methods; e.g. residual vector. You should also
beware of the norm and condition number of a matrix as related to convergence of iterative
schemes.
5. Apply Newton’s method for systems of nonlinear equations.
Please note that in an assignment, the set of exercises given does not cover all the possible
concepts. So you should not read only what is covered in the assignment, rather read according
to the plan given in TL101.
4 Feedback on Solutions
Question 1 [30 marks]
Consider the linear system
√ √ √
πx1 − e x2 + 2x3 − 3x4 = 11
2 2 3
√ π x1 +√e x2 − e x3 +
√ 7 x4 = 0
5x1 − 6x2 +√x3 − 2x4 = √π
π 3 x1 + e2 x2 − 7x3 + 19 x4 = 2
whose actual solution is x = (0.788, −3.12, 0.167, 4.55)T . Carry out the following computations using
4 decimal places with rounding:
(1.1) Write the system as a matrix equation. (2)
(1.2) Solve the system using:
(a) Gaussian elimination without pivoting. (7)
(b) Gaussian elimination with scaled partial pivoting. (7)
(c) Basic LU decomposition. (7)
(1.3) Determine the number of arithmetic operations (multiplication/ division, addition/subtraction)
in (1.2)(a) above. (3)
(1.4) By comparing the residual vectors based on the approximate solutions obtained in (1.2)(a),
(b) and (c), which method gave the best approximation. (4)
Solution
(1.1) First we write the given system such that the coefficients are in decimal form, with four decimal
places values:
3.1416x1 − 2.7183 x2 + 1.4142x3 − 1.7321x4 = 3.3166
9.8696x1 + 2.7183 x2 − 7.3891x3 + 0.4286x4 = 0
2.2361x1 − 2.4495x2 + x3 − 1.4142x4 = 3.1416
31.0063x1 + 7.3891x2 − 2.6458x3 + 0.1111x4 = 1.4142
3
From the above system we deduce the matrix form
3.1416 −2.7183 +1.4142 −1.7321 x1 3.3166
9.8696 2.7183 −7.3891 0.4286 x2 0
x3 = 3.1416
2.2361 −2.4495 1 −1.4142
31.0063 7.3891 −2.6458 0.1111 x4 1.4142
(1.2) (a) To perform Gaussian elimination we start with the augmented matrix
3.1416 −2.7183 1.4142 −1.7321 3.3166
9.8696 2.7183 7.3891 0.4286 0
C= 2.2361 −2.4495
1.0000 −1.4142 3.1416
31.0063 7.3891 −2.6458 0.1111 1.4142
As an example the first reduction, the first set of multipliers is obtained using
for i=2:4
m(i,1)=C(i,1)/C(1,1)
end
after initialising the m-matrix as M = zeros(4).
The underlying manual computations are:
m21 = C(2, 1)/C(1, 1); m31 = C(3, 1)/C(1, 1); m41 = C(4, 1)/C(1, 1)
The first reduction of rows R2 to R4 using the first row is computed using the Mat-
lab/Octave code
for i=2:4
C(i,:)=C(i,:)-m(i,1)*C(1,:)
end
which yields
3.1416 −2.7183 1.4142 −1.7321 3.3166
0 11.2581 2.9463 5.8701 −10.4194
C (1)
0 −0.5147 −0.0066 −0.1813 0.7809
0 34.2176 −16.6034 17.2062 −31.3193
Comment:
It is very important to note the structure of the reduction formula to reduce Row j using
Row i: Rj = Rj − mji Ri for j = i + 1, . . . , n for (some people mix this up and end up with
wrong values)
The command C(i,:)=C(i,:)-m(i,2)*C(2,:) for i = 3, 4 will perform the next row reduction of
R3 and R4 using R2 to yield
3.1416 −2.7183 1.4142 −1.7321 3.3166
0 11.2581 2.9463 5.8701 −10.4194
C (2) =
0 0 0.1281 0.0870 0.3046
0 0 −25.5582 −0.6354 0.3492
4
APM2613/202/0/2022
The last (third) row reduction of R4 using R3 will use m43 = m(4, 3) = C(4, 3)/C(3, 3)
and C(4, :) = C(4, :) − m(4, 3) ∗ C(3, :) which leads to
3.1416 −2.7183 1.4142 −1.7321 3.3166
0 11.2581 2.9463 5.8701 −10.4194
C (3) =
0 0 0.1281 0.0870 0.3046
0 0 0 16.7268 61.1156
Upon back substituting we have (for bravity let us drop the iterative index in the last
matrix C (3) and just use C):
NOTE: s is a set of values, not a vector. These values are row specific. They are com-
puted only once and are used through out the computations. When a row is moved, the
value moves with it.
In the following steps we perform Gaussian elimination using the scaling factor si com-
puted above.
Step 1:
Next we compute the ratios to determine whether row interchange is necessary. These
5
are ratios of the latest matrix entries in the pivot column and the corresponding scalers.
|C(i1)|
ri1 = , i = 1, . . . , 4
s(i)
|C(1, 1)| |C(2, 1)| |C(3, 1)| |C(4, 1)|
ri1 = , , ,
s1 s2 s3 s4
|3.1416| |9.8696| |2.2361| |31.0063|
= , , ,
3.1416 9.8696 2.4495 31.0063
= {1, 1, 0.9130, 1}
Using the notation in the textbook, rp1 = max{ri1 } = max{1, 1, 0.9130, 1} This time we
have a tie between r11 , r21 and r41 . In a case like this, where there is no clear max, we
make an arbitrary row choice among those that qualify. Let’s choose Row 1; i.e p = 1.
In which case there is no row interchange. So we reduce the matrix as it is to get (as
computed in (1.2)(a) above)
3.1416 −2.7183 1.4142 −1.7321 3.3166 s1
0 11.2581 2.9463 5.8701 −10.4194 s2
C (1)
0 −0.5147 −0.0066 −0.1813 0.7809 s3
0 34.2176 −16.6034 17.2062 −31.3193 s4
Notice that we have appended an extra column outside the augmented matrix to help
keep track of the rows after interchanging and the corresponding si . (This is optional,
done here for the convenience of visual tracking purposes)
Step 2:
Before we continue with the reduction we need to decide whether the next pivot C(2, 2)
is the most suitable one to pivot on. So we focus on rows Ri ; i = 2, 3, 4 and use them
with their corresponding scalers.
So we compute
|C(i2)|
ri2 = , i = 2, 3, 4
s(i)
to obtain
|C(2, 2)| |C(3, 2)| |C(4, 2)|
ri2 = , ,
s2 s3 s4
|11.2581| | − 0.5147| |34.2176|
= , ,
9.8696 2.4495 31.0063
= {1.1407, 0.2101, 1.1036}
So that rp2 = r2,2 ; i.e. p = 2. This means that Row 2 is the next pivot row, hence there is
no row interchange.
6
APM2613/202/0/2022
Step 3:
Now we repeat the process of looking for the next best pivot row, focusing on i = 3, 4.
|C(i3)|
ri3 = { , i = 34}
s(i)
|C(3, 3)| |C(4, 3)|
= { , }
s3 s4
| − 25.5582| | − 0.6354|
= { , }
2.4495 31.0063
= {0.0523, 0.8243}
(c) The key to answering this question is found in Theorem 6.19. Part (a) requires solution
of the system by Gaussian elimination without pivoting. This is the same as saying with-
out row interchange for any reason. Thus we can use the results in (a) to answer this
question.
In so doing we need to find the lower and upper triangular matrices after all the reduction,
but before back substitution.
It is useful to keep track of the multipliers for answering this part - LU factorisation/decomposition.
The multipliers obtained before each reduction form the subdiagonal entries of the ma-
trix L and the upper triangular matrix is extracted from the final, row reduced matrix.
7
The matrix of multipliers is
0 0 0 0
3.1416 0 0 0
M =
0.7118 −0.0457
0 0
9.8696 3.0394 −199.5021 0
So that in LU ,
1 0 0 0
3.1416 1 0 0
L = M + [ unit diagonal matrix ] =
0.7118 −0.0457
1 0
9.8696 3.0394 −199.5021 1
From the last step of row reduction in Gaussian elimination we can extract the upper tri-
angular matrix required for (c) (exclude the column corresponding to the reduced vector
b) as
3.1416 −2.7183 1.4142 −1.7321
0 11.2581 2.9463 5.8701
U =
0 0 0.1281 0.0870
0 0 0 16.7268
In solving the system Ax = LU x = b, we let U x = y and solve Ly = b, where b is the
original RHS of the system. We will then use the y to solve U x = y.
First we solve Ly = b:
1 0 0 0 y1 3.3166
3.1416 1 0 0 y2 = 0
Ly =
0.7118 −0.0457
1 0 y3 3.1416
9.8696 3.0394 −199.5021 1 y4 1.4142
Note that we can take advantage of the lower triangular matrix, and solve by forward
substitution to obtain
3.3166
−10.4194
y=
0.3047
61.1330
Now we can obtain x by solving U x = y by backward substitution:
3.1416 −2.7183 1.4142 −1.7321 x1 3.3166
0 11.2581 2.9463 5.8701 x2 −10.4194
= ,
0 0 0.1281 0.0870 x3 0.3047
0 0 0 16.726 x4 61.1330
which yields
0.6913
−2.8040
x=
−0.1038
3.6548
8
APM2613/202/0/2022
(1.3) The number of arithmetic operations can be computed using the formulas given on p371 of
the textbook:
n3 n n3 n2 5n
Multiplications/divisions: + n2 − ; Additions/subtractions: + −
3 3 3 2 6
which works out to be 62 with n = 4.
(1.4) Here we want to compare the the actual solution x = (0.6916, −2.8033, −0.1045, 3.6538) with
the the solutions obtained using the methods in (1.2). The residual vector is simply the differ-
ence between the true solution and the computed solution.
Let xG = (0.6916, ; −2.8033, −0.1045, ; 3.6538), xP = (0.6918, −2.8039, −0.1045 3.6551) and
xD = (0.6913 − 2.8040 − 0.10383.6548) be respectively the solutions obtained using the Gaus-
sian, scaled partial pivoting and decomposition methods. Then the residual vectors are
0.6916 0.6916 0
−2.8033 −2.8033 0
r G = x − xG = −0.1045 −0.1045 = 0
3.6538 3.6538 0
0.6916 0.6918 −0.0002
−2.8033 −2.8039 0.0006
rP = x − xP = −0.1045 − −0.1045 =
0
3.6538 3.6551 −0.001
0.6916 0.6913 0.0003
−2.8033 −2.8040 0.0007
r D = x − xD = −0.1045 − −0.1038 = −0.0007
(2.2) Use the inverse in (2.1) above to solve the linear systems Cx = b1 and Cx = b2 . (6)
(2.3) Find the solution of the above two systems by multiplying the matrix [b1 b2 ] by the inverse
obtained in (2.1) above. Compare the solution with that obtained in (2.2). (4)
(2.4) Solve the linear systems in (2.2) above by applying Gaussian elimination to the augmented
matrix [C : b1 b2 ]. (6)
Solution
This exercise is about solving a linear system using the inverse method. We still apply Gaussian
9
elimination in finding the inverse - not the other methods you learnt from linear algebra.
Ri = Ri − A(i, 1)/A(1, 1) ∗ R1 , i = 2, 3, 4:
We shall use option (ii) and start reduction from the bottom up.
R4 / − 1; Ri = Ri − C(i, 4)/C(4, 4) ∗ R4 , i = 1, 2, 3:
10
APM2613/202/0/2022
We shall omit this code (which changes for i-range) in subsequent calculations.
1.0000 −1.0000 2.0000 0 1.7500 −0.2500 −0.2500 −1.0000
0 1.0000 −3.0000 0 −2.5000 1.5000 0.5000 2.0000
0 0 8.0000 0 7.0000 −5.0000 −1.0000 −8.0000
0 0 0 1.0000 0.7500 −0.2500 −0.2500 −1.0000
Now that we have the identity matrix on the LHS, the RHS is the inverse of the matrix so we
can extract it as
0.1250 0.6250 0.1250 0
0.1250 −0.3750 0.1250 −1.0000
A−1 = 0.8750 −0.6250 −0.1250 −1.0000
(2.2) Having obtained the inverse we can compute the solution of the system Ax = b based on the
fact that
A−1 Ax = x = A−1 b
We multiply b, i = 1, 2 on the left by A−1 , replacing b with b1 and b2 . This yields
0.1250 0.6250 0.1250 0 6 3
0.1250 −0.3750 0.1250 −1.0000 4 = −6
x1 = 0.8750 −0.6250 −0.1250 −1.0000 −2 −2
0.7500 −0.2500 −0.2500 −1.0000 5 −1
and
0.1250 0.6250 0.1250 0 1 1
0.1250 −0.3750 0.1250 −1.0000 1
1
x2 =
0.8750 −0.6250 −0.1250 −1.0000 2
=
1
0.7500 −0.2500 −0.2500 −1.0000 −1 1
Thus we have obtained the solutions to the two systems, as expected.
11
(2.3) We could find both solutions at once by multiplying the inverse into the combined RHS vectors
as
0.1250 0.6250 0.1250 0 6 1 3 1
0.1250 −0.3750 0.1250 −1.0000
A−1 [b1 b2 ] = 4 = −6 1
1
0.8750 −0.6250 −0.1250 −1.0000 −2 2 −2 1
0.7500 −0.2500 −0.2500 −1.0000 5 −1 −1 1
where now the solutions read from the first column for the first system and the second column
for the second system.
(2.4) Instead of using Gaussian elimination on the separate systems we can simply augment both
RHS vectors to C and apply Gaussian elimination.
1 −1 2 −1 6 1
1 0 −1 1 4 1
C= ;
2 1 3 −4 −1 2
0 −1 1 −1 5 −1
R4 = R4 − C(4, 3)/C(3, 3) ∗ R3 :
1 −1 2 −1 6 1
0 1 −3 2 −2 0
C=
0 0 8 −8 −7 0
0 0 0 −1 1.2500 −1
By way of demonstrating an alternative to back substitution, we can reduce the matrix so that
the LHS is an identity matrix, starting from the bottom and working up. R4 /C(4, 4):
1 −1 2 −1 6 1
0 1 −3 2 −2 0
C=
0 0 8 −8 −8 0
0 0 0 1 −1 1
12
APM2613/202/0/2022
1 −1 2 0 5 2
0 1 −3 0 0 −2
C=
0 0 8 0 −16 8
0 0 0 1 −1 1
R3 = R3 /C(3, 3):
1 −1 2 −1 5 1
0 1 −3 2 0 0
C=
0 0 1 0 −2 1
0 0 0 1 −1 1
R2 = R2 − C(2, 3)/C(3, 3) ∗ R3 ; R1 = R1 − C(1, 3)/C(3, 3) ∗ R3 :
1 −1 0 0 9 0
0 1 0 0 −6 1
C=
0 0 1 0 −2 1
0 0 0 1 −1 1
R1 = R1 + R2 :
1 0 0 0 3 1
0 1 0 0 −6 1
C=
0 0 1 0 −2 1
0 0 0 1 −1 1
Thus the two columns on the RHS partition of the matrix give the two solutions x1 and x2
respectively.
Solution
This question is about iterative/indirect methods of solving linear systems. It will serve you well
to note the relationship between these methods. Unlike direct methods, with indirect methods you
start with a guess of the solution, followed by a series of repetitive steps (the iterations) to obtain
(hopefully) better solutions that, if the system is well poised, get closer to the expected solution.
13
(3.1) Diagonal dominance is a quick way to check if a given system is LIKELY to converge as you
carry on with iterations. A system Ax = b is likely to converge to a solution if
n
X
|aii | > |aij |
i=1,i6=j
i.e. if the magnitude of the diagonal entry in each row of the coefficient matrix is not less that
the sum of the magnitudes of the other entries in the corresponding row. So from the given
system, we have the coefficient matrix
3.1416 −2.7183 1.4142 −1.7321
9.8696 2.7183 7.3891 0.4286
2.2361 −2.4495 1.0000 −1.4142
31.0063 7.3891 −2.6458 0.1111
so that
In actual fact, the diagonal entries in each row have the smallest magnitude.
We can deduce from these inequalities that the matrix is not diagonally dominant, hence the
iterative methods are NOT likely to converge.
COMMENT:
It is important to understand the notation used in indicating the iteration count as distinguished
from mere superscripts. In the textbook, the k-th iteration of a vector x = (x1 , x2 , . . . ) is
denoted (k)
x1
(k) x(k)
x = 2
..
.
Thus the interative index/count is denoted with a bracketed superscript (k).
(3.2) Given
3.1416x1 −2.7183x2 1.4142x3 −1.7321x4 = 3.3166
9.8696x1 2.7183x2 7.3891x3 0.4286x4 = 0
2.2361x1 −2.4495x2 1.0000x3 −1.4142x4 = 3.1416
31.0063x1 7.3891x2 −2.6458x3 0.1111x4 = 1.4142
(a) Although there is a formula for the Jacobi iterative scheme given in the textbook, it is
better to understand how the system is arranged to arrive at the scheme.
14
APM2613/202/0/2022
Iteration 1:
(1) 1 (0) (0) (0)
x1 = 3.1416
[3.3166 +2.7183x2 −1.4142x3 +1.7321x4 ]
(1) 1 (0)
x2 = 2.7183
[0 −9.8696x1 −7.3891x03 −0.4286x04 ]
(1) 1 (0) (0) (0)
x3 = 1.0000
[3.1416 −2.2361x1 +2.4495x2 +1.4142x4 ]
(1) 1 (0) (0)
x4 = 0.1111
[1.4142 −31.0063x01 −7.3891x2 +2.6458x3 ]
(1) 1
x1 = 3.1416
[3.3166 +2.7183(0) −1.4142(0) +1.7321(2)] = 2.1584
(1) 1
x2 = 2.7183
[0 −9.8696(0.5) −7.3891(0) −0.4286(2)] = −2.1307
(1) 1
x3 = 1.0000
[3.1416 −2.2361(0.5) +2.4495(0) +1.4142(2)] = 4.8520
(1) 1
x4 = 0.1111
[1.4142 −31.0063(0.5) −7.3891(0) +2.6458(0) ] = −126.81
Two more iterations yield
i 0 1 2 3
x1 0.5 2.1584 −72.8897 −99.2563
x2 0 −2.1307 −1.0307 823.3169
x3 0 4.8520 −186.2433 −306.4530
x4 2 −126.8132 −332.3847 15988.3637
(b) The Gauss Seidel method is an modification of the Jacobi method by using the newly
(k+1) (k+1) (k+1) (k+1)
computed values xk .e.g. in computing x3 you will already have x1 and x2
(k)
which you use immediately, but you will still use x4 from the previous iteration. So for
the given system, the iterative scheme is
(k+1) 1 (k) (k) (k)
x1 = 3.1416
[3.3166 +2.7183x2 −1.4142x3 +1.7321x4 ]
(k+1) 1 (k+1)
x2 = 2.7183
[0 −9.8696x1 −7.3891xk3 −0.4286xk4 ]
(k+1) 1 (k+1) (k+1) (k)
x3 = 1.0000
[3.1416 −2.2361x1 +2.4495x2 +1.4142x4 ]
(k+1) (k+1) (k+1)
x4 = 1
0.1111
[1.4142 −31.0063xk+1
1 −7.3891x2 +2.6458x3 ]
Note the iterative indices (k + 1) on the RHS which indicate the values that are used
immediately. This is the only modification needed of the Jacobi iterative scheme.
COMMENT:
It is important to include these indices when you answer questions so that you will also
be reminded as to which method you are using and in which terms you are using old
and new values.
15
Iteration 1
(1) 1 (0) (0) (0)
x1 = 3.1416
[3.3166 +2.7183x2 −1.4142x3 +1.7321x4 ]
(1) 1 (1)
x2 = 2.7183
[0 −9.8696x1 −7.3891x03 −0.4286x04 ]
(1) 1 (1) (1) (0)
x3 = 1.0000
[3.1416 −2.2361x1 +2.4495x2 +1.4142x4 ]
(1) 1 (1) (1)
x4 = 0.1111
[1.4142 −31.0063x11 −7.3891x2 +2.6458x3 ]
which translates to
(1) 1
x1 = 3.1416 [3.3166 +2.7183(0) −1.4142(0) +1.7321(2)]
= 2.1584
(1) 1
x2 = 2.7183 [0 −9.8696(2.1584) −7.3891(0) −0.4286(2)]
= −8.1520
(1) 1
x3 = 1.0000 [3.1416 −2.2361(2.1584) +2.4495(−8.1520) +1.4142(2)]
= −18.8248
(1) 1
x4 = 0.1111 [1.4142 −31.0063(2.1584) −7.3891(−8.1520) +2.6458(−18.8248) ]
= −495.7690
Iteration 2
(2) 1
x1 = 3.1416
[3.3166 +2.7183(0) −1.4142(0) +1.7321(2)]
= −270.86
(2) 1
x2 = 2.7183
[0 −9.8696(−270.86) −7.3891(0) −0.4286(2)]
= 1112.8
(2) 1
x3 = 1.0000
[3.1416 −2.2361(−270.86) +2.4495(1112.8) +1.4142(2)]
= 2633.5
(2) 1
x4 = 0.1111
[1.4142 −31.0063(−270.86) −7.3891(1112.8) +2.6458(2633.5) ]
= 64312
The summary of outputs is
i 0 1 2
(i)
x1 0.5 2.1584 −270.86
(i)
x2 0 −8.1520 1112.8
(i)
x3 0 −18.825 2633.5
(i)
x4 2 −495.77 64312
We can observe from the above computations that the iterates diverge hopelessly from
the expected solution. This is not surprising since the system did NOT satisfy the diag-
onal dominance test.
(c) The SOR method is a modification of the Gauss-Seidel method by introducing the
weighting ω in the computations. Here we modify the Gauss-Seidel scheme to get SOR
16
APM2613/202/0/2022
scheme:
(k+1) (k) ω (k) (k) (k)
x1 = (1 − ω)x1 − 3.1416
[3.3166 +2.7183x2 −1.4142x3 +1.7321x4 ]
(k+1) (k) ω (k+1)
x2 = (1 − ω)x2 − 2.7183
[0 −9.8696x1 −7.3891xk3 −0.4286xk4 ]
(k+1) (k) ω (k+1) (k+1) (k)
x3 = (1 − ω)x3 − 1.0000
[3.1416 −2.2361x1 +2.4495x2 +1.4142x4 ]
(k+1) (k) (k+1) (k+1)
x4 = (1 − ω)x4 − ω
0.1111
[1.4142 −31.0063xk+1
1 −7.3891x2 +2.6458x3 ]
Note that what is inside the square brackets is exactly what the Gauss-Seidel terms (to-
(k)
gether with the iterative indices). The only modification is the additional terms (1 − ω)xi
and the numerator outside the square bracket which is now ω instead of 1.
So substituting ω = 0.4 into the scheme, we will be computing with the following iterative
scheme explicitly:
(k+1) (k) 0.4 (k) (k) (k)
x1 = 0.6x1 − 3.1416
[3.3166 +2.7183x2 −1.4142x3 +1.7321x4 ]
(k+1) (k) 0.4 (k+1)
x2 = 0.6x2 − 2.7183
[0 −9.8696x1 −7.3891xk3 −0.4286xk4 ]
(k+1) (k) 0.4 (k+1) (k+1) (k)
x3 = 0.6x3 − 1.0000
[3.1416 −2.2361x1 +2.4495x2 +1.4142x4 ]
(k+1) (k) (k+1) (k+1)
x4 = 0.6x4 − 0.4
0.1111
[1.4142 −31.0063xk+1
1 −7.3891x2 +2.6458x3 ]
Iteration 1:
(1) 0.4
x1 = 0.6(0.5) − 3.1416 [3.3166 +2.7183(0) −1.4142(0) +1.7321(2)]
= −0.5634
(1) 0.4
x2 = 0.6(0) − 2.7183 [0 −9.8696(−0.5634) −7.3891(0) −0.4286(0)]
= −0.6920
(1) 0.4 (0)
x3 = 0.6(0) − 1.0000 [3.1416 −2.2361(−0.5634) +2.4495(−0.6920) +1.4142x4 ]
= −2.2138
(1) 0.4
x4 = 0.6(2) − 0.1111 [1.4142 −31.0063(−0.5634) −7.3891(−0.6920) +2.6458(−2.2138)]
= −64.1032
Iteration 2
(2) 0.4
x1 = 0.6(−0.5634) − 3.1416 [3.3166 +2.7183(0) −1.4142(0) +1.7321(2)]
= 13.218
(2) 0.4
x2 = 0.6(0) − 2.7183 [0 −9.8696(13.218) −7.3891(0) −0.4286(0)]
= 12.331
(2) 0.4 (0)
x3 = 0.6(0) − 1.0000 [3.1416 −2.2361(13.218) +2.4495(12.331) +1.4142x4 ]
= 33.417
(2) 0.4
x4 = 0.6(2) − 0.1111 [1.4142 −31.0063(13.218) −7.3891(12.331) +2.6458(33.417)]
= 1441.7
17
Hence the summary of outputs for two iterations is
i 0 1 2
(i)
x1 0.5 −0.56336 13.218
(i)
x2 0 −0.69204 12.331
(i)
x3 0 −2.2138 33.417
(i)
x4 2 −64.103 1441.7
(3.3) The divergence of the iterations does not justify the computation of the residual vectors.
However, looking at the approximations resulting after two iterations, we can observe that
although divergent, the magnitude of the approximate solution vectors is worse with Jacobi
method and smallest with the SOR method.
5x21 = x22
x2 − 0.25(sin x1 + cos x2 ) = 0
(4.1) Draw a sketch of the two functions on the same axes. (5)
(4.2) Write down a computer code for Newton’s method for more than one variable to solve the
above system. (8)
(4.3) Use the algorithm in (4.2) to solve the given system of nonlinear equations with x(0) = (0, 1)T
as the initial guess of the solution. (7)
Solution
(4.1) Plotting two variable functions is possible with Matlab/Octave. Because we want the proper
root equations f (x, y) = 0for the system, let
To plot these in Matlab/Octave first we define the domains of x andy. Then define the func-
tions z and h. Then plot. For a simple plot the series of commands is
18
APM2613/202/0/2022
Try these on Matlab/Octave to see what the graphs look like. There are only two commands
you need, meshgrid and mesh.
(4.2) In this exercise you need to write a code for the algorithm for solving coupled non-linear equa-
tions and use the code to produce some output.
In the case of a single variable, Newton’s method is applied to an root equation written in the
form f (x) = 0 to find a solution x. In a similar manner, now the system of nonlinear equations
has to be in the form
f1 (x1 , x2 ) = 0
f1 (x1 , x2 )
= F (x1 , x2 ) =
f2 (x1 , x2 )
f2 (x1 , x2 ) = 0
Unlike in the case of nonlinear equations involving only one variable, Newton’s method here
replaces the derivative with the Jacobian (Don’t try to solve for one variable in terms of the
other): Instead of
f (x(k) )
x(k+1) = x(k) − 0
f (x(k) )
the iterative scheme is now
" # " #
(k+1) (k)
x1 x1 (k) (k) (k) (k)
(k+1) = (k) − J −1 (x1 , x2 )F (x1 , x2 )
x2 x2
f1 (x1 , x2 )
where F (x1 , x2 ) = and J is the Jacobian matrix.
f2 (x1 , x2 )
19
Manually, the inverse of the Jacobian can be evaluated as follows:
sin x2 1
det(J) = |J| = 10x1 (1 + ) − x2 cos x1
4 2
so that
sin x2
−1 1 1+ 4
2x2
J (x1 , x2 ) = cos x1
det(J) 4
10x1
Once these have been computed we get the iterative scheme
" # " #
(k+1) (k)
x1 x1 (k) (k) (k) (k)
(k+1) = (k) − J −1 (x1 , x2 )F (x1 , x2 ) (1)
x2 ) x2
which is explicitly
" # " (k) #" #
(k+1) sin x (k) 2(k) 2(k)
x1 1 1 + 42 2x2 5x1 − x2
(k+1) = (k) (k) (k) (k) (k) (k)
x2 det(J(x1 , x2 ) cos x1
10x1
(k) x2 − 0.25(sin x1 + cos x2 )
4
where
sin x2 1
det(J) = 10x1 (1 + ) − x2 cos x1
4 2
So in an typical algorithm code you need to define the vector function F (x1 , x2 ), the Jacobian
J(x1 , x2 ) and then compute the inverse J −1 (x1 , x2 ) and then combine these in the iterative
scheme. The computation of the inverse may look complex (which it may), but fortunately the
matrix inverse is one of the built-in functions of Matlab/Octave. So we can take advantage of
this built-in function in our code.
The pseudo code in the textbook is based on rearranging the iterative scheme to the equiva-
lent form " # " #!
(k+1) (k)
(k) (k) x 1 x 1 (k) (k)
J(x1 , x2 ) (k+1) − (k) = −F (x1 , x2 ),
x2 x2
with the notation " #" # " #
(k) (k+1) (k)
y1 x1 x1
y (k) = (k) (k+1) − (k)
y2 x2 x2
or
y (k) = x(k+1) − x(k)
, from which the next iterate is computed as
20
APM2613/202/0/2022
(iii) By letting y (k) = x(k+1) − x(k) , solve and compute solve the equation J −1 y = −F at
(k) (k)
(x1 , x2 )
(iv) Compute the next iterate as x(k+1) = x(k) + y (k)
(v) Repeat steps (ii)-(iv) using x(k+1) instead of x(k) .
Given below is a code for the iterative scheme in (1). The basic code (with no stopping
criterion) is:
(4.3) The output obtained for the given x(0) = (0, 1)T and vector function F is:
k 0 1 2
(k)
x1 0 1.0390 0.5324
(k)
x2 1 0.50000.3845
A few more iterations (by adjusting N) yielded the following output for N = 9
(k) (k)
k x1 x2
0 0 1
1 1.0390 0.5000
2 0.5324 0.3845
3 0.2835 0.3118
4 0.1697 0.2828
5 0.1287 0.2729
6 0.1215 0.2712
7 0.1212 0.2711
8 0.1212 0.2711
9 0.1212 0.2711
from which we observe that Newton’s method converges to the approximate solution (x1 , x2 )T =
(0.1212, 0.2711)T correct to 4 decimal places after 8 iterations.
Slight modifications to the code to accommodate specifications of a specific accuracy can be
done by including an ’if...then’ or ’while ...’ conditional statements.
21