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

10 vues (au cours des 30 derniers jours)
nick Gangi
nick Gangi le 3 Août 2016
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

Réponses (1)

Walter Roberson
Walter Roberson le 4 Août 2016
Move the
hold on
to after the plot() statement. Then, after the ylabel() statement, add
drawnow()

Community Treasure Hunt

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

Start Hunting!

Translated by