f(x)=y , a=1 b=3.2
n=11
1.0 4.9
1.2 5.4
1.4 5.8
1.6 6.2
1.8 6.7
2.0 7.0
2.2 7.3
2.4 7.5
2.6 8.0
2.8 8.2
3.0 8.3
3.2 8.3
/* 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.141593
int main()
{
int n,i;
double a,b,tn,h ,sum=0.0;
n=11;
a=1;
b=3.2;
double x[12]={1.0 ,1.2 ,1.4, 1.6 , 1.8 , 2.0 , 2.2 ,2.4 , 2.6 , 2.8 , 3.0 ,3.2};
double y[12]={4.9 ,5.4 ,5.8, 6.2 , 6.7 , 7.0 , 7.3 ,7.5 , 8.0 , 8.2 , 8.3 ,8.3};
h=(b-a)/n;
for(i=1;i<n;i++)
{
sum=sum+y[i];
printf("i=%2d---y[%2d]=%0.4lf,sum=%0.4lf\n",i,i,y[i],sum);
}
tn=(h/2.0)*(y[0]+y[n]+2.0*sum);
printf("\nh=%0.2lf ",h);
printf("y[0]=%0.2lf ,y[%d]=%0.2lf ",y[0],n,y[n]);
printf("\n\ntn=(h/2.0)*(y[0]+y[n]+2.0*sum);\n");
printf("T%d=%10.6lf\n",n,tn);
return 0 ;
}
輸出畫面
i= 1---y[ 1]=5.4000,sum=5.4000
i= 2---y[ 2]=5.8000,sum=11.2000
i= 3---y[ 3]=6.2000,sum=17.4000
i= 4---y[ 4]=6.7000,sum=24.1000
i= 5---y[ 5]=7.0000,sum=31.1000
i= 6---y[ 6]=7.3000,sum=38.4000
i= 7---y[ 7]=7.5000,sum=45.9000
i= 8---y[ 8]=8.0000,sum=53.9000
i= 9---y[ 9]=8.2000,sum=62.1000
i=10---y[10]=8.3000,sum=70.4000
h=0.20 y[0]=4.90 ,y[11]=8.30
tn=(h/2.0)*(y[0]+y[n]+2.0*sum);
T11= 15.400000
沒有留言:
張貼留言