需將 原方程式改寫
/* 習題 4-9-1.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) (0.5*pow(x,2))
#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=2.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);
}
//sn=(h/3.0)*(F(a)+F(b)+2.0*sum2+4.0*sum1);
//printf("S%d=%lf\n",n,sn);
return;
}
輸出結果
S10=0.138667
S10=0.149333
S10=0.197333
S10=0.240000
S10=0.373333
S10=0.469333
S10=0.730667
S10=0.901333
S10=1.333333
沒有留言:
張貼留言