[求助] 微积分问题
#include <stdio.h>#include <math.h>
double integral(double(*p)(double),double a,double b,int n)
{
int i;
double x,h,s;
h=(a+b)/n;
x=a;
s=0;
for(i=0;i<n;i++)
{
x=x+h;
s=s+(*p)(x)*h;
}
return s;
}
void main()
{
integral(sin,0,1,1000);
printf("%f\n",sin);
}
求sin函数的面积。结果有问题~~~
----------------解决方案--------------------------------------------------------
以下是引用kailun在2006-12-28 9:45:00的发言:
#include <stdio.h>
#include <math.h>
double integral(double(*p)(double),double a,double b,int n)
{
int i;
double x,h,s;
h=(a+b)/n;
x=a;
s=0;
for(i=0;i<n;i++)
{
x=x+h;
s=s+(*p)(x)*h;
}
return s;
}
void main()
{
integral(sin,0,1,1000);
printf("%f\n",sin); \\整个改为printf("%f\n",intergral(sin,0,1,1000);
}
求sin函数的面积。结果有问题~~~
#include <stdio.h>
#include <math.h>
double integral(double(*p)(double),double a,double b,int n)
{
int i;
double x,h,s;
h=(a+b)/n;
x=a;
s=0;
for(i=0;i<n;i++)
{
x=x+h;
s=s+(*p)(x)*h;
}
return s;
}
void main()
{
integral(sin,0,1,1000);
printf("%f\n",sin); \\整个改为printf("%f\n",intergral(sin,0,1,1000);
}
求sin函数的面积。结果有问题~~~
----------------解决方案--------------------------------------------------------
呵呵,汗!到现在才看到原来函数还可以作为一个实参呢!integral()是怎么实现求面积的,我没有怎么看懂你写的,呵呵,辛苦楼主了!
----------------解决方案--------------------------------------------------------
可以考虑用概率分布来做.
----------------解决方案--------------------------------------------------------