我写了下面这个小程序,为什么在区间[-1,1]上积分没有结果?把区间改为[0,1]就正确了,请哪位高手指点一二,不胜感激!!! #include "stdafx.h" #include "iostream.h" #include "math.h" #include "iomanip.h"
double fun1(double x); double fun2(double x); double fun3(double x); double fun4(double x); double fun5(double x); double integrat(double (*fun)(double),double a,double b); void main() { int i,j; double xsh[2][2]; xsh[0][0]=integrat(fun1,-1.0,1.0); xsh[0][1]=integrat(fun2,-1,1.0); xsh[1][0]=integrat(fun2,-1,1.0); xsh[1][1]=integrat(fun3,-1,1.0); cout<<xsh[0][0]<<endl; cout<<xsh[0][1]<<endl; cout<<xsh[1][0]<<endl; cout<<xsh[1][1]<<endl; } double fun1(double x) { return 1; } double fun2(double x) { return x; } double fun3(double x) { return (1.0/3)*pow(x,3); } double fun4(double x) { return 9.0/pow((x+2),2); } double fun5(double x) { return x*9.0/pow((x+2),2); } double integrat(double (*fun)(double),double a,double b) { double s,h,y; int n,i; s=((*fun)(a)+(*fun)(b))/2.0; cout<<"s : "<<s<<endl; n=1000; h=(b-a)/n; for (i=1;i<n;i++) { s+=(*fun)(a+i*h); } y=s*h; return y; }