derive v(t) in matlab

To derive the function v(t) in MATLAB, you can use the diff function to approximate the derivative numerically. First, define a function for v(t) and specify the time interval of interest, as well as any parameters needed for the function:

main.m
% Define function for v(t)
function v = vt(t)
    v = sin(2*pi*t) + exp(-t/5);
end

% Specify time interval and number of points
t_start = 0; t_end = 15; npts = 1000;
t = linspace(t_start, t_end, npts);

% Compute numerical derivative of v(t)
dt = t(2) - t(1);
v_dot = diff(vt(t))/dt;

% Plot v(t) and its derivative
figure(1); clf;
plot(t, vt(t), 'b-', 'LineWidth', 2);
xlabel('Time (s)'); ylabel('v(t)');
title('Function v(t)');
grid on;

figure(2); clf;
plot(t(2:end), v_dot, 'r-', 'LineWidth', 2);
xlabel('Time (s)'); ylabel('dv/dt');
title('Derivative of v(t)');
grid on;
577 chars
26 lines

This code defines a function vt(t) that computes v(t), specifies a time interval and number of points to use for computing the derivative, and computes the derivative using the diff function. The resulting v_dot array is plotted alongside the original function using plot.

gistlibby LogSnag