各位大哥大姐,说一下啊 我都风了 ,下面是程序:
程序功能是:给定一组X,Y数据点(未知表达式情况),求积分(一个区间上的);
/* psimpf_p.c*/
#include<stdio.h>
#include<math.h>
main(int argc,char *argv[])
{
int i,n;FILE *fp;
double a,b,s,fa,eps=1.e-06,x[30],y[30],simp();
if(argc<2) {puts("\nUsage:PSIMP-P date_filename\n");exit(1);}
if((fp=fopen(argv[1],"r"))==0) {printf("can't open date file\n");exit(1);}
fscanf(fp,"%d",&n);
for(i=1;i<=n;i++)
fscanf(fp,"%lf",&x[i]);
for(i=1;i<=n;i++)
fscanf(fp,"%lf",&y[i]);
fscanf(fp,"%lf%lf",&a,&b);
fclose(fp);
s=simp(n,a,b,x,y,eps);
printf("S=%12.6lfJ/mol\n",s);
}
double simp(m,a,b,x,y,eps)
double a,b,x[],y[],eps; int m;
{long int k,n;double la3();
double h,t1,t2,s,s1=0.0,s2,fa,fb,x0,y0,d;
n=1;h=b-a;
fa=la3(m,a,x,y);
fb=la3(m,b,x,y);
t1=0.5*h*(fa+fb);
do{s=0.0;
for(k=1;k<=n;k++)
{x0=a+(k-0.5)*h;
y0=la3(m,x0,x,y);
s+=y0;}
t2=0.5*(t1+h*s);
s2=t2+(t2-t1)/3.0;
d=fabs(s2-s1);
if(fabs(s2)>1.0) d=fabs((s2-s1)/s2);
if(d>=eps)
{n+=n;h*=0.5;t1=t2;s1=s2;}
}while(d>=eps);
return(s2);
}
double la3(n,t,x,y)
int n;double t,x[30],y[30];
{int i,j;double p,q,r,f;
for(j=3;j<n;j++)
{i=j;
if(t>x[i]) continue;
if(fabs(t-x[i-1])<=fabs(t-x[i])) {i=i-1;goto L1;}
}
i=n-2;
L1:
p=(t-x[i])*(t-x[i+1])/(x[i-1]-x[i])/(x[i-1]-x[i+1]);
q=(t-x[i-1])*(t-x[i+1])/(x[i]-x[i-1])/(x[i]-x[i-1]);
r=(t-x[i-1])*(t-x[i])/(x[i+1]-x[i-1])/(x[i+1]-x[i]);
f=p*y[i-1]+q*y[i]+r*y[i+1];
return(f);
}
DAT文件内容如下:
9
15 30 50 100 150 200 300 400 500
7.2 16.4 21.1 24.3 25.2 25.8 26.5 27.4 28.9
15 500
參考答案:先执行一下,再看有问题没,再修改,就是这样的.