Plotting residue vs iterations in matlab
이전 댓글 표시
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)
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Calculus에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!