需將 原方程式改寫
/* 習題 4-9-2.c based on Simpson's Rule to compute
* definite integral with domain [a,b] and
* n even-grid. n must be even.
*/
#include <stdio.h>
#include <math.h>
#define F(x) (x*exp(x)-x)
#define PI 3.14159
void main()
{
int i,m,n;
double a,b,h,x,sum1=0.0,sum2=0.0,sn;
//scanf("a=%lf b=%lf n=%d",&a,&b,&n);
a=0.0;
b=1.0;
n=10;
m=n/2;
h=(b-a)/n;
for(i=1;i<=2*m-1;i++)
{
x=a+i*h;
if(i%2==0)
sum2=sum2+F(x);
else
sum1=sum1+F(x);
}
sn=(h/3.0)*(F(a)+F(b)+2.0*sum2+4.0*sum1);
printf("S%d=%lf\n",n,sn);
return;
}
輸出結果
S10=0.058678
S10=0.061630
S10=0.075625
S10=0.088740
S10=0.131988
S10=0.164873
S10=0.259490
S10=0.324852
S10=0.500004
沒有留言:
張貼留言