Question

Problem 2 [25 points] (Coding, pen and paper) Write the code to perform Jacobi and Gauss- Seidel methods for solving the line

0 0
Add a comment Improve this question Transcribed image text
Answer #1

%%%%%%%%%% Note: Problem 1 you have not given so I have taken one example problem


function xrt=gaus_jocob_Sidel()
clc;
clear all;

% 10x1 + x2 - x3 = 18
%
% x + 15x2 + 2x3 = -12
%
% -x1 + x2 + 20x3 = 17

A = [6 -1 2 1 ; 1 6 1 -1;0 1 3 1;1 -2 1 5];
b = [3; 2; -6;1];
n=length(b);
% error tolerance

disp('Exact solution by Matirx inverse')
y=inv(A)*b %%%Exact


%initial guess:
x0 = ones(n,1);
tol=1e-6; % Stoping (tolerence)
iter=1;

disp(' Solution, number of iteration and error by Gauss Jacobi method')
[x_jacobian, iter, err1]=gausjocobi(A,b,x0,tol)
disp(' Solution, number of iteration and error by Gauss Seidel method')
[x_gauss, iter, err1]=gausssidel(A,b,x0,tol)

% [x_Sor, iter]=Sor(A,b,x0,tol)
function [x_jacobian, iter, err1]=gausjocobi(A,b,x0,tol)

% Jacobi method
%---------------

%A is matrx
%b is right side matrix
%x0 is inital vector
%tol is tolerence


xnew=x0;
iter=1;
error=1;
while (error>tol )
xold=xnew;

for i=1:length(xnew)
off_diag = [1:i-1 i+1:length(xnew)];
xnew(i) = 1/A(i,i)*( b(i)-sum(A(i,off_diag)*xold(off_diag)) );
end

error=norm(xnew-xold);
err1(iter)=error;
iter = iter+1;
end
x_jacobian=xnew;

end


function [x_gauss, iter, err1]=gausssidel(A,b,x0,tol)
xnew=x0;
iter=1;
err=1;
while (err>tol)
lambda=1;
xold=xnew;
for i=1:n
I = [1:i-1 i+1:n];
xnew(i) = (1-lambda)*xnew(i)+lambda/A(i,i)*( b(i)-A(i,I)*xnew(I) );
end
err = norm(xnew-xold);
err1(iter)=err;
iter = iter+1;
  
end
x_gauss=xnew;
end


end

%%%%%%%% Solution

Exact solution by Matirx inverse

y =

1.305429864253394
0.635746606334842
-2.438914027149322
0.680995475113122

Solution, number of iteration and error by Gauss Jacobi method

x_jacobian =

1.305429958624094
0.635746583563940
-2.438914093841731
0.680995535312764


iter =

16


err1 =

Columns 1 through 3

3.933615809065920 1.574193895221642 0.486147264969486

Columns 4 through 6

0.175559380981398 0.056338328280127 0.019173026432052

Columns 7 through 9

0.006157919146378 0.002042336090436 0.000629963356748

Columns 10 through 12

0.000208036056003 0.000059033489543 0.000019697795727

Columns 13 through 15

0.000004870769552 0.000001681352088 0.000000334066886

Solution, number of iteration and error by Gauss Seidel method

x_gauss =

1.305429870055986
0.635746615687181
-2.438914040669446
0.680995480397565


iter =

12


err1 =

Columns 1 through 2

3.609349522843711 1.127507478402957

Columns 3 through 4

0.094708804346742 0.016763100058599

Columns 5 through 6

0.005555852012273 0.001033868860446

Columns 7 through 8

0.000112855725824 0.000015284172615

Columns 9 through 10

0.000005320753452 0.000001066916329

Column 11

0.000000125915926

>>

Add a comment
Know the answer?
Add Answer to:
Problem 2 [25 points] (Coding, pen and paper) Write the code to perform Jacobi and Gauss- Seidel ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT