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

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

DGM
DGM il 25 Gen 2022
Modificato: DGM il 25 Gen 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 Commenti
DGM
DGM il 26 Gen 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'})

Accedi per commentare.

Più risposte (1)

KSSV
KSSV il 25 Gen 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 Commenti
KSSV
KSSV il 25 Gen 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.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by