利用梯形求积公式求积分

类别:编程语言 点击:0 评论:0 推荐:

//梯形求积公式求积分

#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;

}

 

本文地址:http://com.8s8s.com/it/it28584.htm