一个求积分的程序大家过来帮我调试一下,告诉我问题出在哪里。
xenon 2004-05-12 10:11:14 /*
我想在一个文本文件中看到类似如下面的内容,
请问各位,我在哪里错了?
0.041666668782/2000
0.041666669115/3000
0.041666703421/5000
0.041666538664/8000
0.041666078587/12000
0.041668335449/50000
0.041677037592/50080
0.040106806765/500000
0.040106806765/500000
*/
#include "stdafx.h"
#include<stdio.h>
#include<math.h>
double Trapezoid(int toK,double *hSquared,
double (*fun)(double),
double a,double b)
{//梯形公式求积分:
double h,t,u=0,w,q;
int i;
h=fabs(b-a)/(double)toK;
*hSquared=h*h;
t=(*fun)(a)+(*fun)(b);
for(i=1;i<=toK-1;i++)
u=u+(*fun)(a+i*h);
w=t+2*u;
q=w*h/2.0;
return q;
}
double integrand1(double x)//被积分函数:
{
return 1.0/(1+x*x);
}
int main(void)//把结果输出到文件中:
{
FILE *fout;
double p,s,y;
int k;
double hSquared1;
double (*pf)(double);
fout=fopen("trapezoid.doc","w");
fprintf(fout,"For integrand: 1.0/(1+x*x)-->\n");
fclose(fout);
pf=integrand1;
for(k=1860;k<=1860;k=k+200)
{
y=Trapezoid(k,&hSquared1,pf,0.0,1.0);
s=atan((double)1);
p=s-y;
fout=fopen("trapezoid.doc","a");
fprintf(fout,"{[I-T(n)]/(h*h)}=%14.12f/%d\n",p/hSquared1,k);
fclose(fout);
}
printf("Your work has been finished!\n");
return 0;
}