0% found this document useful (0 votes)
13 views5 pages

Practical 5 JacobiSeidel

The document discusses the Gauss-Jacobi and Gauss-Seidel iterative methods for solving systems of linear equations. It provides examples applying each method to solve systems, specifying the initial vector and number of iterations. The examples output the results of each iteration.

Uploaded by

Balpreet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

Practical 5 JacobiSeidel

The document discusses the Gauss-Jacobi and Gauss-Seidel iterative methods for solving systems of linear equations. It provides examples applying each method to solve systems, specifying the initial vector and number of iterations. The examples output the results of each iteration.

Uploaded by

Balpreet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical 5

Gauss Jacobi and Gauss Seidel Method


Gauss -Jacobi Iteration Method: A general linear iterative method for the solution of the system of
equations Ax = b may be defined of the form :
x ( k + 1) = H x ( k ) + C
where , H = - D-1 (L+U)
C = D- 1 b
where, D = diagonal matrix
L = lower triangular matrix
U = upper triangular matrix

Gauss Jacobi method with number of iterations as stopping criteria:


Q1. Use the Gauss Jacobi iteration method to solve the system of equations
2 x1 - x2 + 0 x 3 = 7
- x1 + 2 x 2 - x3 = 1
0 x1 - x2 +2 x3 = 1
with the inital vector x(0) = (0,0,0). Perform 12 iterations.
2 Practical 5 JacobiSeidel.nb

GaussJacobi[A0_, B0_, X0_, max_] :=


Module{A = N[A0], B = N[B0], i, j, k = 0, n = Length[X0], X = X0, Xk = X0},
Print["X" 0 , "=", X];
Whilek < max,

For i = 1, i ≤ n, i ++,

1 n
X[[i]] = B[[i]] + A[[i,i]] Xk[[i]] -  A[[i,j]] Xk[[j]] ;
A[[i,i]] j=1

Print["X"k+1 , "=", X];


Xk = X;
k = k + 1;;
Print[" No. of iterarations performed ", max];
Return[X];;
2 -1 0
A = -1 2 -1 ;
0 -1 2
7
B = 1 ;
1
0
X0 = 0 ;
0
GaussJacobi[A, B, X0, 12]

X0 ={{0}, {0}, {0}}

X1 ={{3.5}, {0.5}, {0.5}}

X2 ={{3.75}, {2.5}, {0.75}}

X3 ={{4.75}, {2.75}, {1.75}}

X4 ={{4.875}, {3.75}, {1.875}}

X5 ={{5.375}, {3.875}, {2.375}}

X6 ={{5.4375}, {4.375}, {2.4375}}

X7 ={{5.6875}, {4.4375}, {2.6875}}

X8 ={{5.71875}, {4.6875}, {2.71875}}

X9 ={{5.84375}, {4.71875}, {2.84375}}

X10 ={{5.85938}, {4.84375}, {2.85938}}

X11 ={{5.92188}, {4.85938}, {2.92188}}

X12 ={{5.92969}, {4.92188}, {2.92969}}

No. of iterarations performed 12

{{5.92969}, {4.92188}, {2.92969}}

Q2. Solve the system of equations


4 x1 +x2 + x3 = 2
Practical 5 JacobiSeidel.nb 3

x1 + 5 x2 +2 x3 = -6
x1 + 2 x2 +3 x3 = -4
with the inital vector x (0) = (0.5,-0.5,-0.5). Perform 15 iterations.

A = {{4, 1, 1}, {1, 5, 2}, {1, 2, 3}};


B = {2, - 6, - 4};
X0 = {0.5, - 0.5, - 0.5};
GaussJacobi[A, B, X0, 15]
X0 ={0.5, -0.5, -0.5}

X1 ={0.75, -1.1, -1.16667}

X2 ={1.06667, -0.883333, -0.85}

X3 ={0.933333, -1.07333, -1.1}

X4 ={1.04333, -0.946667, -0.928889}

X5 ={0.968889, -1.03711, -1.05}

X6 ={1.02178, -0.973778, -0.964889}

X7 ={0.984667, -1.0184, -1.02474}

