Matlab实现——Adaptive Quadrature Using Simpson’s Rule

本文详细介绍了数值积分的基本概念、Simpson's Rule(辛普森法则)的应用及其实现过程,通过实例展示了如何在给定区间上精确计算积分,并通过递归和误差控制策略提高计算精度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

%Input - f is the integrand input as a string ¡¯f¡¯
%    - a and b are upper and lower limits of integration
%   - tol is the tolerance
% Output  - SRmat is the table of values
%                - quad is the quadrature value
%                - err is the error estimate
%Initialize values
SRmat = zeros(30, 6);
iterating=0;
done=1;
SRvec=zeros(1, 6);
SRvec=srule(f, a, b, tol);
SRmat(1, 1:6)=SRvec;
m=1;
state=iterating;
while(state==iterating)
     n=m;
     for j=n:-1:1
         p=j;
         SR0vec=SRmat(p,:);
         err=SR0vec(5);
         tol=SR0vec(6);
if (tol<=err)
      %Bisect interval,apply Simpson¡¯s rule
      %recursively, and determine error
       state=done;
       SR1vec=SR0vec; SR2vec=SR0vec;
       a=SR0vec(1); b=SR0vec(2); c=(a+b)/2;
       err=SR0vec(5); tol=SR0vec(6); tol2=tol/2;
       SR1vec=srule(f,a,c,tol2);
       SR2vec=srule(f,c,b,tol2);
       err=abs(SR0vec(3)-SR1vec(3)-SR2vec(3))/10;
%Accuracy test
if (err<tol)
     SRmat(p, :)=SR0vec;
     SRmat(p,4)=SR1vec(3)+SR2vec(3);  SRmat(p,5)=err;
else
     SRmat(p+1:m+1,:)=SRmat(p:m,:);
     m=m+1;
     SRmat(p,:)=SR1vec;  SRmat(p+1,:)=SR2vec;
     state=iterating;
end
end
     end
end
quad=sum(SRmat(:,4));
err=sum(abs(SRmat(:,5)));
SRmat=SRmat(1:m,1:6);

 

srule.m

 

%Program7.5(Simpson's Rule)

function Z=srule(f,a,b,tol)

%Input- f is the integrand input as a string ??f??

%- a and b are upper and lower limits of integration

%- tol is the tolerance

% Output - Z is a 1x6 vector [a b < xmlnamespace prefix ="st1" ns ="urn:schemas-microsoft-com:office:smarttags" />S S2 err tol1]

h=(b-a)/2;

C=zeros(1,3);

C=feval(f,[a (a+b)/2 b]);

S=h*(C(1)+4*C(2)+C(3))/3;

S2=S;

tol1=tol;

err=tol;

Z=[a b S S2 err tol1];

 

 

 

fun.m

 

function f=fun(x)

f=sin(4*x).*exp(-2*x);

 

 

 

Untitled.m

 

a=0;

b=3;

tol=0.00001;

f='fun';

[SRmat, quad, err]=adapt(f, a, b, tol)
欢迎前往个人博客  驽马点滴 和视频空间 哔哩哔哩-《挨踢日志》
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值