mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-04 16:23:11 +02:00
13 lines
332 B
Matlab
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
|