https://www.jdoodle.com/c-online-compiler
需切換 Version :
/* 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) (exp(x))
void main()
{
int n,i;
double a,b,x,tn,h ,sum=0.0;
n=10;
a=0;
b=1;
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(x) , a=0 , b=1 梯形積分法 : T%d=%10.6lf\n",n,tn);
printf("exp(x) , a=0 , b=1 實際積分值 :%10.6lf \n",fabs(exp(1)-1));
printf("實際值與梯形積分的誤差 :%10.6lf \n",fabs(tn-fabs(exp(1)-1)));
return;
}
輸出畫面
沒有留言:
張貼留言