Functions on same plot

1 Ansicht (letzte 30 Tage)
Areg Arzoomanian
Areg Arzoomanian am 5 Mär. 2020
Bearbeitet: Adam Danz am 6 Mär. 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

Antworten (1)

Adam Danz
Adam Danz am 6 Mär. 2020
Bearbeitet: Adam Danz am 6 Mär. 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')

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by