/* ex4-1.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) (x)
void main()
{
int n,i;
double a,b,x,tn,h ,sum=0.0;
n=100;
a=0;
b=10;
h=(b-a)/n;
x=a;
for(i=1;i<=n-1;i++)
{
x=x+h;
sum=sum+F(fabs(x));
}
tn=(h/2.0)*(F(fabs(a))+F(fabs(b))+2.0*sum);
printf("梯形積分法 : T%d=%10.6lf\n",n,tn);
x=a;
sum=0;
n=0;
while (x<b-h)
{
sum=sum + h* F(fabs(x));
x=x+h;
n=n+1;
}
printf("左端近似積分法 : T%d=%10.6lf\n",n,sum);
x=a+h;
sum=0;
n=0;
while (x<=b)
{
sum=sum + h* F(fabs(x));
x=x+h;
n=n+1;
}
printf("右端近似積分法 : T%d=%10.6lf\n",n,sum);
return;
}
輸出畫面
梯形積分法 : T100= 50.000000
左端近似積分法 : T100= 49.500000
右端近似積分法 : T100= 50.500000
...Program finished with exit code 40
Press ENTER to exit console.
沒有留言:
張貼留言