#include"iostream.h"
#include"math.h"
class tixing //梯形类
{
public:
tixing();//初始化
void SetValue(double LowLimit1,double UpLimit1);//设置积分上下限值
double f(double x);//函数f(x)
virtual double Result(void);//积分结果,虚函数
virtual void wucha(void);//误差范围,虚函数
protected:
double LowLimit;//上限
double UpLimit;//下限
};
tixing::tixing()
{
LowLimit=0.0;//初始值
UpLimit=0.0;
}
void tixing::SetValue(double LowLimit1,double UpLimit1)//设置函数
{
LowLimit=LowLimit1;
UpLimit=UpLimit1;
}
double tixing::f(double x)
{
return exp(-x);//本例f(x)为e^(-x)
}
double tixing::Result(void)
{
return (UpLimit-LowLimit)/2*(f(LowLimit)+f(UpLimit));//梯形公式
}
void tixing::wucha(void)
{
double x;
x=-pow((UpLimit-LowLimit),3)/12*(-f(LowLimit));//梯形误差公式
cout<<"梯形公式误差范围<="<<x<<endl;
}
class xinpusheng:public tixing{//辛浦生类,从梯形继承
public:
virtual double Result(void);//对结果重新定义(因为公式不同)
virtual void wucha(void);//同上,对误差重新定义
};
double xinpusheng::Result(void)//结果
{
return (UpLimit-LowLimit)*(f(LowLimit)+4*f((LowLimit+UpLimit)/2.0)+f(UpLimit))/6.0;
}
void xinpusheng::wucha(void)//误差
{
double x;
x=-pow((UpLimit-LowLimit),5)/2880*(-f(LowLimit));
cout<<"wucha:";
if (x<0){
x=-x;
}
cout<<"辛浦生公式误差范围<=:"<<x<<endl;
}
int main(void)
{
double LowLimit1=0.0,UpLimit1=0.0;
tixing tixing1;//梯形对象
xinpusheng xinpusheng1;//辛浦生对象
cout<<"f(x)=e^(-x)"<<endl;
cout<<"请输入积分上限:";
cin>>UpLimit1;
cout<<"请输入积分下限:";
cin>>LowLimit1;
tixing1.SetValue(LowLimit1,UpLimit1);//梯形赋值
xinpusheng1.SetValue(LowLimit1,UpLimit1);//辛浦生赋值
cout<<"积分结果: 梯形"<<tixing1.Result()<<endl;
tixing1.wucha();
cout<<"积分结果: 辛浦生"<<xinpusheng1.Result()<<endl;
xinpusheng1.wucha();
return 0;
}
#include"iostream.h"
#include"math.h"
class tixing //梯形类
{
public:
tixing();//初始化
void SetValue(double LowLimit1,double UpLimit1);//设置积分上下限值
double f(double x);//函数f(x)
virtual double Result(void);//积分结果,虚函数
virtual void wucha(void);//误差范围,虚函数
protected:
double LowLimit;//上限
double UpLimit;//下限
};
tixing::tixing()
{
LowLimit=0.0;//初始值
UpLimit=0.0;
}
void tixing::SetValue(double LowLimit1,double UpLimit1)//设置函数
{
LowLimit=LowLimit1;
UpLimit=UpLimit1;
}
double tixing::f(double x)
{
return exp(-x);//本例f(x)为e^(-x)
}
double tixing::Result(void)
{
return (UpLimit-LowLimit)/2*(f(LowLimit)+f(UpLimit));//梯形公式
}
void tixing::wucha(void)
{
double x;
x=-pow((UpLimit-LowLimit),3)/12*(-f(LowLimit));//梯形误差公式
cout<<"梯形公式误差范围<="<<x<<endl;
}
class xinpusheng:public tixing{//辛浦生类,从梯形继承
public:
virtual double Result(void);//对结果重新定义(因为公式不同)
virtual void wucha(void);//同上,对误差重新定义
};
double xinpusheng::Result(void)//结果
{
return (UpLimit-LowLimit)*(f(LowLimit)+4*f((LowLimit+UpLimit)/2.0)+f(UpLimit))/6.0;
}
void xinpusheng::wucha(void)//误差
{
double x;
x=-pow((UpLimit-LowLimit),5)/2880*(-f(LowLimit));
cout<<"wucha:";
if (x<0){
x=-x;
}
cout<<"辛浦生公式误差范围<=:"<<x<<endl;
}
int main(void)
{
double LowLimit1=0.0,UpLimit1=0.0;
tixing tixing1;//梯形对象
xinpusheng xinpusheng1;//辛浦生对象
cout<<"f(x)=e^(-x)"<<endl;
cout<<"请输入积分上限:";
cin>>UpLimit1;
cout<<"请输入积分下限:";
cin>>LowLimit1;
tixing1.SetValue(LowLimit1,UpLimit1);//梯形赋值
xinpusheng1.SetValue(LowLimit1,UpLimit1);//辛浦生赋值
cout<<"积分结果: 梯形"<<tixing1.Result()<<endl;
tixing1.wucha();
cout<<"积分结果: 辛浦生"<<xinpusheng1.Result()<<endl;
xinpusheng1.wucha();
return 0;
}
//计算方法,数值分析中的梯形积分公式,辛浦生积分公式,简称梯形公式,辛浦生公式求函数f(x)=e^(-x)在积分上下限//
//为1~0的范围内的积分,并算出了各自算法的误差.
//下面是对上面的代码简化
#include"iostream.h"
#include"math.h"
double f(double x)
{
return exp(-x);//本例f(x)为e^(-x)
}
double tixingResult(double UpLimit,double LowLimit)
{
return (UpLimit-LowLimit)/2*(f(LowLimit)+f(UpLimit));//梯形公式
}
void tixingwucha(double UpLimit,double LowLimit)
{
double x;
x=-pow((UpLimit-LowLimit),3)/12*(-f(LowLimit));//梯形误差公式
cout<<"梯形公式误差范围<="<<x<<endl;
}
double xinpushengResult(double UpLimit,double LowLimit)//结果
{
return (UpLimit-LowLimit)*(f(LowLimit)+4*f((LowLimit+UpLimit)/2.0)+f(UpLimit))/6.0;
}
void xinpushengwucha(double UpLimit,double LowLimit)//误差
{
double x;
x=-pow((UpLimit-LowLimit),5)/2880*(-f(LowLimit));
cout<<"wucha:";
if (x<0){
x=-x;
}
cout<<"辛浦生公式误差范围<=:"<<x<<endl;
}
int main(void)
{
double LowLimit,UpLimit;
cout<<"f(x)=e^(-x)"<<endl;
cout<<"请输入积分上限:";
cin>>UpLimit;
cout<<"请输入积分下限:";
cin>>LowLimit;
cout<<"积分结果: 梯形"<<tixingResult(UpLimit,LowLimit)<<endl;
tixingwucha(UpLimit,LowLimit);
cout<<"积分结果: 辛浦生"<<xinpushengResult(UpLimit,LowLimit)<<endl;
xinpushengwucha(UpLimit,LowLimit);
return 0;
}