Lab 1
Lab 1
Objective:
The aim of this tutorial is to implement the iterative Jacobi and Gauss-Seidel methods
for solving a system of linear equations (SLE). You’ll need the numpy and matplotlib
modules. Load these modules by typing the following commands:
import numpy as np
import matplotlib.pyplot as plt
1 Course reminders
1.1 Iterative methods for solving a SLE
Iterative methods consist in having a recurrent sequence of vectors, which converges to-
wards the solution of the SLE. This solution is given once a stopping condition imposed
on the algorithm has been satisfied.
1.1.1 Principle
Consider the SEL (S): AX = b with:
a11 a12 a13 · · · a1n x1 b1
a21
a22 a23 · · · a2n
x2
b2
A = a31
a32 a33 · · · a3n , X = x3 et b = b3
.
.. .. .. .. .. .. ..
. . . . . . .
an1 an2 an3 · · · ann xn bn
It is assumed that (S) is Cramer. Iterative methods are based on a decomposition of A into
the form A = M − N, where M is an invertible matrix. A recurrent sequence of solutions
1
X (k) , k ≥ 0, is then generated as follows:
X ∈ R given
(0) n
with X (0) an initial vector. This sequence converges, under certain conditions, to the exact
solution X of system (S). Examples: Jacobi, Gauss-Seidel...
A = D−E−F
1.1.3 Convergence
If the matrix A has a strictly dominant diagonal, then the Jacobi and Gauss-Seidel methods
converge whatever the choice of initial vector X (0) .
|| AX (k) − b|| ≤ ε.
2
2 Numerical applications
Let n ≥ 4 and ai ∈ R for i = 1, 2, 3. We are interested in the numerical solution of the
problem (Sn ): AX = b with
a1 a2 0 ... ... 0
.. ..
x1
2
a3 a1 a2 . .
.. .
x2 2
. ..
0 a3 a1 a2
A = A ( a1 , a2 , a3 ) = , X = .. and b = ... .
.. .. .. .. .. .
. . . . . 0 x n −1
2
..
.. .. .. xn 2
. . . a2
.
0 . . . . . . 0 a3 a1
(c) Using the function np.linalg.solve( B1 , b), solve (S10 ). and find the Jacobi error
in Euclidean norms.
3
4. (a) write a function gauss_seidel(B, b1, X0, epsilon) which returns an ap-
proximate solution of the SEL BX = b1 by the Gauss-Seidel method and the
number of iterations performed to reach convergence by testing whether A is
strictly diagonal dominant.
(b) Test the Gauss-Seidel function for the same example considered in 3.b). Then
find the error in Euclidean norm.
5. Compare the results obtained by the two methods.
6. Plot on the same graph the number of iterations by the two iterative methods: Jacobi
and Gauss-Seidel, as a function of the accuracy epsilon. We consider epsilon ∈
{10−3 , 10−6 , 10−9 , 10−12 } and n = 10. Interpret the obtained results.
7. Let Niter(epsilon) be the function defined as follows
def Niter(epsilon):
N=np.arange(5,31,5)
Niter_J=[]
Niter_GS=[]
for n in N:
Ab=tridiag(4,1,1,n)
X0=np.zeros((n,1))
J=jacobi(Ab[0],Ab[1],X0,epsilon)
GS=gauss_seidel(Ab[0],Ab[1],X0,epsilon)
Niter_J.append(J[1])
Niter_GS.append(GS[1])
return Niter_J, Niter_GS
Test this function for epsilon=10−6 . Explain the obtained results.