clc
format long
clear
a=0;b=1;p=0;
n=4;m=0;
h=(b-a)/n;
for i=1:n-1
x=0;
x=a+h*i;
m=m+sin(x)/x;
end
h=(b-a)/(2*n);
for k=1:2:2*n
p=p+sin(a+k*h)/(a+k*h);
end
Sn=h/3*[1+4*p+2*m+sin(b)/b] %辛普森公式结果
result=sinint(1) %计算精确值
format short
结果:
Sn =
0.94608331088847
result =
0.94608307036718
>>