C语言 高斯积分公式
#include <stdio.h>#include <math.h>
#define N 3
float gauss(float(*)(float), float, float, int);
void main( )
{
float function_name(float);
float a, b;
printf ("请输入积分上限b\n");
scanf("%f", &b);
printf("请输入积分下限a\n");
scanf("%f", &a);
float ans;
ans=gauss(function_name, a, b, N);
printf("ans=%f", ans);
}
float gauss(float(*func)(float x), float a, float b, int n )/*高斯求积*/
{
/*高斯点及其求积系数列表*/
float x1[1]={0.0};
float A1[1]={2};
float x2[2]={-0.5573503, 0.5573503};
float A2[2]={1, 1};
float x3[3]={-0.7745967, 0.0, 0.7745967};
float A3[3]={0.555556, 0.888889, 0.555556};
float x4[4]={0.3399810, -0.3399810, 0.8611363, -0.8611363};
float A4[4]={0.6521452, 0.6521452, 0.3478548, 0.3478548};
float x5[5]={0.0, 0.5384693, -0.5384693, 0.9061799, -0.9061799};
float A5[5]={0.5688889, 0.4786287, 0.4786287, 0.2369269, 0.2369269};
float *p, *t;
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!");
}
float g;
int i;
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;
}
float function_name(float x)
{
return (sqrt(4-x*x));
}
编译出错,求高手帮忙
[ 本帖最后由 sinodoo 于 2013-7-8 18:46 编辑 ]