How to plot different y values for different x values, provided the x and y values are to be generated in a loop?

2 views (last 30 days)
Hello Everyone. I am unable to fetch the values of xy generated in the iteration process.
For every value of a, I want to plot ra. Following is my code, please help:
n=100;
b=6.9;
r=5;
y=(r-1)+0.5;
e=0;z=2;
U=9000;
for a=linspace(4500,U,500)
q=2*pi*a*r;
for m=0:n
p=(4*q/pi)*((((-1)^m)/((2*m)+1))*(exp(-((2*m)+1)*(pi/(2*b))*z))*(cos(((2*m)+1)*(pi/(2*b))*y)));
e=e+p;
if (abs(p)/e)<(10^(-6))
break
end
u=e;
while z<=6
z=z+1;
end
end
ps=1000*((u/100)^2/2);
ra=2*0.072/ps;
plot(a,ra)
hold on
end
hold off

Accepted Answer

DGM
DGM on 25 Jan 2022
Edited: DGM on 25 Jan 2022
This is a start:
n=100;
b=6.9;
r=5;
y=(r-1)+0.5;
e=0;z=2;
U=9000;
npoints = 500;
a = linspace(4500,U,500);
q = 2*pi*a*r;
ra = zeros(1,npoints);
for idx = 1:npoints
for m=0:n
p=(4*q(idx)/pi)*((((-1)^m)/((2*m)+1))*(exp(-((2*m)+1)*(pi/(2*b))*z))*(cos(((2*m)+1)*(pi/(2*b))*y)));
e=e+p;
if (abs(p)/e)<(10^(-6))
break
end
u=e;
while z<=6
z=z+1;
end
end
ps = 1000*((u/100)^2/2);
ra(idx) = 2*0.072/ps;
end
plot(a,ra)
  5 Comments
DGM
DGM on 26 Jan 2022
n = 100; %fixing the number of iterations
b = 6.9;
r = 5;
y = (r-1)+0.5;
e = 0;
U = 4500:500:9000;
npoints = numel(U);
ra = zeros(5,npoints);
for uidx = 1:npoints
q = 2*pi*(U(uidx)/60)*r; %linear vel conversion
for z = 2:1:6
for cidx = 1:npoints
for m = 0:n
p = (4*q/pi)*((((-1)^m)/((2*m)+1))*(exp(-((2*m)+1)*(pi/(2*b))*z))*(cos(((2*m)+1)*(pi/(2*b))*y)));
e = e+p;
if (abs(p)/e)<(10^(-6)) %convergence check
break
end
u = e;
end
ps = 1000*((u/100)^2/2);
ra(z,cidx) = (2*0.072/ps)*(10^(6));
end
e = 0; % reset e
end
end
plot(U,ra)
legend({'z=2cm','z=3cm','z=4cm','z=5cm','z=6cm'})

Sign in to comment.

More Answers (1)

KSSV
KSSV on 25 Jan 2022
As you are plotting a point, you need to use marker.
n=100;
b=6.9;
r=5;
y=(r-1)+0.5;
e=0;z=2;
U=9000;
figure
hold on
for a=linspace(4500,U,500)
q=2*pi*a*r;
for m=0:n
p=(4*q/pi)*((((-1)^m)/((2*m)+1))*(exp(-((2*m)+1)*(pi/(2*b))*z))*(cos(((2*m)+1)*(pi/(2*b))*y)));
e=e+p;
if (abs(p)/e)<(10^(-6))
break
end
u=e;
while z<=6
z=z+1;
end
end
ps=1000*((u/100)^2/2);
ra=2*0.072/ps;
plot(a,ra,'.b')
end
hold off
But it is suggested to store thevalues into an array and plot after the loop as @DGM suggested.
  3 Comments
KSSV
KSSV on 25 Jan 2022
Becuase you are not plotting z. Include that plot also, so that you can get. But I feel z values lies far above the value of ra and plot doesn't look good.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!