#include"math.h"
double f(double x)
{ double f1;
f1=4/(1+x*x);
return(f1) ;
}
double trapezia2(int k,double h,double a,double tn)
{ double t2n,s=0;
int i;
t2n=tn/2;
for(i=1;i<=pow(2,k);i++)
{
s+=f(a+(2*i-1)*h);
}
t2n=t2n+s*h;
return t2n;
}
void main()
{double a,b,h,Tn,T2n,e;
int n,k;
printf("please input a,b,n,e\n");
scanf("%lf%lf%d%lf",&a,&b,&n,&e);
h=b-a;
Tn=(b-a)*(f(a)+f(b))/2;
for(k=0;k<n;k++)
{ h=h/2;
T2n=trapezia2(k,h,a,Tn);
if(fabs(T2n-Tn)<e)
break;
printf("%lf\n",Tn);
Tn=T2n;
}
printf("%lf\n",Tn);
}
评论0
最新资源