数值积分-梯形公式,看看谁找出错误最快
#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,f();
int i,n;
printf("please input a= and b=\n");
scanf("%d %d",&a,&b);
n=1;
s0=(f(a)+f(b))*(b-a)/2;
do
{
step=(b-a)/n;
for(i=0;i<n;i++)
{
x[i]=a+i*step;
s1=0;
for(i=1;i<=n;i++)
s1+=(f(x[i+1])+f(x[i]))*step/2;
t=s0;
s0=s1;
n=n+1;
}
}
while (fabs(s0-t)>eps);
{
printf("s0=%lf",s0);
printf("此时步长为:%d",n);
printf("\n");
}
return 0;
}
double f(double m)
{
double y;
y=sqrt(1+pow(m,2));
return y;
}