Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
I assigned different colors to my plots but they're all the same color
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Star Strider
am 5 Mär. 2020
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 Kommentare
Star Strider
am 5 Mär. 2020
It worked when I ran it (in R2019b), and produced this plot figure:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/275439/image.png)
I have no idea why it fails to run for you. My code quite definitely works correctly.
Diese Frage ist geschlossen.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!