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

    Click :ref:`here <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.



.. image:: /examples/images/sphx_glr_example_diffev_001.png
    :class: sphx-glr-single-img


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

 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.96333090 +/- 0.04735921 (4.92%) (init = 2)
        omega:   3.98700821 +/- 0.02079710 (0.52%) (init = 3.3)
        amp:     1.80253577 +/- 0.19401989 (10.76%) (init = 2.5)
        decay:   5.76279825 +/- 1.04073303 (18.06%) (init = 1)
    [[Correlations]] (unreported correlations are < 0.100)
        C(amp, decay) = -0.755


    # Fit using differential_evolution:
    [[Fit Statistics]]
        # fitting method   = differential_evolution
        # function evals   = 1595
        # 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.96333094 +/- 0.04735903 (4.92%) (init = 2)
        omega:   3.98700859 +/- 0.02121806 (0.53%) (init = 3.3)
        amp:     1.80252931 +/- 0.19022466 (10.55%) (init = 2.5)
        decay:   5.73540163 +/- 1.00452879 (17.51%) (init = 1)
    [[Correlations]] (unreported correlations are < 0.100)
        C(amp, decay) = -0.743
    /Users/Newville/Codes/lmfit-py/examples/example_diffev.py:55: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
      plt.show()





|


.. code-block:: default

    import matplotlib.pyplot as plt
    import numpy as np

    import lmfit

    np.random.seed(2)
    x = np.linspace(0, 10, 101)

    # Setup example
    decay = 5
    offset = 1.0
    amp = 2.0
    omega = 4.0

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


    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


    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)

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

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

    plt.plot(x, yn, 'ko', lw=2)
    plt.plot(x, yn+o1.residual, 'r-', lw=2)
    plt.plot(x, yn+o2.residual, 'b--', lw=2)
    plt.legend(['data', 'leastsq', 'diffev'], loc='upper left')
    plt.show()


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

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


.. _sphx_glr_download_examples_example_diffev.py:


.. only :: html

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



  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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


.. only:: html

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

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