分别用n=8,16,32的复合梯形和复合辛普森公式计算 根号X乘lnX 0到1的积分

利用复合梯形,复合辛普森公式计算定积分的近似值
2024-12-20 14:10:37
推荐回答(1个)
回答1:

代码如下,但是似乎你的题目有问题:
根号X乘lnX 0到1,根号0乘ln0等于0,根号1乘ln1等于1,怎么求?

#include
#include
#include

double fsimpf(double x) /*要进行计算的被积函数*/
{
double y;
y=log(x)*sqrt(x);
return(y);
}

double fsimp(double a,double b,double eps,int n) /*辛普森算法: a为积分下限,b为积分上限,eps是希望达到的精度*/
{
int k;
double h,t1,t2,s1,s2,ep,p,x;
h=(float)(b-a)/n;
t1=h*(fsimpf(a)+fsimpf(b))/2.0; /*用梯形公式求出一个大概的估值*/
s1=t1;
ep=eps+1.0;
while (ep>=eps)
{
/*用梯形法则计算*/
p=0.0;
for (k=0;k<=n-1;k++)
{
x=a+(k+0.5)*h;
p=p+fsimpf(x);
}
t2=(t1+h*p)/2.0;
/*用辛普森公式求精*/
s2=(4.0*t2-t1)/3.0;
ep=fabs(s2-s1);
t1=t2; s1=s2; n=n+n; h=h/2.0;
}
return(s2);
}

void main()
{
double a,b,eps,t;
a=0.0; b=1.0; eps=0.0000001;

printf("**********************************************************\n");
printf("* This program is to calculat the Value of *\n");
printf("* a definite integral by Simpson Method. *\n");
printf("**********************************************************\n");

printf("\n----------------------------------------------------------\n");
printf(" >> The result of definite integral is (n=8): \n");
printf(" >> SIGMA(0,1)sqrt(x)*ln(x)dx = ");
t=fsimp(a,b,eps,8);
printf("%e\n",t);
printf("----------------------------------------------------------\n");

printf("\n----------------------------------------------------------\n");
printf(" >> The result of definite integral is (n=16): \n");
printf(" >> SIGMA(0,1)sqrt(x)*ln(x)dx = ");
t=fsimp(a,b,eps,16);
printf("%e\n",t);
printf("----------------------------------------------------------\n");

printf("\n----------------------------------------------------------\n");
printf(" >> The result of definite integral is (n=32): \n");
printf(" >> SIGMA(0,1)sqrt(x)*ln(x)dx = ");
t=fsimp(a,b,eps,32);
printf("%e\n",t);
printf("----------------------------------------------------------\n");

system("pause");
}