Thursday, March 23, 2023

Gauss-Seidel for solve systems of linear equation using MATLAB solver

 Gauss-Seidel method

Gauss-Seidel is an iterative method used to solve systems of linear equations. It is named after the German mathematicians' Carl Friedrich Gauss and Philipp Ludwig von Seidel.

The Gauss-Seidel method is an iterative technique that starts with an initial guess for the solution of the system of equations and then improves that guess by updating each component of the solution vector using the current values of the other components. This process is repeated until a desired level of accuracy is achieved.

The algorithm for the Gauss-Seidel method is as follows:

  1. Start with an initial guess for the solution vector x.
  2. For each equation in the system, use the current values of the other components of x to solve for the next component of x.
  3. Update the value of the current component of x with the value obtained in step 2.
  4. Repeat steps 2 and 3 for all components of x until the desired level of accuracy is achieved.

The iteration formula for Gauss-Seidel can be written as:

x_i^{(k+1)} = (b_i - \sum_{j=1}^{i-1} a_{ij}x_j^{(k+1)} - \sum_{j=i+1}^{n} a_{ij}x_j^{(k)})/a_{ii}

where i = 1,2,...,n and k is the iteration number.

The convergence of the Gauss-Seidel method depends on the properties of the coefficient matrix of the system of equations. In general, the method converges if the matrix is diagonally dominant, which means that the absolute value of each diagonal element is greater than or equal to the sum of the absolute values of the other elements in the same row.

The Gauss-Seidel method can be computationally efficient for solving large systems of linear equations, particularly if the coefficient matrix is sparse. However, the method may not converge for all systems of equations and may converge slowly for some systems. Let us summarize and solve an example. In the following, the Matlab code is written based on the methodology. After that, a typical result is obtained based on the stoping criteria. IN the example the stoping criteria are the maximum error and maximum iteration.


 
Maximum error =1e-6;


=====================================================================

%Gauss-Seidel using Matlab ===> the given expression is diagonally dominant
% Define the coefficient matrix A and the constant vector b
A = [3 -0.1 -0.2; 0.1 7 -0.3; 0.3 -0.2 10];
b = [7.25; -19.3; 71.4];
% Choose an initial guess for x and set the tolerance and maximum number of iterations
x0 = [0; 0; 0];
tol = 1e-6;
max_iter = 100;
% Initialize the solution vector x, the iteration counter iter, and the error measure error
x = x0;
iter = 0;
error = tol + 1;
fprintf('iter\t\tx(1)\t\tx(2)\t\tx(3)\t\terror\n'); % for show
% Implement the Gauss-Seidel algorithm
while error > tol && iter < max_iter
% Save the old value of x for error calculation
x_old = x;
% Calculate the new value of x(i) using the Gauss-Seidel formula
x(1) = (b(1) - A(1,2)*x_old(2) - A(1,3)*x_old(3)) / A(1,1);
x(2) = (b(2) - A(2,1)*x(1) - A(2,3)*x(3)) / A(2,2);
x(3) = (b(3) - A(3,1)*x(1) - A(3,2)*x(2)) / A(3,3);
% Calculate the relative error between the old and new values of x
error = norm(x - x_old) / norm(x);
% Increment the iteration counter
iter = iter + 1;
% Now let us see each step of the iteration
fprintf('%f\t%f\t%f\t%f\t%f\n',iter,x(1),x(2),x(3),error);
end
% Display the solution and the number of iterations
fprintf('Solution: x =\n');
disp(x);
fprintf('Number of iterations: %d\n', iter);

Here is the result of the computation:

=================================================

Gauss_Seidel

iter x(1) x(2) x(3) error
1.000000 2.416667 -2.791667 7.011667 1.000000
2.000000 2.791056 -2.496515 7.006338 0.060014
3.000000 2.800539 -2.496879 7.006046 0.001195
4.000000 2.800507 -2.496891 7.006047 0.000004
5.000000 2.800507 -2.496891 7.006047 0.000000
Solution: x =
2.8005
-2.4969
7.0060

Number of iterations: 5
 

No comments:

Post a Comment