Differentiation

Differentiation is performed by descendants of o2scl::deriv_base. These allow one to calculate either first, second, and third derivatives. A GSL-based routine is used in o2scl::deriv_gsl, and the CERNLIB routine is used in o2scl::deriv_cern. For functions which are tabulated over equally-spaced abscissas, the class o2scl::deriv_eqi is provided which applies the formulas from Abramowitz and Stegun at a specified order. The class o2scl::deriv_cern is slower and sometimes more accurate, but also fails more often than o2scl::deriv_gsl, which never calls the error handler.

Warning: For o2scl::deriv_gsl and o2scl::deriv_cern, the second and third derivatives are calculated by naive repeated application of the code for the first derivative and can be particularly troublesome if the function is not sufficiently smooth. Error estimation is not provided for second and third derivatives.

Differentiation example

This example computes first and second derivatives of

\[ y(x) = \sin (2 x) + \frac{1}{2} \]

with both o2scl::deriv_gsl and o2scl::deriv_cern .

/* Example: ex_deriv.cpp
-------------------------------------------------------------------
An example to demonstrate numerical differentiation
*/
#include <cmath>
#include <boost/numeric/ublas/vector.hpp>
#include <o2scl/test_mgr.h>
#include <o2scl/funct.h>
#include <o2scl/deriv_gsl.h>
#include <o2scl/deriv_cern.h>
using namespace std;
using namespace o2scl;
class cl {
public:
// This is the function we'll take the derivative of
double function(double x) {
return sin(2.0*x)+0.5;
}
};
int main(void) {
// The class and associated function
cl acl;
funct f1=std::bind(std::mem_fn<double(double)>(&cl::function),
&acl,std::placeholders::_1);
// Note that the GSL derivative routine requires an initial stepsize
gd.h=1.0e-3;
// Compute the first derivative using the deriv_gsl class and
// verify that the answer is correct
double d1=gd.deriv(1.0,f1);
t.test_rel(d1,2.0*cos(2.0),1.0e-10,"deriv_gsl");
// Compute the first derivative using the deriv_cern class and
// verify that the answer is correct
double d2=cd.deriv(1.0,f1);
t.test_rel(d2,2.0*cos(2.0),1.0e-10,"deriv_cern");
// Compute the second derivative also
double d3=gd.deriv2(1.0,f1);
t.test_rel(d3,-4.0*sin(2.0),5.0e-7,"deriv_gsl");
double d4=cd.deriv2(1.0,f1);
t.test_rel(d4,-4.0*sin(2.0),1.0e-8,"deriv_cern");
t.report();
return 0;
}
// End of example
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::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::deriv_gsl::h
fp_t h
Initial stepsize.
Definition: deriv_gsl.h:144
o2scl::deriv_gsl
Numerical differentiation (GSL)
Definition: deriv_gsl.h:124
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::deriv_base< funct, double >::deriv
virtual double deriv(double x, funct &func)
Calculate the first derivative of func w.r.t. x.
Definition: deriv.h:106
o2scl::deriv_base< funct, double >::deriv2
virtual double deriv2(double x, funct &func)
Calculate the second derivative of func w.r.t. x.
Definition: deriv.h:118

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