習題 EX4-2 梯行法雙重積分---C語言
輸入資料
n=10 a=0 b=5.0
0 0
0.5 4.67
1.0 7.34
1.5 8.86
2.0 9.73
2.5 10.22
3.0 10.51
3.5 10.67
4.0 10.76
4.5 10.81
5.0 10.81
程式
/* p4-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>
void main()
{
int i,m,n;
double a,b,h,x[50],y[50],sum1=0.0,sum2=0.0,sn;
scanf("n=%d a=%lf b=%lf",&n,&a,&b);
for(i=0;i<=n;i++)
scanf("%lf %lf",&x[i],&y[i]);
m=n/2;
h=(b-a)/n;
for(i=1;i<=2*m-1;i++)
{
if(i%2==0)
sum2=sum2+y[i];
else
sum1=sum1+y[i];
sn=(h/3.0)*(y[0]+y[n]+2.0*sum2+4.0*sum1);
printf("S%d=%lf\n",n,sn);
}
//sn=(h/3.0)*(y[0]+y[n]+2.0*sum2+4.0*sum1);
//printf("S%d=%lf\n",n,sn);
return;
}
================
輸出結果
================S10=4.915000
S10=7.361667
S10=13.268333
S10=16.511667
S10=23.325000
S10=26.828333
S10=33.941667
S10=37.528333
S10=44.735000
沒有留言:
張貼留言