0% found this document useful (0 votes)
30 views

Statistics Gauss-Siedel Examples

This document discusses and provides code examples for implementing the Gauss-Seidel and Jacobi methods for solving systems of linear equations in MATLAB. The Gauss-Seidel method iteratively updates values by using the most recent estimates, while the Jacobi method updates values using only the previous estimates. Both methods iterate until the change between iterations is less than a threshold.

Uploaded by

sandaru14malinga
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)
30 views

Statistics Gauss-Siedel Examples

This document discusses and provides code examples for implementing the Gauss-Seidel and Jacobi methods for solving systems of linear equations in MATLAB. The Gauss-Seidel method iteratively updates values by using the most recent estimates, while the Jacobi method updates values using only the previous estimates. Both methods iterate until the change between iterations is less than a threshold.

Uploaded by

sandaru14malinga
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/ 1

Gauss Seidel and Jacobi Method in MATLAB

>> D = diag( A ).^-1


D=
0.2500
0.1250
0.0833
>> diag( diag( A ) )
ans =
4 0 0
0 8 0
0 0 12
>> A1 = A - diag( diag( A ) )
A1 =
0 -1 -1
6 0 0
-5 0 0

%A = [4 -1 -1;6 8 0;-5 0 12];


%b = [-2 45 80]'
function x=GaussSeidelMethod(A,b)
D = diag( A ).^-1;
diag( diag( A ) );
A1 = A - diag( diag( A ) );
x = [0, 0, 0]';
for i = 1:50
xold = x;
for j = 1:3
x(j) = D(j)*(b(j) - A1(j, :)*x);
end
if (norm( x - xold ) /norm(x))< 0.001
break;
end
end

%A = [4 -1 -1;6 8 0;-5 0 12];


%b = [-2 45 80]';
function x=JacobiMethod(A,b)
D = diag( A ).^-1;
A1 = A - diag( diag( A ) );
x = [0, 0, 0]';
for i=1:50
xn = D.*(b - A1*x);

% multiply element-wise

if (norm( xn - x)/norm(xn)) < 0.001


break;
end
x = xn;
end

You might also like