Functions on same plot

조회 수: 2 (최근 30일)
Areg Arzoomanian
Areg Arzoomanian 2020년 3월 5일
편집: Adam Danz 2020년 3월 6일
clear,clc
%% Givens
m=2; %kg
k=200; %N/m
x0=0.05; %meters
x_dot=2; %m/s
%% Solution Part A
Wn= (k/m)^0.5;
c= (2*m*Wn);
cc= (2*m*Wn);
Zeta_0=c/cc
%% Continuation
c_1= 0*cc;
c_2= 0.5*cc;
c_3= cc;
c_4= 2*cc;
Zeta1= 0*Zeta_0;
Zeta2= 0.5*Zeta_0;
Zeta3= Zeta_0;
Zeta4= 2*Zeta_0;
for c=[c_1]
timespan= [0 3];
[t,x]= ode45 (@ (t,x) fun (t,x,m,k,c),timespan,[x0;x_dot]);
figure(1)
plot(t,x(:,1),'b')
hold on
c= [c_2]
plot(t,x(:,1),'g')
c=[c_3]
plot(t,x(:,1),'r')
c=[c_4]
plot(t,x(:,1),'k')
hold off
end
figure(1)
title ('Position Response vs. Time')
legend ('Undamped','Underdamped','Critically Damped','Overdamped')
xlabel('Time')
ylabel ('Position Response')
function v= fun(t,x,m,k,c)
v=[x(2); (-c/m)*x(2)-(k/m)*x(1)];
end
I need all four c_1/2/3/4 to be on the same plot with the colors assigned, however it only plots the first c_1 and not the other three

답변 (1개)

Adam Danz
Adam Danz 2020년 3월 6일
편집: Adam Danz 2020년 3월 6일
Set up a loop that evaluates the function for each c-value and adds the line to the plot.
allc = [c_1, c_2, c_3, c_4];
colors = {'b' 'g' 'r' 'k'};
figure(1)
hold on
for c = 1:numel(allc)
timespan= [0 3];
[t,x]= ode45 (@ (t,x) jff(t,x,m,k,allc(c)),timespan,[x0;x_dot]);
plot(t,x(:,1),colors{c})
end
hold off
title ('Position Response vs. Time')
legend ('Undamped','Underdamped','Critically Damped','Overdamped')
xlabel('Time')
ylabel ('Position Response')

카테고리

Help CenterFile Exchange에서 Title에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by