Matlab adaptive quadrature

这段Matlab脚本展示了如何进行自适应求积。首先定义了被积函数,然后在给定误差容忍度下使用自适应Simpson法则进行积分。通过比较不同点数的Simpson规则结果,展示自适应求积法的优势。

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

% script to perform adaptive quadrature
clear all, close all

global pts

% function to be integrated defined in routine f
f = 'integrand';
a = 1; b = 3;
pts = [a;b];

tol = input('Enter error tolerance: ');

% this is just to plot the graph
% it's usually a good idea to look at the integrand
% if possible before you try to integrate it
ezplot(f,[1,3])
disp('Hit any key to continue')
pause
hold on

fa = feval(f,a);
fb = feval(f,b);

Sf_old = simp(a,b,f,fa,fb);

Sf = adaptiveSimpson(a,b,f,fa,fb,tol,Sf_old)

qpts = sort(pts);
plot(qpts,zeros(length(pts),1),'rx')

disp('number of function evaluations')
disp(length(pts))
disp('Hit any key to continue')
pause

% now compare result with straight Simpson's rule
% using the same number of points
sum = 0; 
h = (b-a)/(length(pts)-1);
for i=0:length(pts)-1,
   fxi = feval(f,a+i*h);
   if i == 0 | i == length(pts)-1,
      sum = sum + fxi;
   elseif mod(i,2) == 1,
         sum = sum + 4*fxi;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值