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

Gauss

Report on Gauss method
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 views2 pages

Gauss

Report on Gauss method
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/ 2

Gauss-Seidel method for solving Ax = b

% Parameters that are used in Gauss-Seidel method :

%A - Coefficient matrix

%b - Right-hand side vector

% x0 - Initial guess for the solution

% tol - Tolerance for convergence

% max_iter - Maximum number of iterations

%%%% initialization %%%%%%%

A = [3 -0.1 -0.2;

0.1 7 -0.3;

0.3 -0.2 10];

b = [7.85; -19.3; 71.4];

x0 = [0; 0; 0];

tol = 1e-6;

max_iter = 10;

if n ~= m

error('Matrix A must be square.');

end

% Initialize solution vector

x = x0;

for iter = 1:max_iter

x_old = x;

% Perform Gauss-Seidel iteration

for i = 1:n

% Compute the sum for current row

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

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

x(i) = (b(i) - sum1 - sum2) / A(i, i);

end
% Display current solution

fprintf('%d\t', iter);

fprintf('%.6f ', x);

fprintf('\n');

% Check for convergence

if norm(x - x_old, inf) < tol

fprintf('Converged to solution after %d iterations.\n', iter);

return;

end

end

fprintf('Maximum iterations reached. Approximate solution:\n');

end

You might also like