.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/calculus/finite_differences/plot_02_fermat_adequality.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_calculus_finite_differences_plot_02_fermat_adequality.py: Fermat's adequality: maxima from a difference quotient ============================================================ Fermat found the maximum of a function by setting a difference quotient (f(x+h) - f(x))/h equal to zero and then letting h vanish. This example locates the maximum of Fermat's own problem, the largest product x(a - x), and shows the O(h) error of the forward difference. .. GENERATED FROM PYTHON SOURCE LINES 12-18 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from scipy.optimize import brentq from mathematicskit.calculus import forward_difference .. GENERATED FROM PYTHON SOURCE LINES 19-21 Fermat's problem: split a segment of length a to maximize x(a - x) --------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 21-34 .. code-block:: Python a = 10.0 def area(x): return x * (a - x) for h in (1.0, 0.1, 0.01): x_star = brentq(lambda x: forward_difference(area, x, h=h), 0.0, a) print(f"h = {h:5.2f}: difference quotient vanishes at x = {x_star:.4f}") print(f"letting h -> 0 gives x = a/2 = {a / 2}") .. rst-class:: sphx-glr-script-out .. code-block:: none h = 1.00: difference quotient vanishes at x = 4.5000 h = 0.10: difference quotient vanishes at x = 4.9500 h = 0.01: difference quotient vanishes at x = 4.9950 letting h -> 0 gives x = a/2 = 5.0 .. GENERATED FROM PYTHON SOURCE LINES 35-37 The forward difference is first-order accurate ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 37-47 .. code-block:: Python hs = np.logspace(-8, -1, 30) errors = [abs(forward_difference(np.sin, 1.0, h=h) - np.cos(1.0)) for h in hs] fig, ax = plt.subplots() ax.loglog(hs, errors, "o-", label="forward difference") ax.loglog(hs, 0.5 * np.sin(1.0) * hs, "--", label=r"$\frac{h}{2}|f''|$ truncation") ax.set_xlabel("step h") ax.set_ylabel("error in f'(1) for f = sin") ax.legend() ax.set_title("Truncation error falls as h, until rounding takes over") .. image-sg:: /api/gallery/calculus/finite_differences/images/sphx_glr_plot_02_fermat_adequality_001.png :alt: Truncation error falls as h, until rounding takes over :srcset: /api/gallery/calculus/finite_differences/images/sphx_glr_plot_02_fermat_adequality_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'Truncation error falls as h, until rounding takes over') .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.085 seconds) .. _sphx_glr_download_api_gallery_calculus_finite_differences_plot_02_fermat_adequality.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_fermat_adequality.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_fermat_adequality.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_fermat_adequality.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_