数值积分-(自适应辛普森法)

 

#include<stdio.h>
#include<math.h>

double F(double x){return log10(x);}

//三点辛普森公式
double simpson(double width,double fa,double fb,double fc) 
{
	return (fb+fa+4*fc)*width/6;
}

//自适应simpson公式递归过程
double asr(double a,double b,double eps,double A)
{
	double c=(a+b)/2;
	double fa,fb,fc,L,R;
	fa=F(a);fb=F(b);fc=F(c);
	L=simpson(c-a,fa,fc,F((c+a)/2));
	R=simpson(b-c,fc,fb,F((b+c)/2));
	if(fabs(L+R-A)<=15*eps) return L+R+(L+R-A)/15;
	return asr(a,c,eps/2,L)+asr(c,b,eps/2,R);
}

//自适应simpson公式主过程
double asr(double a,double b,double eps)
{
	return asr(a,b,eps,simpson(b-a,F(a),F(b),F((b+a)/2)));
}
int main()
{
	printf("%lf\n",asr(1,5,0.00000001));
	return 0;
}


 

你可能感兴趣的:(数值积分-(自适应辛普森法))