ROOT  6.06/09
Reference Guide
RichardsonDerivator.h
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Authors: David Gonzalez Maline 01/2008
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2006 , LCG ROOT MathLib Team *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Header file for RichardsonDerivator
12 //
13 // Created by: David Gonzalez Maline : Mon Feb 4 2008
14 //
15 
16 #ifndef ROOT_Math_RichardsonDerivator
17 #define ROOT_Math_RichardsonDerivator
18 
19 #include <Math/IFunction.h>
20 
21 /**
22  @defgroup Deriv Numerical Differentiation
23  Classes for numerical differentiation
24  @ingroup NumAlgo
25 */
26 
27 
28 namespace ROOT {
29 namespace Math {
30 
31 //___________________________________________________________________________________________
32 /**
33  User class for calculating the derivatives of a function. It can calculate first (method Derivative1),
34  second (method Derivative2) and third (method Derivative3) of a function.
35 
36  It uses the Richardson extrapolation method for function derivation in a given interval.
37  The method use 2 derivative estimates (one computed with step h and one computed with step h/2)
38  to compute a third, more accurate estimation. It is equivalent to the
39  <a href = http://en.wikipedia.org/wiki/Five-point_stencil>5-point method</a>,
40  which can be obtained with a Taylor expansion.
41  A step size should be given, depending on x and f(x).
42  An optimal step size value minimizes the truncation error of the expansion and the rounding
43  error in evaluating x+h and f(x+h). A too small h will yield a too large rounding error while a too large
44  h will give a large truncation error in the derivative approximation.
45  A good discussion can be found in discussed in
46  <a href=http://www.nrbook.com/a/bookcpdf/c5-7.pdf>Chapter 5.7</a> of Numerical Recipes in C.
47  By default a value of 0.001 is uses, acceptable in many cases.
48 
49  This class is implemented using code previosuly in TF1::Derivate{,2,3}(). Now TF1 uses this class.
50 
51  @ingroup Deriv
52 
53  */
54 
56 public:
57 
58  /** Destructor: Removes function if needed. */
60 
61  /** Default Constructor.
62  Give optionally the step size for derivation. By default is 0.001, which is fine for x ~ 1
63  Increase if x is in averga larger or decrease if x is smaller
64  */
65  RichardsonDerivator(double h = 0.001);
66 
67  /** Construct from function and step size
68  */
69  RichardsonDerivator(const ROOT::Math::IGenFunction & f, double h = 0.001, bool copyFunc = false);
70 
71  /**
72  Copy constructor
73  */
75 
76  /**
77  Assignment operator
78  */
80 
81 
82  /** Returns the estimate of the absolute Error of the last derivative calculation. */
83  double Error () const { return fLastError; }
84 
85 
86  /**
87  Returns the first derivative of the function at point x,
88  computed by Richardson's extrapolation method (use 2 derivative estimates
89  to compute a third, more accurate estimation)
90  first, derivatives with steps h and h/2 are computed by central difference formulas
91  Begin_Latex
92  D(h) = #frac{f(x+h) - f(x-h)}{2h}
93  End_Latex
94  the final estimate Begin_Latex D = #frac{4D(h/2) - D(h)}{3} End_Latex
95  "Numerical Methods for Scientists and Engineers", H.M.Antia, 2nd edition"
96 
97  the argument eps may be specified to control the step size (precision).
98  the step size is taken as eps*(xmax-xmin).
99  the default value (0.001) should be good enough for the vast majority
100  of functions. Give a smaller value if your function has many changes
101  of the second derivative in the function range.
102 
103  Getting the error via TF1::DerivativeError:
104  (total error = roundoff error + interpolation error)
105  the estimate of the roundoff error is taken as follows:
106  Begin_Latex
107  err = k#sqrt{f(x)^{2} + x^{2}deriv^{2}}#sqrt{#sum ai^{2}},
108  End_Latex
109  where k is the double precision, ai are coefficients used in
110  central difference formulas
111  interpolation error is decreased by making the step size h smaller.
112  */
113  double Derivative1 (double x) { return Derivative1(*fFunction,x,fStepSize); }
114  double operator() (double x) { return Derivative1(*fFunction,x,fStepSize); }
115 
116  /**
117  First Derivative calculation passing function object and step-size
118  */
119  double Derivative1(const IGenFunction & f, double x, double h);
120 
121  /// Computation of the first derivative using a forward formula
122  double DerivativeForward(double x) {
123  return DerivativeForward(*fFunction, x, fStepSize);
124  }
125 
126  /// Computation of the first derivative using a forward formula
127  double DerivativeForward(const IGenFunction &f, double x, double h);
128 
129  /// Computation of the first derivative using a bacward formula
130  double DerivativeBackward(double x) {
131  return DerivativeBackward(*fFunction, x, fStepSize);
132  }
133 
134  /// Computation of the first derivative using a forward formula
135  double DerivativeBackward(const IGenFunction &f, double x, double h) {
136  return DerivativeForward(f, x, -h);
137  }
138 
139  /**
140  Returns the second derivative of the function at point x,
141  computed by Richardson's extrapolation method (use 2 derivative estimates
142  to compute a third, more accurate estimation)
143  first, derivatives with steps h and h/2 are computed by central difference formulas
144  Begin_Latex
145  D(h) = #frac{f(x+h) - 2f(x) + f(x-h)}{h^{2}}
146  End_Latex
147  the final estimate Begin_Latex D = #frac{4D(h/2) - D(h)}{3} End_Latex
148  "Numerical Methods for Scientists and Engineers", H.M.Antia, 2nd edition"
149 
150  the argument eps may be specified to control the step size (precision).
151  the step size is taken as eps*(xmax-xmin).
152  the default value (0.001) should be good enough for the vast majority
153  of functions. Give a smaller value if your function has many changes
154  of the second derivative in the function range.
155 
156  Getting the error via TF1::DerivativeError:
157  (total error = roundoff error + interpolation error)
158  the estimate of the roundoff error is taken as follows:
159  Begin_Latex
160  err = k#sqrt{f(x)^{2} + x^{2}deriv^{2}}#sqrt{#sum ai^{2}},
161  End_Latex
162  where k is the double precision, ai are coefficients used in
163  central difference formulas
164  interpolation error is decreased by making the step size h smaller.
165  */
166  double Derivative2 (double x) {
167  return Derivative2( *fFunction, x, fStepSize);
168  }
169 
170  /**
171  Second Derivative calculation passing function and step-size
172  */
173  double Derivative2(const IGenFunction & f, double x, double h);
174 
175  /**
176  Returns the third derivative of the function at point x,
177  computed by Richardson's extrapolation method (use 2 derivative estimates
178  to compute a third, more accurate estimation)
179  first, derivatives with steps h and h/2 are computed by central difference formulas
180  Begin_Latex
181  D(h) = #frac{f(x+2h) - 2f(x+h) + 2f(x-h) - f(x-2h)}{2h^{3}}
182  End_Latex
183  the final estimate Begin_Latex D = #frac{4D(h/2) - D(h)}{3} End_Latex
184  "Numerical Methods for Scientists and Engineers", H.M.Antia, 2nd edition"
185 
186  the argument eps may be specified to control the step size (precision).
187  the step size is taken as eps*(xmax-xmin).
188  the default value (0.001) should be good enough for the vast majority
189  of functions. Give a smaller value if your function has many changes
190  of the second derivative in the function range.
191 
192  Getting the error via TF1::DerivativeError:
193  (total error = roundoff error + interpolation error)
194  the estimate of the roundoff error is taken as follows:
195  Begin_Latex
196  err = k#sqrt{f(x)^{2} + x^{2}deriv^{2}}#sqrt{#sum ai^{2}},
197  End_Latex
198  where k is the double precision, ai are coefficients used in
199  central difference formulas
200  interpolation error is decreased by making the step size h smaller.
201  */
202  double Derivative3 (double x) {
203  return Derivative3(*fFunction, x, fStepSize);
204  }
205 
206  /**
207  Third Derivative calculation passing function and step-size
208  */
209  double Derivative3(const IGenFunction & f, double x, double h);
210 
211  /** Set function for derivative calculation (copy the function if option has been enabled in the constructor)
212 
213  \@param f Function to be differentiated
214  */
215  void SetFunction (const IGenFunction & f);
216 
217  /** Set step size for derivative calculation
218 
219  \@param h step size for calculation
220  */
221  void SetStepSize (double h) { fStepSize = h; }
222 
223 protected:
224 
225  bool fFunctionCopied; // flag to control if function is copied in the class
226  double fStepSize; // step size used for derivative calculation
227  double fLastError; // error estimate of last derivative calculation
228  const IGenFunction* fFunction; // pointer to function
229 
230 };
231 
232 } // end namespace Math
233 
234 } // end namespace ROOT
235 
236 #endif /* ROOT_Math_RichardsonDerivator */
237 
double Derivative3(double x)
Returns the third derivative of the function at point x, computed by Richardson's extrapolation metho...
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition: IFunction.h:133
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
double DerivativeForward(double x)
Computation of the first derivative using a forward formula.
TH1 * h
Definition: legend2.C:5
double Derivative2(double x)
Returns the second derivative of the function at point x, computed by Richardson's extrapolation meth...
Double_t x[n]
Definition: legend1.C:17
void SetFunction(const IGenFunction &f)
Set function for derivative calculation (copy the function if option has been enabled in the construc...
double Derivative1(double x)
Returns the first derivative of the function at point x, computed by Richardson's extrapolation metho...
double DerivativeBackward(double x)
Computation of the first derivative using a bacward formula.
~RichardsonDerivator()
Destructor: Removes function if needed.
double DerivativeBackward(const IGenFunction &f, double x, double h)
Computation of the first derivative using a forward formula.
RichardsonDerivator(double h=0.001)
Default Constructor.
double f(double x)
Namespace for new Math classes and functions.
double Error() const
Returns the estimate of the absolute Error of the last derivative calculation.
RichardsonDerivator & operator=(const RichardsonDerivator &rhs)
Assignment operator.
void SetStepSize(double h)
Set step size for derivative calculation.
User class for calculating the derivatives of a function.