plotting in a for loop

Basically i am trying to get all the iterations of the step response for all values in the zeta matrix to be placed on to the same graph using a for loop. for some reason i think it is adding them up and putting them in the same line.
%%
zeta= [-.02,0,.05,.2,1,2];
omegan=2*pi;
num=[omegan^2];
for i=numel(zeta)
den{i}=[1 2*zeta(i)*omegan omegan^2];
sys(i)=tf(num,den{i});
tfinal=4;
numpts=201;
time=0:tfinal/(numpts-1):tfinal;
y{i}=step(sys(i),time);
plot(time,y{i})
hold on
plot([0 tfinal], [1 1], 'k-')
xlabel ('t (sec)')
ylabel ('y(t) (m)')
title ('Step Response')
end

回答(1 个)

hi
see modified code
zeta= [-.02,0,.05,.2,1,2];
omegan=2*pi;
num=[omegan^2];
tfinal=4;
numpts=201;
time=0:tfinal/(numpts-1):tfinal;
y_plot = zeros(numpts , numel(zeta));
for i= 1:numel(zeta)
den{i}=[1 2*zeta(i)*omegan omegan^2];
sys(i)=tf(num,den{i});
[y,t]=step(sys(i),time);
y_plot(:,i) = y;
mylegend{i+1} = ([' zeta = ' num2str(zeta(i)) ]);
end
mylegend{1} = (' target ');
plot([0 tfinal], [1 1], 'k-',time,y_plot)
legend(mylegend');
xlabel ('t (sec)')
ylabel ('y(t) (m)')
title ('Step Response')

类别

帮助中心File Exchange 中查找有关 Scope Variables and Generate Names 的更多信息

产品

版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by