|
本帖最后由 markfang2050 于 2019-5-5 03:25 编辑
#include <math.h>
#include <stdio.h>
#include<stdlib.h>
#include <time.h>
double f(double x)
//辛普森(Simpson)公式是牛顿-科特斯公式当n=2时的情形,也称为三点公式。//
//利用区间二等分的三个点来进行积分插值。其科特斯系数分别为1/6,4/6,1/6。//
{
double y;
y=sqrt(1.0000+100.0000/(x*x*x*x));
return y;
}
double simpsom(double a,double b,double eps,double (*f)(double))
{
int n,k;
double h,t1,t2,s1,s2,ep,p,x;
n=1;
h=b-a;
t1=h*((*f)(a)+(*f)(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+(*f)(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;
}
void main()
{
clock_t begin, end;
double cost;
begin = clock();
double a,b,eps,t,f(double);
a=1.0;
b=10.0;
eps=0.0000001;
t=simpsom(a,b,eps,f);
printf("t=%e\n",t);
end = clock();
cost = (double)(end - begin) / CLOCKS_PER_SEC;
printf("%lf seconds\n", cost);
system("pause");
}
结果:14.60846
|
|