⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fidiff.m

📁 这是《Numerical Methods with MATLAB: Implementation and Application》一书的配书程序(Matlab)
💻 M
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -