.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/optimization/scalar_search/plot_01_golden_section.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_optimization_scalar_search_plot_01_golden_section.py: Golden-section search: shrinking the bracket ================================================== Locates the minimum of a unimodal function on an interval. Each iteration costs one new function evaluation and shrinks the bracket by the factor :math:`1/\varphi \approx 0.618`. .. GENERATED FROM PYTHON SOURCE LINES 11-16 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.optimization import golden_section_search .. GENERATED FROM PYTHON SOURCE LINES 17-19 Minimize f(x) = x^2 - sin(4x) on [-1, 2] ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 19-32 .. code-block:: Python def f(x): return x**2 - np.sin(4.0 * x) result = golden_section_search(f, -1.0, 2.0, tol=1e-8) print(f"minimizer x = {result.x:.8f}, f(x) = {result.fun:.8f}") print(f"{result.iterations} iterations, {result.nfev} function evaluations") widths = result.brackets[:, 1] - result.brackets[:, 0] print(f"successive width ratio: {widths[1] / widths[0]:.6f} (1/phi = {(np.sqrt(5) - 1) / 2:.6f})") .. rst-class:: sphx-glr-script-out .. code-block:: none minimizer x = 0.34886654, f(x) = -0.86296114 41 iterations, 43 function evaluations successive width ratio: 0.618034 (1/phi = 0.618034) .. GENERATED FROM PYTHON SOURCE LINES 33-35 The first few brackets, and the width decay ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 35-48 .. code-block:: Python fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4)) xs = np.linspace(-1.0, 2.0, 400) ax1.plot(xs, f(xs), color="0.3") for i, (a, b) in enumerate(result.brackets[:8]): ax1.plot([a, b], [-1.2 - 0.12 * i] * 2, "|-", color="tab:blue") ax1.plot(result.x, result.fun, "o", color="tab:red") ax1.set_title("Brackets (top to bottom: iterations 0-7)") ax2.semilogy(widths, "o-", ms=3) ax2.set_xlabel("iteration") ax2.set_ylabel("bracket width") ax2.set_title("Width shrinks by 1/phi per step (Kiefer, 1953)") fig.tight_layout() .. image-sg:: /api/gallery/optimization/scalar_search/images/sphx_glr_plot_01_golden_section_001.png :alt: Brackets (top to bottom: iterations 0-7), Width shrinks by 1/phi per step (Kiefer, 1953) :srcset: /api/gallery/optimization/scalar_search/images/sphx_glr_plot_01_golden_section_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.114 seconds) .. _sphx_glr_download_api_gallery_optimization_scalar_search_plot_01_golden_section.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_golden_section.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_golden_section.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_golden_section.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_