numder.m

来自「小的源码Matlab编程」· M 代码 · 共 27 行

M
27
字号

function der = numder(fun, x, h, n, varargin)

% Approximation der of the first order derivative, at the point x, 
% of a function named by the string fun. Parameters h and n
% are user supplied values of the initial step size and the number
% of performed iterations in the Richardson extrapolation.
% For fuctions that depend on parameters their values must follow
% the parameter n.

d = [];
for i=1:n
   s = (feval(fun,x+h,varargin{:})-feval(fun,x-h,varargin{:}))/(2*h);
   d = [d;s];
   h = .5*h;
end
l = 4;
for j=2:n
   s = zeros(n-j+1,1);
   s = d(j:n) + diff(d(j-1:n))/(l - 1);
   d(j:n) = s;
   l = 4*l;
end
der = d(n);


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?