calculate derivative of time history in matlab

To calculate the derivative of a time history in MATLAB, you can use the following steps:

  1. Define the time vector t and the signal vector x.
  2. Calculate the time step dt as the difference between two consecutive time values.
  3. Use the diff function to calculate the difference between two consecutive signal values.
  4. Divide the result by dt to obtain the derivative.

Here's the code:

main.m
% Define the time vector and the signal vector
t = 0:0.1:10;
x = sin(t);

% Calculate the time step
dt = t(2) - t(1);

% Calculate the derivative
dxdt = diff(x) / dt;

% Plot the original signal and its derivative
subplot(2,1,1)
plot(t,x)
xlabel('Time')
ylabel('Signal')
title('Original Signal')
grid on

subplot(2,1,2)
plot(t(1:end-1),dxdt)
xlabel('Time')
ylabel('Derivative')
title('Derivative of Signal')
grid on
416 chars
25 lines

This code generates two subplots. The top one shows the original signal, and the bottom one shows its derivative. Note that the diff function returns a vector that is one element shorter than the original signal, which is why we need to remove the last element of the t vector before plotting the derivative.

gistlibby LogSnag