MATLAB: Multiple equation for loop not plotting.

for loopplotplotting

I cannot get the following program to plot the variables w & P. I have noticed that the loop is only using the last variable but still does not plot the point. I'm not sure what is happening. Thank you!
clear
clc
k=1000; c=50; M=200; m=2; e=0.1;
vv=[20,40,0,0.5];
for w=20:0.5:40
r=w*sqrt(M/k);
wn=w/r;
damp=c/2/M/wn;
phi=atan((2*damp*r)/(1-r^2));
X=(m*e*r)/(M*sqrt((1-r^2)^2+(2*damp*r)^2));
F=m*e*w^2*sin(w);
dx=w*X*cos(w-phi);
T=2*pi*w;
P=abs(F*dx);
plot(w,P,'k','linewidth',2); axis(vv); grid on; hold on;
end
xlabel('Forcing Frequency (Hz)', 'fontsize', 12);
title('Problem 7');
ylabel('Mean Power Output (Hp)', 'fontsize', 12);

Best Answer

It seems that you need to subscript the appropriate variables. I’m not certain that this is what you want, but it at least has the virtue of running and plotting:
k=1000; c=50; M=200; m=2; e=0.1;
vv=[20,40,0,0.5];
w=[20:0.5:40];
for k1 = 1:length(w)
r(k1)=w(k1)*sqrt(M/k);
wn=w(k1)./r(k1);
damp=c/2/M./wn;
phi=atan((2*damp.*r(k1))./(1-r(k1).^2));
X=(m*e*r(k1))./(M*sqrt((1-r(k1).^2).^2+(2*damp.*r(k1)).^2));
F=m*e*w(k1).^2.*sin(w(k1));
dx=w(k1).*X.*cos(w(k1)-phi);
T=2*pi*w(k1);
P(k1,:)=abs(F.*dx);
end
plot(w,P,'k','linewidth',2); axis(vv); grid on; hold on;
xlabel('Forcing Frequency (Hz)', 'fontsize', 12);
title('Problem 7');
ylabel('Mean Power Output (Hp)', 'fontsize', 12);
Experiment with it to get the result you want. You likely have to move the plot call outside the loop in any event.
Related Question