Chebyshev Approximation

A class implementing the Chebyshev approximations based on GSL is given in o2scl::cheb_approx_tl. This class has its own copy constructor, so that Chebyshev approximations can be copied and passed as arguments to functions. Derivatives and integrals of o2scl::cheb_approx_tl objects are created as new o2scl::cheb_approx_tl objects which can be easily manipulated.

Chebyshev approximation example

This example performs an approximation of the function $ y=\sin\left[ 1/\left(x+0.08 \right) \right] $ over $ [0,2 \pi] $ . This function oscillates strongly over this interval and requires a high order approximation to be accurate.

The image below shows the approximation for $ n=50 $ and $ n=25 $ . The $ n=100 $ would be nearly indistinguishable from the exact result on this scale.

Chebyshev approximation plot
/* Example: ex_chebapp.cpp
-------------------------------------------------------------------
*/
#include <iostream>
#include <o2scl/constants.h>
#include <o2scl/test_mgr.h>
#include <o2scl/cheb_approx.h>
#include <o2scl/deriv_cern.h>
#include <o2scl/inte_qag_gsl.h>
using namespace std;
using namespace o2scl;
double func(double x) {
return sin(1.0/(x+0.08));
}
double dfunc(double x) {
return -cos(1.0/(x+0.08))/pow(x+0.08,2.0);
}
// Simple function to output information to file for plotting
void write_file(cheb_approx &gc);
int main(void) {
cout.setf(ios::scientific);
funct tf=func;
double res, err;
double x0=0.55;
// Initialize the Chebyshev approximation
gc.init(func,100,0.0,2.0*o2scl_const::pi);
// Evaluate the approximation and compare with the exact result
cout << "f(0.55)" << endl;
cout << "Exact : " << func(x0) << endl;
gc.eval_err(x0,res,err);
cout << "Approx (n=100): " << res << endl;
cout << " Est. Error : " << err << endl;
cout << " Act. Error : " << fabs(res-func(x0)) << endl;
// Evaluate the approximation at lower order
gc.eval_n_err(50,x0,res,err);
cout << "Approx (n=50) : " << res << endl;
cout << " Est. Error : " << err << endl;
cout << " Act. Error : " << fabs(res-func(x0)) << endl;
gc.eval_n_err(25,x0,res,err);
cout << "Approx (n=25) : " << res << endl;
cout << " Est. Error : " << err << endl;
cout << " Act. Error : " << fabs(res-func(x0)) << endl;
cout << endl;
t.test_rel(gc.eval(x0),func(x0),1.0e-4,"eval");
// Show how to use operator=() to create a new approximation
cheb_approx gc2=gc;
cout << "Using operator=(): " << gc2.eval(x0) << " " << func(x0) << endl;
cout << endl;
t.test_rel(gc2.eval(x0),gc.eval(x0),1.0e-10,"op=");
// Show how to compute the derivative
cheb_approx gc_deriv;
gc.deriv(gc_deriv);
cout << "f'(0.55)" << endl;
cout << "Exact : " << dfunc(x0) << endl;
gc_deriv.eval_err(x0,res,err);
cout << "Approx (n=100): " << res << endl;
cout << " Est. Error : " << err << endl;
cout << " Act. Error : " << fabs(res-dfunc(x0)) << endl;
cd.deriv_err(x0,tf,res,err);
cout << "Direct deriv : " << res << endl;
cout << " Est. Error : " << err << endl;
cout << " Act. Error : " << fabs(res-dfunc(x0)) << endl;
cout << endl;
t.test_abs(res,dfunc(x0),1.0e-12,"deriv with deriv_cern");
t.test_abs(gc_deriv.eval(x0),dfunc(x0),5.0e-3,"deriv with cheb");
// Show how to compute the integral
cheb_approx gc_integ;
gc.integ(gc_integ);
cout << "int(f,0,0.55)" << endl;
gc_integ.eval_err(x0,res,err);
cout << "Approx (n=100): " << res << endl;
cout << " Est. Error : " << err << endl;
gi.integ_err(tf,0.0,x0,res,err);
cout << "Direct integ : " << res << endl;
cout << " Est. Error : " << err << endl;
cout << "Rel. Error : " << fabs(res-gc_integ.eval(x0)) << endl;
cout << endl;
t.test_abs(gc_integ.eval(x0),gi.integ(tf,0.0,x0),1.0e-6,"integral");
write_file(gc);
t.report();
return 0;
}
// End of example
o2scl::deriv_cern::deriv_err
virtual int deriv_err(double x, func_t &func, double &dfdx, double &err)
Calculate the first derivative of func w.r.t. x and the uncertainty.
Definition: deriv_cern.h:158
o2scl::inte_qag_gsl::integ_err
virtual int integ_err(func_t &func, double a, double b, double &res, double &err)
Integrate function func from a to b and place the result in res and the error in err.
Definition: inte_qag_gsl.h:95
o2scl::inte_qag_gsl
Adaptive numerical integration of a function (without singularities) on a bounded interval (GSL)
Definition: inte_qag_gsl.h:80
o2scl::cheb_approx_tl::eval
fp_t eval(fp_t x) const
Evaluate the approximation.
Definition: cheb_approx.h:216
o2scl::cheb_approx_tl::deriv
void deriv(cheb_approx_tl &gc) const
Make gc an approximation to the derivative.
Definition: cheb_approx.h:405
o2scl_const::pi
const double pi
Definition: constants.h:65
o2scl
The main O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl names...
Definition: anneal.h:42
o2scl::test_mgr::report
bool report() const
Provide a report of all tests so far.
o2scl::cheb_approx_tl::eval_n_err
void eval_n_err(size_t n, fp_t x, fp_t &result, fp_t &abserr)
Evaluate the approximation to a specified order and give the uncertainty.
Definition: cheb_approx.h:308
o2scl::test_mgr::test_rel
bool test_rel(data_t result, data_t expected, data_t rel_error, std::string description)
Test for .
Definition: test_mgr.h:168
o2scl::test_mgr
A class to manage testing and record success and failure.
Definition: test_mgr.h:50
o2scl::cheb_approx_tl::init
void init(func_t &func, size_t ord, fp_t a1, fp_t b1)
Initialize a Chebyshev approximation of the function func over the interval from a1 to b1.
Definition: cheb_approx.h:120
o2scl::inte< funct >::integ
virtual double integ(funct &func, double a, double b)
Integrate function func from a to b.
Definition: inte.h:92
o2scl::deriv_cern
Numerical differentiation routine (CERNLIB)
Definition: deriv_cern.h:94
o2scl::test_mgr::set_output_level
void set_output_level(int l)
Set the output level.
Definition: test_mgr.h:119
o2scl::funct
std::function< double(double)> funct
One-dimensional function typedef in src/base/funct.h.
Definition: funct.h:48
o2scl::test_mgr::test_abs
bool test_abs(data_t result, data_t expected, data_t abs_error, std::string description)
Test for .
Definition: test_mgr.h:131
o2scl::cheb_approx_tl::integ
void integ(cheb_approx_tl &gc) const
Make gc an approximation to the integral.
Definition: cheb_approx.h:439
o2scl::cheb_approx_tl
Chebyshev approximation (GSL)
Definition: cheb_approx.h:49
o2scl::cheb_approx_tl::eval_err
void eval_err(fp_t x, fp_t &result, fp_t &abserr)
Evaluate the approximation and give the uncertainty.
Definition: cheb_approx.h:271

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).