一、2021辛普森公式
![](https://i-blog.csdnimg.cn/blog_migrate/5f0fe1f7c65f2e054446ff4b72346741.png)
/*
* 使用 simpson 公式
*/
#include <stdio.h>
#include <math.h>
double f1(double x){
return sqrt(x+1);//填空
}
double f2(double x){
return exp(x);
}
double Simpson(double a,double b,double n,double (*f)(double)){ //填空
double h,area;
h=(b-a)/n;
area=f(0)+f(n); //填空
for(int i=1;i<n;i+=2){
area+=4*f(i); //填空
}
for (int i = 0; i < n; i+=2) {
area+=2*f(i); //填空
}
area = h*area/3;
return area;
}
int main(){
printf("answer1 = %lf\n", Simpson(3,8,10000,f1));
printf("answer2 = %lf\n", Simpson(2,5,10000,f2));
return 0;
}