Gauss – Seidel Method with MATLAB | MATLAB Tutorial

Gauss – Seidel Method with MATLAB | MATLAB Tutorial

Iterative Methods for Linear Systems

Gauss – Seidel

The Gauss-Seidel method is the most commonly used iterative method for solving linear algebraic equations. Assume that we are given a set of n equations:

[A]{x} = {b}

Suppose that for conciseness we limit ourselves to a 3 × 3 set of equations. If the diagonal elements are all nonzero, the first equation can be solved for x1, the second for x2, and the third for x3 to yield

where j and j − 1 are the present and previous iterations.

To start the solution process, initial guesses must be made for the x’s. A simple approach is to assume that they are all zero. These zeros can be substituted into Eq. 1, which can be used to calculate a new value for x1 = b1/a11. Then we substitute this new value of x1 along with the previous guess of zero for x3 into Eq. 2 to compute a new value for x2. The process is repeated for Eq. 3 to calculate a new estimate for x3. Then we return to the first equation and repeat the entire procedure until our solution converges closely enough to the true values. Convergence can be checked using the criterion that for all i,

Problem 1: Use the Gauss -Seidel method to obtain the solution for

Note that the solution is x1=3, x2=-2.5 , and x3=7.

First, solve each of the equations for its unknown on the diagonal:

By assuming that x2 and x3 are zero,

This value, along with the assumed value of x3 = 0,

The first iteration is completed by substituting the calculated values for x1 and x2,





For the second iteration, the same process is repeated to compute

The method is, therefore, converging on the true solution. Additional iterations could be applied to improve the answers. However, in an actual problem, we would not know the true answer a priori. Consequently, Eq. 4 provides a means to estimate the error. For example, for x1:

For x2 and x3, the error estimates are εa,2 = 11.8% and εa,3 = 0.076%.

As each new x value is computed for the Gauss-Seidel method, it is immediately used in the next equation to determine another x value. Thus, if the solution is converging, the best available estimates will be employed. An alternative approach, called Jacobi iteration, utilizes a somewhat different tactic. Rather than using the latest available x’s, this technique uses Eqs.1,2,3 to compute a set of new x’s on the basis of a set of old x’s. Thus, as new values are generated, they are not immediately used but rather are retained for the next iteration.

The difference between the Gauss-Seidel method and Jacobi iteration is depicted in Fig. 1. Although there are certain cases where the Jacobi method is useful, Gauss-Seidel’s utilization of the best available estimates usually makes it the method of preference.

Figure 1: Graphical depiction of the difference between (a) the Gauss-Seidel and (b) the Jacobi iterative methods for solving simultaneous linear algebraic equations.

 Matlab m-file:

function x = GaussSeidel(A,b,es,maxit)

% input:

% A = coefficient matrix

% b = right hand side vector

% es = stop criterion (default = 0.00001%)

% maxit = max iterations (default = 50)

% output:

% x = solution vector

if(nargin==2)
 error('at least 2 input arguments required');
end

if(nargin==4 ||isempty(maxit))
 maxit=50;
end

if(nargin==3 ||isempty(es))
 es=0.00001;
end

[m,n] = size(A);

if(m~=n)
 error('Matrix A must be square');
end

C = A;

for i = 1:n
 
 C(i,i) = 0;
 
 x(i) = 0;
 
end

x = x';

for i = 1:n
 
 C(i,1:n) = C(i,1:n)/A(i,i);
 
end

for i = 1:n
 
 d(i) = b(i)/A(i,i);
 
end

iter = 0;

while(1)
 
 xold = x;
 
 for i = 1:n
 
 x(i) = d(i)-C(i,:)*x;
 
 if(x(i) ~= 0)
 
 ea(i) = abs((x(i) - xold(i))/x(i)) * 100;
 
 end
 
 end
 
 iter = iter+1;
 
 if(max(ea)==es || iter == max)
 break;
 end
 
end

The M-file can be used to solve the problem 1.

>> A = [3 -.1 -.2;.1 7 -.3;.3 -.2 10];

>> b = [7.85; -19.3; 71.4];

>> GaussSeidel(A,b);

x1 = 3.000000

x2 = -2.500000

x3 = 7.000000

[/code]


I am a Certified MATLAB Associate. I love MATLAB Coding & Enjoy Programming. I can make anything & everything possible with MATLAB.