Please help me transform Jacobi into Gauss-Seidel
7 views (last 30 days)
Show older comments
Hello everybody...
To be honest, I am taking a linear algebra class (where the professor teaches and uses Matlab quite a bit). Not all of us took Matlab (but C programming, and long ago at that) and are at a big disadvantage trying to do his assignment. None of the other Linear Algebra professors use Matlab like he does, I figure, because it is one of his specialties.
Myself and a few fellow students were able to get this follow Jacobi program to run and work correctly... But now, we must transform into Gauss-Seidel and need help.
I honestly have no idea what I am doing... It also mentions something about Convergence Criteria and Relaxation in the Conversion, but not sure exactly what he means. He was poor at explaining.
I appreciate your help, very much. Information below.
-------------------------------
This was written to solve the following equations:
1.) 3X1 - 0.1X2 - 0.2X3 = 7.85
2.) 0.1X1 + 7X2 - 0.3X3 = -19.3
3.) 0.3X1 - 0.2X2 + 10X3 = 71.4
And Solutions are:
X1 = 3 X2 = -2.5 X3 = 7
------------------------
n=input('Dimension of system= ');
a=input('Coefficent matrix= ');
b=input('Solution vector= ');
x=input('Starting vector= ');
epsilon=input('Convergence criterion= ');
itmax=input('Maxinmum number of iterations= ');
%----------------------------------------------------
%Outer loop for Jacobi-iteration
maxerr=100;
it=0;
while maxerr>epsilon && it<itmax;
it = it+1;
xold=x;
%loop over all equations in system
for i=1:n,
%loop to perform jacobi-sum
sum = b(i);
for j =1:n;
%skip over i = j
if i~=j;
sum=sum-a(i,j)*xold(j);
end
x(i)=sum/a(i,i);
end
%compute current error
if x(i)~=0;
xerr(i)=abs((x(i)-xold(i))/x(i))*100;
end
end
%check for maximum error
maxerr=max(xerr);
end
%Show Solutions
fprintf('Number of Iternations=%8.3f\n',it);
fprintf('Maximum Error=%8.3f\n',maxerr);
disp('Solution Vector=');
disp(x)
0 Comments
Answers (1)
Matt Tearle
on 23 Mar 2011
So you keep a copy of x, called xold, which you use to calculate the new values of x. The main part of the algorithm happens on the line sum=sum-a(i,j)*xold(j)
The difference between Jacobi and GS is whether or not you use the new x values calculated so far in the current iteration. Given that you use xold to calculate sum, you're not using the new values...
I think that's about as big a hint as I can give without just telling you the answer!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!