Plot all four functions

4 views (last 30 days)
Areg Arzoomanian
Areg Arzoomanian on 24 Feb 2020
Answered: Aquatris on 24 Feb 2020
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

Accepted Answer

Aquatris
Aquatris on 24 Feb 2020
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.

More Answers (1)

darova
darova on 24 Feb 2020
Please use hold on
  1 Comment
Areg Arzoomanian
Areg Arzoomanian on 24 Feb 2020
how would I integrate that into my code? Please elaborate. Thank you for your fast response.

Sign in to comment.

Categories

Find more on Interpolation in Help Center and File Exchange

Tags

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!