How to obtain the first and second derivative of the "xt" function

22 views (last 30 days)
Hi, I am looking for a way to obtain the first and second derivative of the "xt" function stated in the code below. This is to obtain the velocity and acceleration graph of the displacement "xt" given. Is there a command on Matlab that will allow me to do so ?
%data given
m=4;
k=2500;
c=100;
x0=0.1;
xd0=-10;
n=50;
dt=0.01;
t=[0:dt:(n*dt)];
%natural frequency
wn=sqrt(k/m);
%damping ratio
tho=c/(2*sqrt(k*m));
%Calculating displacement
if tho<1 %underdamped
xt=(exp(-tho.*wn.*t).*(((xd0+tho.*wn.*x0)/(wn.*(1-tho^2)^0.5)).*sin(((1-tho^2)^0.5).*wn.*t)+x0.*cos(((1-tho^2)^0.5).*wn.*t)));
end
%plot displacement, velocity and acceleration
plot(t,xt,t,xdt,t,xddt);
legend('Displacement','velocity','acceleration')
title('System responses')
xlabel('time(s)')
ylabel('x(t), v(t),a(t)')
grid on

Accepted Answer

Paul Hoffrichter
Paul Hoffrichter on 6 Feb 2021
You can use diff function as a crude way to take derivates. But, you should also know that diff introduces noise. Here is an article that deals with this noise issue.
xdt = diff(xt)/dt;
xdt = [xdt xdt(end)];
xddt = diff(xdt)/dt;
xddt = [xddt xddt(end)];
%plot displacement, velocity and acceleration
subplot(3,1,1), plot(t,xt,'-b.'), title('Displacement');
xlabel('time(s)'), ylabel('x(t)'), grid on, grid minor
subplot(3,1,2), plot(t,xdt,'-b.'), title('Velocity System Response');
xlabel('time(s)'), ylabel('v(t)'), grid on, grid minor
subplot(3,1,3), plot(t,xddt,'-b.'), title('Acceleration System Response');
xlabel('time(s)'), ylabel('a(t)'), grid on, grid minor
Notice that the first zero crossing of velocity is about a 1/2 sample from the minimum displacement. If using the diff function, you can reduce this error by upsampling your xt signal so that dt is smaller.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!