how to get multiple lines on a plot with ode45 and for loop?

3 次查看(过去 30 天)
I'm modeling a mass spring damper system and I'm trying to get multiple lines on the plot for each different damping coefficient value and I can only get one line for the first value. my code is a bit mess but here it is.
clear
clc
g=9.81; %m/s^2
m=1179/4; % mass of a honda civic kg
L=1; %Distance traveled meters
t=0:.001:3;
y_0=0;
dydt_0=0;
k=(m*g)/.1; %spring constant N/m
v_0=20; %velocity m/s
%x=v_0*t;
f=250; %load in kg
%z=.5;
z=[0 .1 .2 .25 .35 .5 .65 .75 .9 1]; %damping factor ranges
wn=(k/m)^.5; %natural frequency
c=2*m*wn.*z; %damping coeff
wd=wn*(1-z.^2).^.5; %damped natural freq
hold on
for i=1:10
[t,y]=ode45(@EOMM,t,[y_0 dydt_0]);
plot(t,y(:,1))
grid on
xlabel('Time(s)')
ylabel('Deflection(m)')
end
% function
function [ dydt ] = EOMM(t,y)
%UNTITLED8 Summary of this function goes here
% Detailed explanation goes h
g=9.81; %m/s^2
m=1179/4; % mass of a honda civic kg
L=1; %Distance traveled meters
t=0:.001:3;
y_0=0;
dydt_0=0;
k=(m*g)/.1; %spring constant N/m
v_0=20; %velocity m/s
f=250; %load in kg
%z=.5;
z=[0 .1 .2 .25 .35 .5 .65 .75 .9 1]; %damping factor ranges
wn=(k/m)^.5; %natural frequency
c=2*m*wn.*z; %damping coeff
for i=1:10
dydt_1=y(2);
dydt_2(i)=(1/m)*(f-c(i)*y(2)-k*y(1));
dydt=[dydt_1;dydt_2(i)];
end
end

回答(1 个)

Walter Roberson
Walter Roberson 2016-8-4
Move the
hold on
to after the plot() statement. Then, after the ylabel() statement, add
drawnow()

类别

Help CenterFile Exchange 中查找有关 Assembly 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by