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

Answers (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')

Products

Release

R2020a

Answered:

on 15 Oct 2020

Community Treasure Hunt

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

Start Hunting!