numericalderivative.FirstDerivativeCentral

class numericalderivative.FirstDerivativeCentral(function, x, args=None)

Compute the first derivative using central finite difference formula

Methods

compute(step)

Compute first derivative using central finite difference.

compute_error(step[, ...])

Compute the total error for central finite difference for f'

compute_step([third_derivative_value, ...])

Compute the exact optimal step for central finite difference for f'.

get_function()

Return the function

get_x()

Return the input point

__init__(function, x, args=None)

Compute the first 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 first derivative using central finite difference formula

compute(step)

Compute first derivative using central finite difference.

compute_error(step[, ...])

Compute the total error for central finite difference for f'

compute_step([third_derivative_value, ...])

Compute the exact optimal step for central finite difference for f'.

get_function()

Return the function

get_x()

Return the input point

compute(step)

Compute first derivative using central finite difference.

This is based on the central finite difference formula (see (Faires & Burden, 1998) page 166) :

\[f'(x) = \frac{f(x + h) - f(x - h)}{2h} - \frac{h^2}{6} f'''(\xi)\]

where \(h > 0\) is the step and \(\xi \in (x, x + h)\).

Parameters:
stepfloat, > 0

The finite difference step

Returns:
first_derivativefloat

The approximate first derivative at point x.

References

  • Faires, J. D., & Burden, R. L. (1998). Numerical methods, 2d edition. Cengage Learning.

static compute_error(step, third_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{\epsilon_f}{h} + \frac{h^2}{6} |f'''(x)|\]

where \(\epsilon_f > 0\) is the absolute precision of the function evaluation.

Parameters:
stepfloat

The differentiation step h.

third_derivative_valuefloat

The value of the third 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(third_derivative_value=1.0, absolute_precision=1e-16)

Compute the exact optimal step for central finite difference for f'.

This is the step which is optimal to approximate the first derivative f'(x) using the central finite difference formula (see compute()). The optimal step is:

\[h^\star = \left( \frac{3 \epsilon_f}{|f'''(x)|} \right)^{1/3}\]

The total absolute error corresponding to the optimal step is:

\[e(h^\star) = \frac{1}{2} 3^{\frac{2}{3}} \left( \epsilon_f^2 |f'''(x)| \right)^{1/3}\]
Parameters:
third_derivative_valuefloat

The value of the third derivative 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 optimal differentiation step h.

absolute_errorfloat

The optimal absolute error.

get_function()

Return the function

Returns:
functionfunction

The function

get_x()

Return the input point

Returns:
xlist

The point