请教:c语言怎样用辛普森法求cos x在-90度到90度的定积分

如题所述

本题一个完整的c程序如下,在win-tc和Dev-c++下通过:
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include "math.h"
#define PI 3.14159

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

double fsimp(a,b,eps) /*辛普森算法*/
double a,b,eps; /*a为积分下限,b为积分上限,eps是希望达到的精度*/
{
int n,k;
double h,t1,t2,s1,s2,ep,p,x;
n=1; h=b-a;
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);
}
main()
{
double a,b,eps,t;
a=-PI/2; b=PI/2; eps=0.0000001;
clrscr();
puts("**********************************************************");
puts("* This program is to calculat the Value of *");
puts("* a definite integral by Simpson Method. *");
puts("**********************************************************");
t=fsimp(a,b,eps);
puts("\n----------------------------------------------------------");
printf(" >> The result of definite integral is : \n");
printf(" >> SIGMA(-PI/2,PI/2)cos(x)dx = ");
printf("%e\n",t);
puts("----------------------------------------------------------");
printf("\n Press any key to quit...");
getch();
}
温馨提示:答案为网友推荐,仅供参考