.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/optimization/least_squares/plot_01_levenberg_marquardt.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_least_squares_plot_01_levenberg_marquardt.py: Levenberg-Marquardt: fitting a damped oscillation ======================================================= Fits a four-parameter damped cosine to noisy data. The residuals depend nonlinearly on the decay rate and frequency, so the fit needs an iterative method; Levenberg-Marquardt blends gradient descent (far from the solution) with Gauss-Newton (close to it). .. GENERATED FROM PYTHON SOURCE LINES 12-17 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.optimization import levenberg_marquardt .. GENERATED FROM PYTHON SOURCE LINES 18-20 Noisy data from a known model ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 20-32 .. code-block:: Python rng = np.random.default_rng(0) t = np.linspace(0.0, 10.0, 120) true = np.array([2.0, 0.3, 1.5, 0.4]) # amplitude, decay, frequency, phase def model(p, t): return p[0] * np.exp(-p[1] * t) * np.cos(p[2] * t + p[3]) y = model(true, t) + 0.05 * rng.normal(size=t.size) .. GENERATED FROM PYTHON SOURCE LINES 33-35 Fit from a rough initial guess ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 35-49 .. code-block:: Python x0 = [1.0, 0.1, 1.3, 0.0] result = levenberg_marquardt(lambda p: model(p, t) - y, x0) print(f"true parameters: {true}") print(f"fitted parameters: {result.x.round(4)}") print(f"residual cost 0.5*||r||^2 = {result.cost:.4f} after {result.nfev} evaluations") fig, ax = plt.subplots() ax.plot(t, y, ".", color="0.5", label="data") ax.plot(t, model(x0, t), "--", label="initial guess") ax.plot(t, model(result.x, t), label="Levenberg-Marquardt fit") ax.set_xlabel("t") ax.legend() ax.set_title("Nonlinear least squares (Levenberg 1944, Marquardt 1963)") .. image-sg:: /api/gallery/optimization/least_squares/images/sphx_glr_plot_01_levenberg_marquardt_001.png :alt: Nonlinear least squares (Levenberg 1944, Marquardt 1963) :srcset: /api/gallery/optimization/least_squares/images/sphx_glr_plot_01_levenberg_marquardt_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none true parameters: [2. 0.3 1.5 0.4] fitted parameters: [2.0297 0.3011 1.5011 0.4084] residual cost 0.5*||r||^2 = 0.1332 after 8 evaluations Text(0.5, 1.0, 'Nonlinear least squares (Levenberg 1944, Marquardt 1963)') .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.031 seconds) .. _sphx_glr_download_api_gallery_optimization_least_squares_plot_01_levenberg_marquardt.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_levenberg_marquardt.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_levenberg_marquardt.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_levenberg_marquardt.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_