numericalderivative.SecondDerivativeCentral¶
- class numericalderivative.SecondDerivativeCentral(function, x, args=None)¶
Compute the second derivative using central finite difference formula
Methods
compute(step)Compute an approximate second derivative using finite differences.
compute_error(step[, ...])Compute the total error for central finite difference for f''
compute_step([fourth_derivative_value, ...])Compute the optimal step for the central finite difference for f''.
Return the function
get_x()Return the input point
- __init__(function, x, args=None)¶
Compute the second derivative using central finite difference formula
- Parameters:
- functionfunction
The function to differentiate.
- xfloat
The point where the derivative is to be evaluated.
- argslist
A list of optional arguments that the function takes as inputs. By default, there is no extra argument and calling sequence of the function must be y = function(x). If there are extra arguments, then the calling sequence of the function must be y = function(x, arg1, arg2, ...) where arg1, arg2, ..., are the items in the args list.
- Returns:
- None.
Methods
__init__(function, x[, args])Compute the second derivative using central finite difference formula
compute(step)Compute an approximate second derivative using finite differences.
compute_error(step[, ...])Compute the total error for central finite difference for f''
compute_step([fourth_derivative_value, ...])Compute the optimal step for the central finite difference for f''.
Return the function
get_x()Return the input point
- compute(step)¶
Compute an approximate second derivative using finite differences.
The formula is (see (Faires & Burden, 1998) page 171):
\[f''(x) = \frac{f(x + h) - 2 f(x) + f(x - h)}{h^2} + \frac{h^2}{12} f^{(4)}(\xi)\]where \(h > 0\) is the step and \(\xi \in (x, x + h)\).
This second derivative can be used to compute an approximate optimal step for the forward first finite difference. Please use
compute_step()to do this.- Parameters:
- stepfloat
The step.
- Returns:
- second_derivativefloat
An estimate of f''(x).
References
Gill, P. E., Murray, W., Saunders, M. A., & Wright, M. H. (1983). Computing forward-difference intervals for numerical optimization. SIAM Journal on Scientific and Statistical Computing, 4(2), 310-321.
Faires, J. D., & Burden, R. L. (1998). Numerical methods, 2d edition. Cengage Learning.
- static compute_error(step, fourth_derivative_value=1.0, absolute_precision=1e-16)¶
Compute the total error for central finite difference for f''
The total error is the sum of the rounding error in the finite difference formula and the truncation error in the Taylor expansion:
\[e(h) = \frac{4 \epsilon_f}{h^2} + \frac{h^2}{12} \left|f^{(4)}(x)\right|\]where \(\epsilon_f > 0\) is the absolute precision of the function evaluation.
- Parameters:
- stepfloat
The differentiation step h.
- fourth_derivative_valuefloat
The value of the fourth derivative at point x.
- absolute_precisionfloat, optional
The absolute error of the function f at the point x. This is equal to abs(relative_precision * f(x)) where relative_precision is the relative accuracy and f(x) is the function value of f at point x.
- Returns:
- absolute_errorfloat
The optimal absolute error.
- static compute_step(fourth_derivative_value=1.0, absolute_precision=1e-16)¶
Compute the optimal step for the central finite difference for f''.
This step minimizes the total error of the second derivative central finite difference (see compute()). The optimal step is (see Eq. 8bis, page 314 in Gill, Murray, Saunders, & Wright, 1983)):
\[h^\star = \left(\frac{48 \epsilon_f}{|f^{(4)}(x)|} \right)^{1/4}\]The total absolute error corresponding to the optimal step is:
\[e(h^\star) = \frac{2 \sqrt{3}}{3} \sqrt{\epsilon_f |f^{(4)}(x)|}\]- Parameters:
- fourth_derivative_valuefloat
The value of the fourth derivative f^(4) at point x. If this value is unknown, we suggest to use the value 1 as an initial guess.
- absolute_precisionfloat, optional
The absolute error of the function f at the point x. This is equal to abs(relative_precision * f(x)) where relative_precision is the relative accuracy and f(x) is the function value of f at point x.
- Returns:
- optimal_stepfloat
The finite difference step.
- absolute_errorfloat
The absolute error.
- get_function()¶
Return the function
- Returns:
- functionfunction
The function
- get_x()¶
Return the input point
- Returns:
- xlist
The point