区间折半法
从梯形公式出发,上一步步长为
h
{h}
h ,则有步长折半后的积分
T
2
n
=
1
2
T
n
+
h
2
∑
i
=
0
n
−
1
f
(
x
i
+
0.5
)
T_{2n}= \frac{1}{2}T_n+ \frac{h}{2} \sum_{i=0}^{ n-1}f(x_{i+0.5})
T2n=21Tn+2hi=0∑n−1f(xi+0.5)
matlab实现
%% 区间折半法例子
format long
[I i] = halfStep(@f,0,1,1e-7,10)
function r = f(x)
if x == 0
r = 1;
else
r = sin(x)./x;
end
end
%% 区间折半法
% 输入函数,范围,精度,最大迭代次数
% 输出积分值,迭代次数
function [I,i] = halfStep(f,a,b,eps,max_iter)
h = b-a;
T0 = h/2*(f(a)+f(b))
for i = 1:max_iter
x_n = a+h/2:h:b-h/2;
T1 = 0.5*T0+0.5*h*sum(f(x_n))
if abs(T1-T0)<eps
I = T1;
break
end
T0 = T1;
h = h/2;
end
end