Plot all four functions

3 次查看(过去 30 天)
I need help. I have my four functions and if I run my code it only plots the first response not all four. Furthermore I don't know how to have all four plots on the same frame.
Thank you.
%% 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= c/Cc
%% Solution Part B
if Zeta==1
C1=x0;
C2=x_dot+Wn*x0;
x = @(t) (C1+C2*t)*exp(-Wn*t)
fplot(x,[0,1])
end
if Zeta==0
X= (((x0^2)*(Wn^2)+(x_dot^2)+(2*x0*x_dot*Zeta*Wn))^0.5)/(((1-(Zeta^2))^0.5)*Wn);
phi= atan((x_dot+(Zeta*Wn*x0))/(x0*Wn*((1-(Zeta^2))^0.5)));
Wd= ((1-(Zeta^2))^0.5)*Wn;
x = @(t) X*(exp(-Zeta*Wn*t))* cos((Wd*t)-phi)
fplot(x,[0,1])
end
if Zeta==0.5
X= (((x0^2)*(Wn^2)+(x_dot^2)+(2*x0*x_dot*Zeta*Wn))^0.5)/(((1-(Zeta^2))^0.5)*Wn);
phi= atan((x_dot+(Zeta*Wn*x0))/(x0*Wn*((1-(Zeta^2))^0.5)));
Wd= ((1-(Zeta^2))^0.5)*Wn;
x = @(t) X*(exp(-Zeta*Wn*t))* cos((Wd*t)-phi)
fplot(x,[0,1])
end
if Zeta==2
C1=x0;
C2=x_dot+Wn*x0;
x= @(t) C1*exp(-Zeta+sqrt(Zeta^2-1)*Wn*t)+C2*exp(-Zeta-sqrt(Zeta^2-1)*Wn*t)
fplot(x,[0,1])
end
%% Solution Part C

采纳的回答

Aquatris
Aquatris 2020-2-24
It only plots the first one because your Zeta is always 1 (c/cc = 1). Moreover, even if you changed Zeta to be other values, it would overwrite your graph since you do not have "hold on" for the figure.
instead of using if statements, why dont you define 4 different functions like this:
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);
%% Solution Part B
Zeta=1;
C1=x0;
C2=x_dot+Wn*x0;
x_1 = @(t) (C1+C2*t)*exp(-Wn*t);
Zeta=0;
X= (((x0^2)*(Wn^2)+(x_dot^2)+(2*x0*x_dot*Zeta*Wn))^0.5)/(((1-(Zeta^2))^0.5)*Wn);
phi= atan((x_dot+(Zeta*Wn*x0))/(x0*Wn*((1-(Zeta^2))^0.5)));
Wd= ((1-(Zeta^2))^0.5)*Wn;
x_0 = @(t) X*(exp(-Zeta*Wn*t))* cos((Wd*t)-phi);
Zeta=0.5;
X= (((x0^2)*(Wn^2)+(x_dot^2)+(2*x0*x_dot*Zeta*Wn))^0.5)/(((1-(Zeta^2))^0.5)*Wn);
phi= atan((x_dot+(Zeta*Wn*x0))/(x0*Wn*((1-(Zeta^2))^0.5)));
Wd= ((1-(Zeta^2))^0.5)*Wn;
x_05 = @(t) X*(exp(-Zeta*Wn*t))* cos((Wd*t)-phi);
Zeta = 2;
C1=x0;
C2=x_dot+Wn*x0;
x_2= @(t) C1*exp(-Zeta+sqrt(Zeta^2-1)*Wn*t)+C2*exp(-Zeta-sqrt(Zeta^2-1)*Wn*t);
figure(1)
fplot(x_0,[0 1],'b')
hold on
fplot(x_05,[0 1],'r')
fplot(x_1,[0 1],'g')
fplot(x_2,[0 1],'k')
hold off
legend('Zeta = 0','Zeta = 0.5','Zeta = 1','Zeta = 2')
axis([0 1 -0.5 0.5])
%% Solution Part C
This way you can plot them all.

更多回答(1 个)

darova
darova 2020-2-24
Please use hold on
  1 个评论
Areg Arzoomanian
Areg Arzoomanian 2020-2-24
how would I integrate that into my code? Please elaborate. Thank you for your fast response.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

标签

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by