X8 ={1.01079, -0.987037, -0.982622}

X9 ={0.992415, -1.00911, -1.01224}

X10 ={1.00534, -0.993588, -0.9914}

X11 ={0.996247, -1.00451, -1.00605}

X12 ={1.00264, -0.996828, -0.995744}

X13 ={0.998143, -1.00223, -1.00299}

X14 ={1.00131, -0.998431, -0.997894}

X15 ={0.999081, -1.0011, -1.00148}

No. of iterarations performed 15

{0.999081, - 1.0011, - 1.00148}

Gauss-Seidel Method
Gauss - Seidel Iteration Method: A general linear iterative method for the solution of the system of
equations Ax = b may be defind of the form.
x ( k + 1) = H x ( k ) + C
where x (k+1) and x (k) are approximations of x in the (k+1)th and kth iterations.
H is called the iteration matrix of A and C is called column vector.
Here, H = - D-1 (L+U)
C = (D + L)-1 b
where, D = diagonal matrix
L = lower triangular matrix
U = upper triangular matrix .

Gauss Seidel method with number of iterations as stopping criteria:


Q. Use the Gauss Seidel iteration method to solve the system of equations in 10 iterations
2 x1 - x 2 + 0 x 3 = 7
4 Practical 5 JacobiSeidel.nb

- x 1 + 2 x 2 - x3 = 1
0 x1 - x2 +2 x3 = 1
with the inital vector x (0) = (0,0,0).

GaussSeidel[A0_, B0_, X0_, max_] :=


Module{A = N[A0], B = N[B0], i, j, k = 0, n = Length[X0], X = X0, Xk = X0},
Print["X"0 , "=", X];
Whilek < max,

For i = 1, i ≤ n, i ++,

1 n
X[[i]] = B[[i]] + A[[i,i]] X[[i]] -  A[[i,j]] X[[j]] ;
A[[i,i]] j=1

Print["X"k+1 , "=", X];


Xk = X;
k = k + 1;;
Print["No. of iterations performed ", k];
Return[X];;
A = {{2, - 1, 0}, {- 1, 2, - 1}, {0, - 1, 2}};
B = {7, 1, 1};
X0 = {0, 0, 0};
GaussSeidel[A, B, X0, 10]

X0 ={0, 0, 0}

X1 ={3.5, 2.25, 1.625}

X2 ={4.625, 3.625, 2.3125}

X3 ={5.3125, 4.3125, 2.65625}

X4 ={5.65625, 4.65625, 2.82813}

X5 ={5.82813, 4.82813, 2.91406}

X6 ={5.91406, 4.91406, 2.95703}

X7 ={5.95703, 4.95703, 2.97852}

X8 ={5.97852, 4.97852, 2.98926}

X9 ={5.98926, 4.98926, 2.99463}

X10 ={5.99463, 4.99463, 2.99731}

No. of iterations performed 10

{5.99463, 4.99463, 2.99731}

Q2. Solve the system of equations


4 x1 +x2 + x3 = 2
x1 + 5 x2 +2 x3 = -6
x1 + 2 x2 +3 x3 = -4
with the inital vector x (0) = (0.5,-0.5,-0.5). Perform 11 iterations.
Practical 5 JacobiSeidel.nb 5

A = {{4, 1, 1}, {1, 5, 2}, {1, 2, 3}};


B = {2, - 6, - 4};
X0 = {0.5, - 0.5, - 0.5};
GaussSeidel[A, B, X0, 11]

X0 ={0.5, -0.5, -0.5}

X1 ={0.75, -1.15, -0.816667}

X2 ={0.991667, -1.07167, -0.949444}

X3 ={1.00528, -1.02128, -0.987574}

X4 ={1.00221, -1.00541, -0.997129}

X5 ={1.00064, -1.00128, -0.999362}

X6 ={1.00016, -1.00029, -0.999862}

X7 ={1.00004, -1.00006, -0.999971}

X8 ={1.00001, -1.00001, -0.999994}

X9 ={1., -1., -0.999999}

X10 ={1., -1., -1.}

X11 ={1., -1., -1.}

No. of iterations performed 11

{1., - 1., - 1.}

You might also like