/* ex4-4.c based on Trapezoidol Rule is
* used for computing definite integral with
* domain [a,b] with n even-grids.
ex4-5.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 PI 3.141596
#define F(x) (exp(1/x))
void main()
{
int n,i,m;
double a,b,x,tn,h ,sum=0.0 , sn=0.0 ,sum1=0.0 ,sum2=0.0 ;
n=4;
a=1;
b=2;
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("exp(1/x) , a=1 , b=2 \n梯形積分法 : T%d=%10.6lf\n",n,tn);
//========================================================
n=4;
a=1;
b=2;
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;
}
輸出畫面
exp(1/x) , a=1 , b=2
梯形積分法 : T4= 2.031893
辛普森法 S4=2.020651
Command exited with non-zero status 25
沒有留言:
張貼留言