Differences and approximate derivatives - MATLAB diff
www.mathworks.com › help › matlabFor example, the first derivative of sin(x) with respect to x is cos(x), and the second derivative with respect to x is -sin(x). You can use diff to approximate these derivatives. h = 0.001; % step size X = -pi:h:pi; % domain f = sin(X); % range Y = diff(f)/h; % first derivative Z = diff(Y)/h; % second derivative plot(X(:,1:length(Y)),Y, 'r' ,X,f, 'b' , X(:,1:length(Z)),Z, 'k' )
Differentiation - MATLAB & Simulink
www.mathworks.com › help › symbolicTo determine the default variable that MATLAB differentiates with respect to, use symvar: symvar (f, 1) ans = t. Calculate the second derivative of f with respect to t: diff (f, t, 2) This command returns. ans = -s^2*sin (s*t) Note that diff (f, 2) returns the same answer because t is the default variable.