00001 00002 #include "Cpx.h" // complex variable definitions 00003 #include "Cheb.h" // Chebyshev polynomial objects 00004 #include "PrtC.h" // auxiliary print stream object 00005 00006 using namespace std; // standard C++ library namespace 00007 using namespace calc; // CalcLib namespace 00008 00009 typedef Cpx<float> CPXf; // alias for <yType> 00010 00011 // test function data 00012 const CPXf C1(-1.0f,-1.0f); // test function constant 00013 const CPXf C2(-1.0f, 1.0f); // test function constant 00014 CPXf fn(float x) {return C1*sin(C2*x);} // test function 00015 CPXf dfn(float x) {return C1*C2*cos(C2*x);} // test function derivative 00016 CPXf ifn(float x) {return C1/C2*(1.0f-cos(C2*x));} // test function integral 00017 00018 // results print function 00019 void results(ostream &ostr, const char *cDat, CPXf cpf, int nCnt) 00020 { 00021 ostr<<" "<<cDat<<" = "; // print out title 00022 cpf.stream(ostr)<<" ("<<nCnt<<" coefficients)"; // print data values 00023 ostr<<endl; // end the text line 00024 } 00025 00026 int main(void) 00027 { 00028 // set parameters 00029 const float xLo =0.0f; // domain lower value 00030 const float xHi =0.5f*Base::PI; // domain upper value 00031 const float yError=0.05f; // polynomial curve-fit error 00032 CPXf y,y0,y1,y2; // evaluation variables 00033 00034 // determine interpolation coefficients 00035 Interp<float,CPXf> fn0( fn,xLo,xHi,yError); // curve-fit test function 00036 Interp<float,CPXf> ifn1(ifn,xLo,xHi,yError); // curve-fit integral test function 00037 Interp<float,CPXf> dfn2(dfn,xLo,xHi,yError); // curve-fit derivative test function 00038 00039 // Integrate or differentiate to get the same function 00040 Differ<float,CPXf> fn1=ifn1; // differentiate integral test function 00041 Integr<float,CPXf> fn2=dfn2; // integrate derivative test function 00042 00043 // evaluate equivalent function at same point 00044 const float xEval =0.25f*Base::PI; // evaluation point 00045 try { 00046 y=fn(xEval); // test function 00047 y0=fn0.eval(xEval); // interpolated function 00048 y1=fn1.eval(xEval); // differentiated function 00049 y2=fn2.eval(xEval); // integrated function 00050 } 00051 catch(ChebErr& chebErr) {cout<<chebErr<<endl; return 1;} 00052 catch(...) {cout<<"Unknown execution error..."<<endl; return 1;} 00053 00054 // print out evaluations comparing the functions 00055 cout.precision(5); 00056 cout.setf(ios::showpoint,ios::showpoint); 00057 cout<<endl<<" calc::Cheb Class Example Application"<<endl<<endl; 00058 cout<<" exact evaluation of fn(x) fn(x) = "; y.stream(cout)<<" (exact)"<<endl; 00059 results(cout,"interpolate fn(x) fn0(x)",y0,fn0.getCount()); 00060 results(cout,"interpolate derivative of ifn(x) fn1(x)",y1,fn1.getCount()); 00061 results(cout,"interpolate integral of dfn(x) fn2(x)",y2,fn2.getCount()); 00062 cout<<endl; 00063 00064 // print defining Chebyshev data in "C style" format 00065 PrtC pr; // create print object 00066 pr.setFormat("float","CPXf"); // identify labels for <xType,yType> 00067 pr.setFormat(pr.LABEL,"fn1(x)"); // set variable label 00068 pr.setFormat(pr.WIDTH,10); // set data field width 00069 pr.stream(cout,fn1); // send the Cheb data to the output stream 00070 cout<<flush; // clear print buffer 00071 00072 return 0; 00073 } 00074
The following output file shows the approximate agreement among the test function fn(x), the calc::Interp Chebyshev polynomial object fn0.eval(x), the calc::Differ Chebyshev polynomial object fn1.eval(x), and the calc::Integr Chebyshev polynomial object fn2.eval(x). The output file includes an output using a "C style" format of a calc::Differ object through the calc::PrtC object. All information required to duplicate the object fn1 is included in the "C style" printout.
00001 00002 calc::Cheb Class Example Application 00003 00004 exact evaluation of fn(x) fn(x) = 1.5509+i0.32240 (exact) 00005 interpolate fn(x) fn0(x) = 1.5630+i0.32558 (4 coefficients) 00006 interpolate derivative of ifn(x) fn1(x) = 1.5570+i0.32393 (3 coefficients) 00007 interpolate integral of dfn(x) fn2(x) = 1.5502+i0.32159 (5 coefficients) 00008 00009 00010 // fn1(x) Cheb Function Interpolation Parameters 00011 00012 // fn1(x) Low,High Interval Boundary Data Array 00013 const float xBound [ 2]={ float(0.00000e+00), float(1.57080e+00)}; 00014 00015 // fn1(x) (-1,+1) Chebyshev Interval Transformation Values 00016 const float xScale = float(1.27324e+00); 00017 const float xOffset = float(-1.00000e+00); 00018 00019 // fn1(x) Characteristic Data Point Count 00020 const int nPoints = int( 3); 00021 00022 // fn1(x) Polynomial Coefficient Data Array 00023 const CPXf arC [ 3]={ CPXf(2.84182e+00,1.58692e+00), CPXf(1.36633e+00,1.11217e+00), CPXf(-1.36061e-01,4.69531e-01)}; 00024
1.4.7