example_Intg.txt

The following source file shows several calc::Intg objects and functions applied with calc::Cpx variables. Initially, a test function along with its derivative are defined (dfn(x), fn(x)). Parameters are set indicating the integration interval of the function and the accuracy of the solutions. The classes, calc::Trap, calc::Simp, calc::Romb, use the test function dfn(x) to approximate the function fn(x) by summing increasingly smaller step-sizes until the indicated error is reached.

00001 
00002  #include "Cpx.h"                                      // complex variable definitions
00003  #include "Intg.h"                                     // function integral objects
00004 
00005  using namespace std;                                  // standard C++ library namespace
00006  using namespace calc;                                 // CalcLib namespace
00007 
00008  typedef Cpx<float> CPXf;                              // alias for <yType>
00009 
00010  // test function data
00011  const CPXf C1(-1.0f,-1.0f);                           // test function constant
00012  const CPXf C2(-1.0f, 1.0f);                           // test function constant
00013  CPXf  fn(float x) {return sin(C2*x)*C1;}              // test function
00014  CPXf dfn(float x) {return cos(C2*x)*C1*C2;}           // test function derivative
00015 
00016  // results print function
00017  void results(ostream &ostr, const char *cDat, CPXf cpf, int nCnt)
00018  {
00019     ostr<<"   "<<cDat<<" = ";                          // print out title
00020     cpf.stream(ostr)<<"  ("<<nCnt<<" iterations)";     // print data values
00021     ostr<<endl;                                        // end the text line
00022  }
00023 
00024  int main(void)
00025  {
00026     // set parameters
00027     const float xLo   =0.0f;                           // integration lower bound
00028     const float xHi   =0.25f*Base::PI;                 // integration upper bound
00029     const float yError=0.05f;                          // integration error
00030     CPXf y,yTrap,ySimp,yRomb;                          // evaluation variables
00031 
00032     Trap<float,CPXf> iTrap(dfn,yError);                // define trapezoid integration object
00033     Simp<float,CPXf> iSimp=iTrap;                      // define Simpson integration object
00034     Romb<float,CPXf> iRomb=iSimp;                      // define Romberg integration object
00035 
00036     // evaluate equivalent function at same point
00037     try {
00038        y=fn(xHi)-fn(xLo);                              // test function
00039        yTrap=iTrap.eval(xLo,xHi);                      // interpolated function
00040        ySimp=iSimp.eval(xLo,xHi);                      // differentiated function
00041        yRomb=iRomb.eval(xLo,xHi);                      // integrated function
00042     }
00043     catch(IntgErr& intgErr) {cout<<intgErr<<endl; return 1;}
00044     catch(...) {cout<<"Unknown execution error..."<<endl; return 1;}
00045 
00046     // print out evaluations comparing the functions
00047     cout.precision(5);
00048     cout.setf(ios::showpoint,ios::showpoint);
00049     cout<<endl<<"   calc::Intg Class Example Application"<<endl<<endl;
00050     cout<<"   exact evaluation of fn(x)       fn(x) = ";
00051        y.stream(cout)<<"  (exact)"<<endl;
00052     results(cout,"trapezoid estimate of fn(x)  iTrap(x)",yTrap,
00053        iTrap.getCount(Intg<float,CPXf>::LOOP_CURRENT));
00054     results(cout,"Simpson estimate of fn(x)    iSimp(x)",ySimp,
00055        iSimp.getCount(Intg<float,CPXf>::LOOP_CURRENT));
00056     results(cout,"Romberg estimate of fn(x)    iRomb(x)",yRomb,
00057        iRomb.getCount(Intg<float,CPXf>::LOOP_CURRENT));
00058     cout<<endl<<flush;
00059 
00060     return 0;
00061  }
00062 

The following output file shows the approximate agreement among the test function fn(x), the calc::Trap integration object using the trapezoid rule through calc::Intg::eval(), the calc::Simp integration object using Simpson's rule through calc::Intg::eval(), and the calc::Romb integration object using Romberg's method through calc::Intg::eval().

00001 
00002    calc::Intg Class Example Application
00003 
00004    exact evaluation of fn(x)       fn(x) = 1.5509+i0.32240  (exact)
00005    trapezoid estimate of fn(x)  iTrap(x) = 1.5488+i0.33236  (2 iterations)
00006    Simpson estimate of fn(x)    iSimp(x) = 1.5508+i0.32239  (2 iterations)
00007    Romberg estimate of fn(x)    iRomb(x) = 1.5509+i0.32240  (4 iterations)
00008 

Generated on Wed Jul 19 09:23:35 2006 for CalcLib by  doxygen 1.4.7