Plot all four functions

조회 수: 3 (최근 30일)
Areg Arzoomanian
Areg Arzoomanian 2020년 2월 24일
답변: Aquatris 2020년 2월 24일
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