ive tried my code to execute the question that states for a given matrix find solution using gauss seidel also find the residue and its graph vs iteration number residue(r) = (rhs-lhs)/rhs but I am not able to get any data over the graph which pops up. kindly guide me where I'm doing mistakes in plotting graphs or finding the residue %A=[27 6 -1;6 15 2; 1 1 54]; %given matrix %b=[85 72 110]'; A=[2 1 1 1 1; 1 3 1 2 1; 1 2 5 1 3; 2 3 1 4 1; 2 1 1 2 3]; %given matrix b=[5 1 17 0 10]'; r=[100 100 100 100 100]'; Em= input("max error permissible"); %get max error permissible z=input("number of iterations"); %gauss for linear equation for i=1:size(A,1) %get input intial guess of x from user switch i case 1 x(i)=input("guess x1"); case 2 x(i)=input("guess x2"); case 3 x(i)=input("guess x3"); case 4 x(i)=input("guess x4"); case 5 x(i)=input("guess x5"); end end E=100 ; %keeping intial error as 100% iter=0; %keep iteration count as 0 plotGauss=[]; z=[]; while E>Em xold=x; %keeping old value for i= 1:size(A,1) sum=0; %intialise sum=0 sum1=0; %intialise sum1=0 for using in finding r(i) i.e (RHS-LHS)/RHS for j=1:i-1 sum=sum+A(i,j)*x(j); end for j=i+1:size(A,1) sum=sum+A(i,j)*xold(j); end x(i)=(1/A(i,i))*(b(i)-sum); %updation using seidal method if i==4 r(i)=(b(i)-sum-(sum1+A(i,i)*x(i))); else r(i)=(1/b(i))*(b(i)-sum-(sum1+A(i,i)*x(i))); %plotG=r(i); %r; end iter=iter+1; %keep count of iteration z=r(i); %y(iter,:)=x; %keep check on the output value of x after each iteration % r(i)=(1/b(i))*(b(i)-(A(i,i)*x( E=abs(((xold-x)/x)*100); %compute the error percentage for convergence criteria plotG=[plotG;z]; r end end disp("converged value of x1 to x5 is"); x r(1) plot(iter,plotG)

 채택된 답변

VBBV
VBBV 2022년 2월 6일

1 개 추천

A=[2 1 1 1 1; 1 3 1 2 1; 1 2 5 1 3; 2 3 1 4 1; 2 1 1 2 3]; %given matrix
b=[5 1 17 0 10]';
r=[100 100 100 100 100]';
Em= 0.002; %input("max error permissible"); %get max error permissible
z=100;% input("number of iterations");
%gauss seidel method for linear equation
for i=1:size(A,1) %get input intial guess of x from user
switch i
case 1
x(i)=input("guess x1");
case 2
x(i)=input("guess x2");
case 3
x(i)=input("guess x3");
case 4
x(i)=input("guess x4");
case 5
x(i)=input("guess x5");
end
end
E=100 ; %keeping intial error as 100%
iter=0; %keep iteration count as 0
plotGauss=[];
z=[];
while E>Em
xold=x; %keeping old value
for i= 1:size(A,1)
sum=0; %intialise sum=0
sum1=0; %intialise sum1=0 for using in finding r(i) i.e (RHS-LHS)/RHS
for j=1:i-1
sum=sum+A(i,j)*x(j);
end
for j=i+1:size(A,1)
sum=sum+A(i,j)*xold(j);
end
x(i)=(1/A(i,i))*(b(i)-sum); %updation using seidal method
if i==4
r(i)=(b(i)-sum-(sum1+A(i,i)*x(i)));
else
r(i)=(1/b(i))*(b(i)-sum-(sum1+A(i,i)*x(i)));
%plotG=r(i);
%r;
end
iter=iter+1; %keep count of iteration
z=r(i);
%y(iter,:)=x; %keep check on the output value of x after each iteration
% r(i)=(1/b(i))*(b(i)-(A(i,i)*x(
E=abs(((xold-x)/x)*100); %compute the error percentage for convergence criteria
plotG=[plotG;z];
r
plot(1:iter,plotG)
hold on
end
end
disp("converged value of x1 to x5 is");
x
r(1)
Try plot function inside loop, instead of plotting a scalar vs vector outside of loop

댓글 수: 2

thank you soo much !
kindly check if my algorithim is correct to find r i.e residue (rhs-lhs)/rhs teach me to plot for r1 r2 r3 r4 r5 with different colours in same graph as output.
also do you have any refrence to share to learn to code the same method of non linear terms.
Hi anything I can learn how to plot?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

질문:

2022년 2월 6일

댓글:

2022년 2월 7일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by