Functions on same plot

1 view (last 30 days)
Areg Arzoomanian
Areg Arzoomanian on 5 Mar 2020
Edited: Adam Danz on 6 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]
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

Answers (1)

Adam Danz
Adam Danz on 6 Mar 2020
Edited: Adam Danz on 6 Mar 2020
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')

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!