117.求定积分

举报
C语言与CPP编程 发表于 2022/05/05 22:05:47 2022/05/05
【摘要】 #include "stdio.h"#include "math.h" double fsimpf(x) /*要进行计算的被积函数*/double x;{ double y; y=log(1.0+x)/(1.0+x*x); return(y);} double fsimp(a,b,eps) /*辛普森算法*/double ...

  
  1. #include "stdio.h"
  2. #include "math.h"
  3. double fsimpf(x) /*要进行计算的被积函数*/
  4. double x;
  5. {
  6. double y;
  7. y=log(1.0+x)/(1.0+x*x);
  8. return(y);
  9. }
  10. double fsimp(a,b,eps) /*辛普森算法*/
  11. double a,b,eps; /*a为积分下限,b为积分上限,eps是希望达到的精度*/
  12. {
  13. int n,k;
  14. double h,t1,t2,s1,s2,ep,p,x;
  15. n=1; h=b-a;
  16. t1=h*(fsimpf(a)+fsimpf(b))/2.0; /*用梯形公式求出一个大概的估值*/
  17. s1=t1;
  18. ep=eps+1.0;
  19. while (ep>=eps)
  20. {
  21. /*用梯形法则计算*/
  22. p=0.0;
  23. for (k=0;k<=n-1;k++)
  24. {
  25. x=a+(k+0.5)*h;
  26. p=p+fsimpf(x);
  27. }
  28. t2=(t1+h*p)/2.0;
  29. /*用辛普森公式求精*/
  30. s2=(4.0*t2-t1)/3.0;
  31. ep=fabs(s2-s1);
  32. t1=t2; s1=s2; n=n+n; h=h/2.0;
  33. }
  34. return(s2);
  35. }
  36. main()
  37. {
  38. double a,b,eps,t;
  39. a=0.0; b=1.0; eps=0.0000001;
  40. clrscr();
  41. puts("**********************************************************");
  42. puts("* This program is to calculat the Value of *");
  43. puts("* a definite integral by Simpson Method. *");
  44. puts("**********************************************************");
  45. t=fsimp(a,b,eps);
  46. puts("\n----------------------------------------------------------");
  47. printf(" >> The result of definite integral is : \n");
  48. printf(" >> SIGMA(0,1)ln(1+x)/(1+x^2)dx = ");
  49. printf("%e\n",t);
  50. puts("----------------------------------------------------------");
  51. printf("\n Press any key to quit...");
  52. getch();
  53. }

文章来源: blog.csdn.net,作者:程序员编程指南,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/weixin_41055260/article/details/124576484

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200