
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples/example_basinhopping.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_basinhopping.py>`
        to download the full example code.

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

.. _sphx_glr_examples_example_basinhopping.py:


Fit comparing leastsq and basin hopping, or other methods
============================================================

This example compares the ``leastsq`` and ``basinhopping`` algorithms
on a decaying sine wave.  Note that this can be used to compare other
fitting algorithms too.

.. GENERATED FROM PYTHON SOURCE LINES 9-60



.. image-sg:: /examples/images/sphx_glr_example_basinhopping_001.png
   :alt: example basinhopping
   :srcset: /examples/images/sphx_glr_example_basinhopping_001.png, /examples/images/sphx_glr_example_basinhopping_001_3_00x.png 3.00x
   :class: sphx-glr-single-img


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

 .. code-block:: none

    # Fit using leastsq:
    [[Model]]
        Model(sine_decay)
    [[Fit Statistics]]
        # fitting method   = leastsq
        # function evals   = 31
        # data points      = 201
        # variables        = 4
        chi-square         = 37.4526504
        reduced chi-square = 0.19011498
        Akaike info crit   = -329.725713
        Bayesian info crit = -316.512493
        R-squared          = 0.97868751
    [[Variables]]
        amplitude:  12.4411385 +/- 0.18965087 (1.52%) (init = 10)
        frequency:  1.99852115 +/- 0.00324489 (0.16%) (init = 2)
        decay:      4.54866058 +/- 0.09723868 (2.14%) (init = 2)
        offset:     1.25370110 +/- 0.03107114 (2.48%) (init = 1)
    [[Correlations]] (unreported correlations are < 0.100)
        C(amplitude, decay)  = -0.7241
        C(amplitude, offset) = -0.1418

    #####################
    # Fit using basinhopping:
    [[Model]]
        Model(sine_decay)
    [[Fit Statistics]]
        # fitting method   = basinhopping
        # function evals   = 24078
        # data points      = 201
        # variables        = 4
        chi-square         = 37.4526504
        reduced chi-square = 0.19011498
        Akaike info crit   = -329.725713
        Bayesian info crit = -316.512493
        R-squared          = 0.97868751
    ##  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]]
        amplitude:  12.4411365 (init = 10)
        frequency:  1.99852108 (init = 2)
        decay:      4.54866141 (init = 2)
        offset:     1.25370114 (init = 1)






|

.. code-block:: Python


    import sys

    import matplotlib.pyplot as plt
    import numpy as np

    import lmfit

    VALID_METHODS = ['least_squares', 'differential_evolution', 'brute',
                     'basinhopping', 'ampgo', 'nelder', 'lbfgsb', 'powell', 'cg',
                     'newton', 'cobyla', 'bfgs', 'tnc', 'trust-ncg', 'trust-exact',
                     'trust-krylov', 'trust-constr', 'dogleg', 'slsqp', 'emcee',
                     'shgo', 'dual_annealing']


    def sine_decay(x, amplitude, frequency, decay, offset):
        return offset + amplitude * np.sin(x*frequency) * np.exp(-x/decay)


    x = np.linspace(0, 20, 201)
    np.random.seed(2)

    ydat = sine_decay(x, 12.5, 2.0, 4.5, 1.25) + np.random.normal(size=len(x), scale=0.40)

    model = lmfit.Model(sine_decay)
    params = model.make_params(amplitude={'value': 10, 'min': 0, 'max': 1000},
                               frequency={'value': 2.0, 'min': 0, 'max': 6.0},
                               decay={'value': 2.0, 'min': 0.001, 'max': 12},
                               offset=1.0)

    # fit with leastsq
    result0 = model.fit(ydat, params, x=x, method='leastsq')
    print("# Fit using leastsq:")
    print(result0.fit_report())

    method2 = 'basinhopping'
    if len(sys.argv) > 1 and sys.argv[1] in VALID_METHODS:
        method2 = sys.argv[1]


    # fit with other method
    result = model.fit(ydat, params, x=x, method=method2)
    print(f"\n#####################\n# Fit using {method2}:")
    print(result.fit_report())

    # plot comparison
    plt.plot(x, ydat, 'o', label='data')
    plt.plot(x, result0.best_fit, '+', label='leastsq')
    plt.plot(x, result.best_fit, '-', label=method2)
    plt.legend()
    plt.show()


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

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


.. _sphx_glr_download_examples_example_basinhopping.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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