
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/example_diffev.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_examples_example_diffev.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_examples_example_diffev.py:


Fit Using differential_evolution Algorithm
==========================================

This example compares the ``leastsq`` and ``differential_evolution`` algorithms
on a fairly simple problem.

.. GENERATED FROM PYTHON SOURCE LINES 9-25

.. code-block:: Python

    import matplotlib.pyplot as plt
    import numpy as np

    import lmfit


    def resid(params, x, ydata):
        decay = params['decay'].value
        offset = params['offset'].value
        omega = params['omega'].value
        amp = params['amp'].value

        y_model = offset + amp * np.sin(x*omega) * np.exp(-x/decay)
        return y_model - ydata









.. GENERATED FROM PYTHON SOURCE LINES 26-27

Generate synthetic data and set-up Parameters with initial values/boundaries:

.. GENERATED FROM PYTHON SOURCE LINES 27-43

.. code-block:: Python

    decay = 5
    offset = 1.0
    amp = 2.0
    omega = 4.0

    np.random.seed(2)
    x = np.linspace(0, 10, 101)
    y = offset + amp*np.sin(omega*x) * np.exp(-x/decay)
    yn = y + np.random.normal(size=y.size, scale=0.450)

    params = lmfit.Parameters()
    params.add('offset', 2.0, min=0, max=10.0)
    params.add('omega', 3.3, min=0, max=10.0)
    params.add('amp', 2.5, min=0, max=10.0)
    params.add('decay', 1.0, min=0, max=10.0)








.. GENERATED FROM PYTHON SOURCE LINES 44-45

Perform the fits and show fitting results and plot:

.. GENERATED FROM PYTHON SOURCE LINES 45-49

.. code-block:: Python

    o1 = lmfit.minimize(resid, params, args=(x, yn), method='leastsq')
    print("# Fit using leastsq:")
    lmfit.report_fit(o1)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    # Fit using leastsq:
    [[Fit Statistics]]
        # fitting method   = leastsq
        # function evals   = 65
        # data points      = 101
        # variables        = 4
        chi-square         = 21.7961792
        reduced chi-square = 0.22470288
        Akaike info crit   = -146.871969
        Bayesian info crit = -136.411487
    [[Variables]]
        offset:  0.96333089 +/- 0.04735890 (4.92%) (init = 2)
        omega:   3.98700839 +/- 0.02079709 (0.52%) (init = 3.3)
        amp:     1.80253587 +/- 0.19401928 (10.76%) (init = 2.5)
        decay:   5.76279753 +/- 1.04073348 (18.06%) (init = 1)
    [[Correlations]] (unreported correlations are < 0.100)
        C(amp, decay) = -0.7550




.. GENERATED FROM PYTHON SOURCE LINES 50-54

.. code-block:: Python

    o2 = lmfit.minimize(resid, params, args=(x, yn), method='differential_evolution')
    print("\n\n# Fit using differential_evolution:")
    lmfit.report_fit(o2)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none



    # Fit using differential_evolution:
    [[Fit Statistics]]
        # fitting method   = differential_evolution
        # function evals   = 1720
        # data points      = 101
        # variables        = 4
        chi-square         = 21.7961792
        reduced chi-square = 0.22470288
        Akaike info crit   = -146.871969
        Bayesian info crit = -136.411487
    ##  Warning: uncertainties could not be estimated:
        this fitting method does not natively calculate uncertainties
        and numdifftools is not installed for lmfit to do this. Use
        `pip install numdifftools` for lmfit to estimate uncertainties
        with this fitting method.
    [[Variables]]
        offset:  0.96333168 (init = 2)
        omega:   3.98700858 (init = 3.3)
        amp:     1.80253519 (init = 2.5)
        decay:   5.76281245 (init = 1)




.. GENERATED FROM PYTHON SOURCE LINES 55-59

.. code-block:: Python

    plt.plot(x, yn, 'o', label='data')
    plt.plot(x, yn+o1.residual, '-', label='leastsq')
    plt.plot(x, yn+o2.residual, '--', label='diffev')
    plt.legend()



.. image-sg:: /examples/images/sphx_glr_example_diffev_001.png
   :alt: example diffev
   :srcset: /examples/images/sphx_glr_example_diffev_001.png, /examples/images/sphx_glr_example_diffev_001_3_00x.png 3.00x
   :class: sphx-glr-single-img






.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.566 seconds)


.. _sphx_glr_download_examples_example_diffev.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: example_diffev.ipynb <example_diffev.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: example_diffev.py <example_diffev.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: example_diffev.zip <example_diffev.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
