Matlab实现——Differentiation Using Limits

欢迎前往个人博客 驽马点滴 和视频空间 哔哩哔哩-《挨踢日志》

difflim.m

 

%Program 6.1 (Differentiation Using Limits). To approximate 
%f'(x) numerically by generating the sequencef'(x)≈Dk=
%[f(x+(10^-k)*h)-f(x-(10^-k)*h]/[2*(10-k)*h]  for k=0,… ,n
%until |Dn+1-Dn| > |Dn-Dn-1| or |Dn-Dn-1| < tolerance, 
%which is an attempt to find the best approximation f’(x)≈Dn.
function  [L,n]=difflim(f,x,toler)
%Input  - f is the function input as a string 'f '
%       - x is the differentiation point
%       - toler is the desired tolerance
%Output - L=[H' D' E']: 
%                    H is the vector of step sizes
%                    D is the vector of approximate derivatives
%                    E is the vector of error bounds
%         - n is the coordinate of the "best approximation"
max1=15;
h=1;
H(1)=h;
D(1)=(feval(f,x+h)-feval(f,x-h))/(2*h);
E(1)=0;
R(1)=0;
for n=1:2
   h=h/10;
   H(n+1)=h;
   D(n+1)=(feval(f,x+h)-feval(f,x-h))/(2*h);
   E(n+1)=abs(D(n+1)-D(n));
   R(n+1)=2*E(n+1)/(abs(D(n+1))+abs(D(n))+eps);
end
n=2;
while((E(n)>E(n+1))&(R(n)>toler))&n<max1
   h=h/10;
   H(n+2)=h;
   D(n+2)=(feval(f,x+h)-feval(f,x-h))/(2*h);
   E(n+2)=abs(D(n+2)-D(n+1));
   R(n+2)=2*E(n+2)/(abs(D(n+2))+abs(D(n+1))+eps);
   n=n+1;
end
n=length(D)-1;
L=[H' D' E'];


fun.m

 

 

function f=fun(x)
f=sin(cos(1/x));


untitled.m

 

 

x=1/sqrt(2);
toler=10e-13;
f='fun';
[L,n]=difflim(f,x,toler)

欢迎前往个人博客 驽马点滴 和视频空间 哔哩哔哩-《挨踢日志》

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值