.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "advanced/mathematical_optimization/auto_examples/plot_curve_fitting.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_advanced_mathematical_optimization_auto_examples_plot_curve_fitting.py: Curve fitting ============= A curve fitting example .. GENERATED FROM PYTHON SOURCE LINES 7-34 .. image-sg:: /advanced/mathematical_optimization/auto_examples/images/sphx_glr_plot_curve_fitting_001.png :alt: plot curve fitting :srcset: /advanced/mathematical_optimization/auto_examples/images/sphx_glr_plot_curve_fitting_001.png :class: sphx-glr-single-img .. code-block:: default import numpy as np import scipy as sp import matplotlib.pyplot as plt np.random.seed(0) # Our test function def f(t, omega, phi): return np.cos(omega * t + phi) # Our x and y data x = np.linspace(0, 3, 50) y = f(x, 1.5, 1) + .1*np.random.normal(size=50) # Fit the model: the parameters omega and phi can be found in the # `params` vector params, params_cov = sp.optimize.curve_fit(f, x, y) # plot the data and the fitted curve t = np.linspace(0, 3, 1000) plt.figure(1) plt.clf() plt.plot(x, y, 'bx') plt.plot(t, f(t, *params), 'r-') plt.show() .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.054 seconds) .. _sphx_glr_download_advanced_mathematical_optimization_auto_examples_plot_curve_fitting.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_curve_fitting.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_curve_fitting.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_