fidiff.m
来自「这是《Numerical Methods with MATLAB: Imple」· M 代码 · 共 22 行
M
22 行
function fidiff(x)
% fidiff First order finite-difference approximation to d/dx of exp(x)
%
% Synopsis: fidiff(x)
%
% Input: x = (optional) value at which the derivative is to be evaluated
% Default: x = 1
%
% Output: A table and plot of relative error versus stepsize
if nargin<1, x=1; end
fp = exp(x); % Exact value of fprime
h = logspace(-12,0,13)'; % Column vector of stepsizes
fpfd = (exp(x+h) - exp(x))./h; % Vectorized calculation of fd approximation
Erel = abs(fpfd - fp)./fp; % and relative errors
fprintf(' h fp fpfd Erel\n');
for k=1:length(h)
fprintf('%10.1e %9.5f %9.5f %12.2e\n',h(k),fp,fpfd(k),Erel(k));
end
loglog(h,Erel,'+'); xlabel('Stepsize, h'); ylabel('Relative error');
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?