Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
I assigned different colors to my plots but they're all the same color
조회 수: 1 (최근 30일)
이전 댓글 표시
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 c_2 c_3 c_4]
if c==[c_1]
timespan= [0 3];
[t,x]= ode45 (@ (t,x) fun (t,x,m,k,c),timespan,[x0;x_dot]);
end
if c== [c_2]
timespan= [0 3];
[t,x]= ode45 (@ (t,x) fun (t,x,m,k,c),timespan,[x0;x_dot]);
end
if c==[c_3]
timespan= [0 3];
[t,x]= ode45 (@ (t,x) fun (t,x,m,k,c),timespan,[x0;x_dot]);
end
if c==[c_4]
timespan= [0 3];
[t,x]= ode45 (@ (t,x) fun (t,x,m,k,c),timespan,[x0;x_dot]);
end
figure(1)
plot(t,x(:,1),'c')
hold on
plot(t,x(:,1),'m')
plot(t,x(:,1),'y')
plot(t,x(:,1),'k')
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 assigned different colors to my plots but they're all the same color. I need help getting these to plot in their assigned colors
댓글 수: 0
답변 (1개)
Star Strider
2020년 3월 5일
Try this:
fun = @(t,x,m,k,c) [x(2); (-c/m)*x(2)-(k/m)*x(1)];
% %% 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;
cv=[c_1 c_2 c_3 c_4];
for k = 1:numel(cv)
c = cv(k)
timespan= [0 3];
[t{k},x{k}]= ode45 (@ (t,x) fun (t,x,m,k,c),timespan,[x0;x_dot]);
end
figure(1)
plot(t{1},x{1}(:,1),'c')
hold on
plot(t{2},x{2}(:,1),'m')
plot(t{3},x{3}(:,1),'y')
plot(t{4},x{4}(:,1),'k')
댓글 수: 2
Star Strider
2020년 3월 5일
It worked when I ran it (in R2019b), and produced this plot figure:
I have no idea why it fails to run for you. My code quite definitely works correctly.
이 질문은 마감되었습니다.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!