资源描述
复化辛普森求积公式的C++实现
N=8的情况,函数为f(x)=1/(1+x);积分区间为(0,1);
#include <iostream>
#include<cmath>
using namespace std;
const float pi=3.1415926;
float f(float x)
{
float y;
y=1/(1+x*x);
return y;
}
int main()
{
cout.setf(ios::fixed);
cout.precision(6);
float i;
float s=0,s1=0,h=1.0/10,y;
y=pi/4;
for(i=1;i<10;i++)
{
s=s+f(i*h);
}
for(i=1;i<11;i++)
{
s1=s1+f((2*i-1)*h/2);
}
s1=s1*4;
s=s*2;
s=s1+s+f(0)+f(1);
s=s*h/6;
cout<<s<<endl;
cout<<y-s;
return 0;
}
展开阅读全文