範例 EX4-3 梯行法求 (exp(-pow(x,2)/2)) / sqrt(2*3.14159) , x=-5 to 5
/* ex4-3.c based on Trapezoidol Rule is
* used for computing definite integral with
* domain [a,b] with n even-grids.
*/
#include <stdio.h>
#include <math.h>
#define PI 3.141596
#define F(x) (1.0/exp(x*x/2))
void main()
{
int n,i;
double a,b,x,tn,h ,sum=0.0;
scanf("n=%d a=%lf b=%lf",&n,&a,&b);
h=(b-a)/n;
x=a;
for(i=1;i<=n-1;i++)
{
x=x+h;
sum=sum+F(fabs(x));
}
tn=(1.0/sqrt(2*PI))*(h/2.0)*(F(fabs(a))+F(fabs(b))+2.0*sum);
printf("T%d=%10.6lf\n",n,tn);
return;
}
輸入資料
n=100 a=-5.0 b=5.0
輸出資料
T100= 0.999999
沒有留言:
張貼留言