求助simpson计算三次多项式积分
为什么我的结果都是错的,我不知道哪里错了,谁能指点一下#include<iostream>
#include<cmath>
using namespace std;
class Jifeng
{
private:
double a , b , c , d , uplimit , downlimit , precision;
public:
void set();
double hanshu(double x);
double simpson();
void display();
};
#include<iostream>
#include<cmath>
void Jifeng::set()
{
cout<<"输入所需积分的函数的4个系数,以回车区分"<<'\n';
cin>>a>>b>>c>>d;
if(a==0&&b==0&&c==0&&d==0)
{
cout<<"您输入的函数为空,请重新输入"<<'\n';
set();
}
cout<<"输入所需积分的上限和下限,以回车区分"<<'\n';
cin>>uplimit>>downlimit;
cout<<"输入所需精度,以小数表示(如0.0001)"<<'\n';
cin>>precision;
}
double Jifeng::hanshu(double x)
{
return a*x*x*x+b*x*x+c*x+d;
}
double Jifeng::simpson()
{
double I2n=0 , h=uplimit-downlimit , T2n=h*(hanshu(uplimit)+hanshu(downlimit))/2 , In=T2n , Tn;
for(int n=1;abs(I2n-In)>precision;n+=n,h/=2.0)
{
In=I2n; Tn=T2n;
double sigma=0;
for(int k=0;k<n;k++)
{
sigma +=hanshu(a+(k+0.5)*h);
}
T2n=(Tn+h*sigma)/2.0;
I2n=(4*T2n-Tn)/3.0;
}
return I2n;
}
void Jifeng::display()
{
cout<<"您所求的积分是"<<simpson()<<'\n';
}
int main()
{
Jifeng demo;
demo.set();
demo.display();
demo.set();
}