c语言积分问题请教
一个分段函数高斯积分,编写代码如下,正确结果是1,但是运行程序结果为0,但是编译既无错误也没有警告,不知道哪里出错,请高手帮忙看看,谢谢#include <stdio.h>
#include <math.h>
#define N 5
double gauss(double(*)(double), double, double, int);
double function(double x);
int main( )
{
double function(double);
double a, b, ans;
printf ("请输入积分上限b\n");
scanf("%f", &b);
printf("请输入积分下限a\n");
scanf("%f", &a);
ans=gauss(function, a, b, N);
printf("ans=%f", ans);
return 0;
}
double gauss(double(*func)(double x), double a, double b, int n )/*高斯求积*/
{
/*高斯点及其求积系数列表*/
double x1[1]={0.0};
double A1[1]={2};
double x2[2]={-0.5573503, 0.5573503};
double A2[2]={1, 1};
double x3[3]={-0.7745967, 0.0, 0.7745967};
double A3[3]={0.555556, 0.888889, 0.555556};
double x4[4]={0.3399810, -0.3399810, 0.8611363, -0.8611363};
double A4[4]={0.6521452, 0.6521452, 0.3478548, 0.3478548};
double x5[5]={0.0, 0.5384693, -0.5384693, 0.9061799, -0.9061799};
double A5[5]={0.5688889, 0.4786287, 0.4786287, 0.2369269, 0.2369269};
double *p, *t, g;
int i;
switch (n)
{
case 1:
p=x1;
t=A1;
break;
case 2:
p=x2;
t=A2;
break;
case 3:
p=x3;
t=A3;
break;
case 4:
p=x4;
t=A4;
break;
case 5:
p=x5;
t=A5;
break;
default:
printf ("intput wrong!");
}
for (i=0,g=0; i<n; i++){
g+=(*func)((b-a)*p[i]/2+(a+b)/2)*t[i];
}
g*=(b-a)/2;
return g;
}
double function(double x)
{
if(x>=-3 && x<=-2)
return (pow((x+3), 5)/120);
else if(x>=-2 && x<=-1)
return ((pow((x+3),5)-6*pow((x+2),5))/120);
else if(x>=-1 && x<=0)
return ((pow((x+3),5)-6*pow((x+2),5)+15*pow((x+1),5))/120);
else if(x>=0 && x<=1)
return ((pow((3-x),5)-6*pow((2-x),5)+15*pow((1-x),5))/120);
else if(x>=1 && x<=2)
return ((pow((3-x),5)-6*pow((2-x),5))/120);
else if(x>=2 && x<=3)
return (pow((3-x), 5)/120);
else
return 0;
}