www.pudn.com > cghost.rar > BCSIMP.C
/**********************变步长辛普生法*************/
/***a为积分下限;b为积分上限;eps为精度要求.********/
#include "math.h"
double bcsimp(a,b,eps)
double a,b,eps;
{extern double bcsimpf();
int n,k;
double h,t1,t2,s1,s2,ep,p,x;
n=1;h=b-a;
t1=h*(bcsimpf(a)+bcsimpf(b))/2.0;
s1=t1;
ep=eps+1.0;
while(ep>=eps)
{
p=0.0;
for(k=0;k<=n-1;k++)
{x=a+(k+0.5)*h;
p=p+bcsimpf(x);
}
t2=(t1+h*p)/2.0;
s2=(4.0*t2-t1)/3.0;
ep=fabs(s2-s1);
t1=t2;s1=s2;n=n+n;h=h/2.0;
}
return(s2);
}
/*****************************************************************/
double bcsimpf(x)
double x;
{double y;
y=exp(x*x);
return(y);
}
/*****************************************************************/
main()
{
double a,b,eps,t;
a=0.0;b=1.0;eps=0.000001;
t=bcsimp(a,b,eps);
printf("t=%e\n",t);
}