牛顿--科特斯公式-求积分-编译通过,可是逻辑上有错误了,老是得不到答案
#include<stdio.h>#include<math.h>
#define maxsize 100
#define eps 0.000001
int main(void)
{
double a,b,t,x[maxsize],step,s0,s1;
double m1,m2,m3,sum1,sum2,sum3,sum4;
double f(double m);
int i,n;
printf("请输入积分下限:\n");
scanf("%lf",&a);
printf("请输入积分上限:\n");
scanf("%lf",&b);
n=0;
s0=0;
do
{
n=n+1;
step=(b-a)/n;
for(i=0;i<=n;i++)
x[i]=a+i*step;
sum1=sum2=sum3=sum4=1;
for(i=0;i<=n-1;i++)
{
m1=x[i]+(x[i+1]-x[i])/4;
sum1+=f(m1);
}
for(i=0;i<=n-1;i++)
{
m2=x[i]+(x[i+1]-x[i])/2;
sum2+=f(m2);
}
for(i=0;i<=n-1;i++)
{
m3=x[i]+(x[i+1]-x[i])*3/4;
sum1+=f(m1);
}
for(i=0;i<=n-1;i++)
{
sum2+=f(x[i]);
}
s1=0;
s1+=(7*(f(a)+f(b))+32*sum1+12*sum2+32*sum3+14*sum4)*step/90;
t=s0;
s0=s1;
}
while (fabs(s0-t)>eps&&n<100);
{
printf("s0=%lf\n",s0);
printf("此时步长为:%d",n);
printf("\n");
}
return 0;
}
double f(double c)
{
double y;
y=sqrt(1+pow(c,2));
return y;
}