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

5 views (last 30 days)
ISHA ARORA
ISHA ARORA on 25 Jan 2022
Commented: ISHA ARORA on 27 Jan 2022
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

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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!