WUT_Computer_Science/ENUME/projectB/Code/deriv.m
2021-12-03 01:45:50 +01:00

13 lines
332 B
Matlab

% calculate the nth derivative of func at x
function y = deriv(func, x, deg)
% base case: zeroth derivative
if deg == 0
y = func(x);
return
end
% recurse to find the nth derivative
step = sqrt(eps);
y = (deriv(func, x + step, deg - 1) - deriv(func, x - step, deg - 1)) / (2 * step);
end