numericalderivative.FirstDerivativeForward

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

Compute the first derivative using forward finite difference formula

Methods

compute(step)

Compute an approximate first derivative using finite differences

compute_error(step[, ...])

Compute the total error for forward finite difference for f'.

compute_step([second_derivative_value, ...])

Compute the exact optimal step for forward 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 forward 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.

Examples

>>> import numericalderivative as nd
>>> import numpy as np
>>>
>>> def scaled_exp(x):
>>>     alpha = 1.e6
>>>     return np.exp(-x / alpha)
>>>
>>> x = 1.0
>>> formula = nd.FirstDerivativeForward(scaled_exp, x)
>>> step = 1.0e-3  # A first guess
>>> f_prime_approx = formula.compute(step)

Compute the step using an educated guess.

>>> second_derivative_value = 1.0  # A guess
>>> step, absolute_error = formula.compute_step(second_derivative_value)
>>> f_prime_approx = formula.compute(step)

Compute the absolute error from a given step.

>>> absolute_error = formula.compute_error(step, second_derivative_value)

Methods

__init__(function, x[, args])

Compute the first derivative using forward finite difference formula

compute(step)

Compute an approximate first derivative using finite differences

compute_error(step[, ...])

Compute the total error for forward finite difference for f'.

compute_step([second_derivative_value, ...])

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

get_function()

Return the function

get_x()

Return the input point

compute(step)

Compute an approximate first derivative using finite differences

This method uses the formula (see Eq. 1, page 311 in (GMS&W, 1983) and (Faires & Burden, 1998) page 164):

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

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

Parameters:
stepfloat, > 0

The finite difference 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, second_derivative_value=1.0, absolute_precision=1e-16)

Compute the total error for forward 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{2 \epsilon_f}{h} + \frac{h}{2} |f''(x)|\]

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

Parameters:
stepfloat

The differentiation step h.

second_derivative_valuefloat

The value of the second 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:
absolute_errorfloat

The optimal absolute error.

static compute_step(second_derivative_value=1.0, absolute_precision=1e-16)

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

This is the step which is optimal to approximate the first derivative f'(x) using the forward finite difference formula (see compute()). The optimal step is (see Eq. 6 in Gill, Murray, Saunders, & Wright, 1983)):

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

where \(\epsilon_f > 0\) is the absolute precision. The total absolute error corresponding to the optimal step is:

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

The value of the second 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