5.7 Exercises 147
};
classGauss_Legendre:publicIntegral{
private:
static const doubleZERO = 1.0E-10;
static const doublePI = 3.14159265359;
doubleh;
public:
/*
@brief Constructor.
- @param lower_. Lower limit of integration.
@param upper. Upper limit of integration.
*@param npts. Number of points of integration.
@param f_. Reference to a functor representing the function to be integrated.
/
GaussLegendre(doublelower,doubleupper,intnpts, Function &f_);
//! Destructor
~Gauss_Legendre(){}
/
Evaluate the integral of a function f using the Gauss-Legendre approximation.
*@return The value of the integral in double precision.
**/
doubleevaluate();
};
#endif
5.7 Exercises.
5.1.Use Lagrange’s interpolation formula for a second-order polynomial
P 2 (x) =
(x−x 0 )(x−x 1 )
(x 2 −x 0 )(x 2 −x 1 )y^2 +
(x−x 0 )(x−x 2 )
(x 1 −x 0 )(x 1 −x 2 )y^1 +
(x−x 1 )(x−x 2 )
(x 0 −x 1 )(x 0 −x 2 )y^0 ,
and insert this formula in the integral
∫+h
−h
f(x)dx≈
∫+h
−h
P 2 (x)dx,
and derive Simpson’s rule. You need to define properly the valuesx 0 ,x 1 andx 2 and link them
with the integration limitsx 0 −handx 0 +h. Simpson’s formula reads
∫+h
−h
f(x)dx=
h
3 (fh+^4 f^0 +f−h)+O(h
(^5) ).
Write thereafter a class which implements both the Trapezoidal rule and Simpson’s rule. You
can for example follow the example given in the last section of this chapter. You can look up
the header file for this class athttp://folk.uio.no/mhjensen/compphys/programs/chapter05/cpp/Integral.h.
5.2.Write a program which then uses the above class containing the Trapezoidal rule and
Simpson’s rule to implement the adaptive algorithm discussed in section 5.2. Compute the
integrals