function T = myInt3(f,a,b,e)
h = b-a;
T1 = h*(f(a)+f(b))/2;
T2 = T1/2 + h/2*f(a+h/2);
n = 2;
while abs(T2-T1) >= e
T1 = T2;
S = 0;
for j=0:n-1
x = a + (2*j+1)*(b-a)/(2*n);
S = S+ f(x);
end
T2 = T1/2 +S*(b-a)/(2*n);
n = n*2;
end
T = T2;
测试如下:
对函数3*x^2+4*sin(x)在区间[-2,2]上积分
>> f=@(x)(3*x^2+4*sin(x)');
>> myInt3(f,-2,2,1e-4)
ans =
16.0000