//梯形求积公式求积分
#include"iostream.h"
#include"math.h"
class Simpson
{
public:
Simpson();
void SetValue(double LowerLimit1,double UpperLimit1);
double f(double x);
double ReturnResult(void);
void Deviation(void);
private:
double LowerLimit;
double UpperLimit;
};
//Constructor
Simpson::Simpson()
{
LowerLimit=0.0;
UpperLimit=0.0;
}
//Initialize varibles
void Simpson::SetValue(double LowerLimit1,double UpperLimit1)
{
LowerLimit=LowerLimit1;
UpperLimit=UpperLimit1;
}
//calculate the value of the function at x
double Simpson::f(double x)
{
return 5.0*pow(x,5.0)-3.0*pow(x,3.0)+2.0*pow(x,2.0)-10.0;
}
//Return the integral value
double Simpson::ReturnResult(void)
{
return (UpperLimit-LowerLimit)*(f(LowerLimit)+4*f((LowerLimit+UpperLimit)/2.0)+f(UpperLimit))/6.0;
}
//Return the deviation value
void Simpson::Deviation(void)
{
cout<<"Deviation:"<<endl<<-pow((UpperLimit-LowerLimit),2.0)/12.0<<"*f'(a)"<<endl;
cout<<LowerLimit<<"<=a<="<<UpperLimit<<endl;
}
int main(void)
{
double LowerLimit=0.0,UpperLimit=0.0;
Simpson simpson1;
cout<<"f(x)=5.0*x^5-3.0*x^3+2.0*x^2-10.0"<
cout<<"Please input the lower integral limit:"<
cin>>LowerLimit;
cout<<"Please input the upper integral limit:"<
cin>>UpperLimit;
simpson1.SetValue(LowerLimit,UpperLimit);
cout<<"Result:"<
simpson1.Deviation();
return 0;
}
附:
利用梯形求积公式求积分程序流程图: