example_Intp.txt

The following source file shows several calc::Mtx and calc::Intp objects applied. The source and output files demonstrate that data can be defined in an array of matrices calc::Mtx, and that this array of matrix data points can be interpolated between by calc::Intp interpolation objects. The objective in this example is to show that, when oscillating data is interpolated, interpolation error increases with data frequency.

Initially, an array of tabulated data points is generated which aids in forming the calc::Mtx matrix objects and the calc::Intp interpolation objects. Each matrix has 1 row and 3 columns. The first matrix column contains data at the lowest frequency. The frequency of the data is incremented for the other columns. Each matrix contains wave interpolation data for a specific location, there are 10 matrices which span the location space of the wave data.

The array of 10 calc::Mtx matrices are passed to one-dimensional interpolation objects which construct wave curves for each of the 3 frequencies. Three different interpolation methods are used: (1) calc::Cube (cubic splines); (2) calc::Nurb (non-uniform rational Bezier splines); (3) calc::Poly (polynomial curve-fit splines). Each interpolation object is evaluated at the same data point.

00001 
00002  #include "Mtx.h"                                      // matrix definitions
00003  #include "Intp.h"                                     // function interpolation objects
00004 
00005  using namespace std;                                  // standard C++ library namespace
00006  using namespace calc;                                 // CalcLib namespace
00007 
00008  typedef Mtx<float> MTXf;                              // alias for <yType>
00009 
00010  // results print function
00011  void results(ostream &ostr, const char *cDat, MTXf mtx)
00012     {ostr<<"   "<<cDat<<"  "<<mtx<<endl;}              // print out text and values
00013 
00014  int main(void)
00015  {
00016     // determine matrix size
00017     const int NPTS=10;                                 // number of data points to curve-fit
00018     const int NROW=1;                                  // number of rows in matrix
00019     const int NCOL=3;                                  // number of columns in matrix
00020 
00021     // set parameters
00022     float aFrq[NCOL];                                  // frequency of data in each column
00023     float xLoc[NPTS];                                  // distance data of waves across interpolation
00024     float aMtx[NPTS][NCOL];                            // matrix array for data
00025 
00026     // determine input data
00027     for(int iCol=0; iCol<NCOL; iCol++)
00028        aFrq[iCol]=2.0*Base::PI*(float)(iCol+1);        // frequency data across columns
00029     for(int iPt=0; iPt<NPTS; iPt++)
00030        xLoc[iPt]=(float)(iPt)/(float)(NPTS-1);         // location data across interpolation points
00031     for(int iPt=0; iPt<NPTS; iPt++)
00032        for(int iCol=0; iCol<NCOL; iCol++)
00033           aMtx[iPt][iCol]=sin(aFrq[iCol]*xLoc[iPt]);   // determine array data
00034 
00035     // initialize array of matrices for each interpolation point
00036     MTXf yMtx[NPTS];
00037     for(int iPt=0; iPt<NPTS; iPt++)
00038        yMtx[iPt]=MTXf(aMtx[iPt],NROW,NCOL); 
00039 
00040     // initialize interpolation objects with matrix data
00041     Cube< float,MTXf > cube(xLoc,yMtx,NPTS);
00042     Nurb< float,MTXf > nurb=cube;
00043     Poly< float,MTXf > poly=nurb;
00044 
00045     // calculate test point values for comparison
00046     const float xTst=0.38f;                            // test evaluation location for comparison
00047     float aTst[NCOL];                                  // test evaluation points for comparison
00048     for(int iCol=0; iCol<NCOL; iCol++)
00049        aTst[iCol]=std::sin(aFrq[iCol]*xTst);           // determine exact value of test point
00050     MTXf yTst(aTst,NROW,NCOL);                         // put test data in matrix object
00051     MTXf xFrq(aFrq,NROW,NCOL);                         // put frequency data in matrix object
00052 
00053     // compare polynomial evaluations at same point
00054     cout.precision(5);
00055     cout.setf(ios::showpoint,ios::showpoint);
00056     try {
00057        cout<<endl<<"   calc::Intp Class Example Application"<<endl<<endl;
00058        results(cout,"Wave function data frequencies:  ",xFrq); cout<<endl;
00059        results(cout,"calc::Cube interpolation Errors: ",cube.eval(xTst)-yTst);
00060        results(cout,"calc::Nurb interpolation Errors: ",nurb.eval(xTst)-yTst);
00061        results(cout,"calc::Poly interpolation Errors: ",poly.eval(xTst)-yTst);
00062        cout<<endl<<flush;
00063     }
00064     catch(IntpErr& intpErr) {cout<<intpErr<<endl; return 1;}
00065     catch(MtxErr& mtxErr) {cout<<mtxErr<<endl; return 1;}
00066     catch(...) {cout<<"Unknown execution error..."<<endl; return 1;}
00067 
00068     return 0;
00069  }
00070 

The following output file shows the error between the interpolation calculations and and the exact calculations. The input frequency values used in the calculations are given on the first line. Interpolation errors are then shown for eacn of the 3 interpolation objects at each of the 3 frequencies. As the data shows, interpolation error increases with data frequency.

00001 
00002    calc::Intp Class Example Application
00003 
00004    Wave function data frequencies:    {        6.2832,        12.566,        18.850}
00005 
00006    calc::Cube interpolation Errors:   {   -0.00040424,      0.014513,      -0.10721}
00007    calc::Nurb interpolation Errors:   {   -0.00048798,      0.012479,      -0.11576}
00008    calc::Poly interpolation Errors:   {    -0.0033833,      0.072701,      -0.25700}
00009 

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