0% found this document useful (0 votes)
24 views3 pages

GaussSeidelMethod - Copy

Matlab Code with Explaination of Gauss Seidal Method
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views3 pages

GaussSeidelMethod - Copy

Matlab Code with Explaination of Gauss Seidal Method
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

MATLAB Code for Gauss-Seidel Method

The Gauss-Seidel method is an iterative technique used to solve systems of linear equations.
Here's the MATLAB implementation with detailed explanations:

Code:
% Gauss-Seidel Method in MATLAB

% Define the coefficient matrix (A) and the right-hand side vector (b)

A = [4, -1, 0, 0;

-1, 4, -1, 0;

0, -1, 4, -1;

0, 0, -1, 3]; % Example 4x4 system

b = [15; 10; 10; 10]; % Right-hand side vector

% Initial guess for the solution

x0 = zeros(size(b)); % Starting with a zero vector

% Convergence criteria

tol = 1e-6; % Tolerance level for convergence

max_iter = 100; % Maximum number of iterations

% Number of equations

n = length(b);

% Gauss-Seidel Iteration

x = x0; % Initialize the solution vector

iter = 0; % Initialize iteration counter

while iter < max_iter

x_old = x; % Store the previous solution

for i = 1:n

% Calculate the sum excluding the current variable x(i)

sum1 = A(i, 1:i-1) * x(1:i-1); % Sum of terms with updated x values

sum2 = A(i, i+1:n) * x_old(i+1:n); % Sum of terms with old x values

x(i) = (b(i) - sum1 - sum2) / A(i, i); % Update the solution

end
% Check for convergence

if norm(x - x_old, inf) < tol

break;

end

iter = iter + 1; % Increment iteration counter

end

% Display the results

if iter == max_iter

fprintf('Gauss-Seidel did not converge within %d iterations.\n', max_iter);

else

fprintf('Solution converged in %d iterations.\n', iter);

disp('Solution:')

disp(x)

end

Wokplace of the Code: Output of the Code:

Code Explaination:
Input Matrix Definition:
A = [ ... ]; % Coefficient matrix
b = [ ... ]; % Right-hand side vector
A is the matrix of coefficients.
b is the vector of constants in the system of equations Ax=b.

Initial Guess:
x0 = zeros(size(b));
Initializes the solution vector with zeros.
Convergence Criteria:
tol = 1e-6; % Tolerance for convergence
max_iter = 100; % Maximum number of iterations
Specifies how close the solution should be before stopping.
Sets a limit on the maximum number of iterations.

Iterative Gauss-Seidel Process:


for i = 1:n
sum1 = A(i, 1:i-1) * x(1:i-1); % Sum of terms with updated x values
sum2 = A(i, i+1:n) * x_old(i+1:n); % Sum of terms with old x values
x(i) = (b(i) - sum1 - sum2) / A(i, i); % Update the solution
end
Splits the terms involving x(i) into parts with updated and old values.
Updates the solution for each variable sequentially.
Convergence Check:
if norm(x - x_old, inf) < tol
break;
end
Stops the iterations if the solution changes less than the tolerance.
Output Results:
Displays the solution and the number of iterations it took to converge.
fprintf('Solution converged in %d iterations.\n', iter);
disp('Solution:')
disp(x)

You might also like