discretize.tests.check_derivative#

discretize.tests.check_derivative(fctn, x0, num=7, plotIt=True, dx=None, expectedOrder=2, tolerance=0.85, eps=1e-10, ax=None)[source]#

Perform a basic derivative check.

Compares error decay of 0th and 1st order Taylor approximation at point x0 for a randomized search direction.

Parameters:
fctnfunction

Function handle

x0numpy.ndarray

Point at which to check derivative

numint, optional

number of times to reduce step length to evaluate derivative

plotItbool, optional

If True, plot the convergence of the approximation of the derivative

dxnumpy.ndarray, optional

Step direction. By default, this parameter is set to None and a random step direction is chosen.

expectedOrderint, optional

The expected order of convergence for the numerical derivative

tolerancefloat, optional

The tolerance on the expected order

epsfloat, optional

A threshold value for approximately equal to zero

axmatplotlib.pyplot.Axes, optional

An axis object for the convergence plot if plotIt = True. Otherwise, the function will create a new axis.

Returns:
bool

Whether you passed the test.

Examples

>>> from discretize import tests, utils
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> def simplePass(x):
...     return np.sin(x), utils.sdiag(np.cos(x))
>>> passed = tests.check_derivative(simplePass, np.random.randn(5))
==================== check_derivative ====================
iter    h         |ft-f0|   |ft-f0-h*J0*dx|  Order
---------------------------------------------------------
 0   1.00e-01    1.690e-01     8.400e-03      nan
 1   1.00e-02    1.636e-02     8.703e-05      1.985
 2   1.00e-03    1.630e-03     8.732e-07      1.999
 3   1.00e-04    1.629e-04     8.735e-09      2.000
 4   1.00e-05    1.629e-05     8.736e-11      2.000
 5   1.00e-06    1.629e-06     8.736e-13      2.000
 6   1.00e-07    1.629e-07     8.822e-15      1.996
========================= PASS! =========================
Once upon a time, a happy little test passed.

(Source code, png, pdf)

../../_images/discretize-tests-check_derivative-1.png