cmlabs.optimize.test_root

cmlabs.optimize.test_root()[source]

Test the root finding function.

This test checks if the root function correctly identifies the root of a given function within a specified interval.

Results

>>> # Test 1: Root
>>> # f(x) = 2 * (x^2) - cos(2 * x)
>>> # f'(x) = 4 * x + 2 * sin(2 * x)
>>> # a = -10
>>> # b = 10
>>> find_root_brackets(f, -10, 10)
>>> # Intervals: [(np.float64(-0.612244897959183), np.float64(-0.204081632653061)), (np.float64(0.204081632653061), np.float64(0.612244897959183))]
>>> # BISECT:
>>> bisect(f, (np.float64(-0.612244897959183), np.float64(-0.204081632653061)))
>>> # Root: -0.5108455735809945
>>> bisect(f, (np.float64(0.204081632653061), np.float64(0.612244897959183)))
>>> # Root: 0.5108455735809945
>>> # NEWTON:
>>> newton(f, df, -0.612244897959183)
>>> # Root: -0.5108803890400834
>>> newton(f, df, 0.204081632653061)
>>> # Root: 0.5108803890400834
>>> # SECANT:
>>> secant(f, -0.612244897959183, -0.204081632653061)
>>> # Root: -0.5108449781771545
>>> secant(f, 0.204081632653061, 0.612244897959183)
>>> # Root: 0.5108448318004966