plot different curves(different output of a for loops) in single graph

1 view (last 30 days)
hi ive made a code to solve a set linear equation
also the reside r i.e r= (rhs-lhs)/rhs
so i need to find r of every loop while finding the root(x) value.
this different r1 r2 r3 r4 r5 have to plotted on same graph with different colours.
kindly guide me how to plot different r in single curve of this code.
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 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
plotG=[];
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)));
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
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

Answers (1)

Milan Bansal
Milan Bansal on 13 Sep 2023
Hi,
I understand that you are trying to plot different plots for "r" on the same plot in different colours while using the "for" loop.
iter=iter+1; %keep count of iteration
z=r(i);
%y(iter,:)=x; %keep check on the output value of x after each iteration
E=abs(((xold-x)/x)*100); %compute the error percentage for convergence criteria
plotG=[plotG;z];
r
plot(1:iter,plotG)
hold on
In the above part of the your code, value of "iter" is incremented and a new element "z" is concatenated with "plotG" array in each iteration of the "for" loop. Then, a "line" plot is plotted for "plotG" array. This is resulting is overlapping of the same curve with a new point in each iteration, hence new curves are not visible in different colors.
Instead of using "plot" function, you may use "scatter" function to plot values of r in different colors for each interation in "for" loop. Try using the code given below.
iter=iter+1; %keep count of iteration
z=r(i);
%y(iter,:)=x; %keep check on the output value of x after each iteration
E=abs(((xold-x)/x)*100); %compute the error percentage for convergence criteria
plotG=[plotG;z];
r
scatter(iter,plotG(iter))
hold on
Refer to the below documentation link to learn more about "scatter" plot.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!