MATLAB Answers

I assigned different colors to my plots but they're all the same color

1 view (last 30 days)
Areg Arzoomanian
Areg Arzoomanian on 5 Mar 2020
Commented: Star Strider on 5 Mar 2020
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 Comments

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 5 Mar 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 Comments

Areg Arzoomanian
Areg Arzoomanian on 5 Mar 2020
it says: Unable to perform assignment because brace indexing is not supported for variables of this type.
Error in untitled5 (line 28)
[t{k},x{k}]= ode45 (@ (t,x) fun (t,x,m,k,c),timespan,[x0;x_dot ]);
Star Strider
Star Strider on 5 Mar 2020
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.

Sign in to comment.

Tags

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!