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

.. only:: html

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

        Click :ref:`here <sphx_glr_download_examples_example_fit_with_algebraic_constraint.py>`
        to download the full example code

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

.. _sphx_glr_examples_example_fit_with_algebraic_constraint.py:


Fit with Algebraic Constraint
=============================

.. GENERATED FROM PYTHON SOURCE LINES 6-58

.. code-block:: default

    import matplotlib.pyplot as plt
    from numpy import linspace, random

    from lmfit import Minimizer, Parameters
    from lmfit.lineshapes import gaussian, lorentzian
    from lmfit.printfuncs import report_fit


    def residual(pars, x, sigma=None, data=None):
        yg = gaussian(x, pars['amp_g'], pars['cen_g'], pars['wid_g'])
        yl = lorentzian(x, pars['amp_l'], pars['cen_l'], pars['wid_l'])

        slope = pars['line_slope']
        offset = pars['line_off']
        model = yg + yl + offset + x*slope

        if data is None:
            return model
        if sigma is None:
            return model - data
        return (model - data) / sigma


    random.seed(0)
    x = linspace(0.0, 20.0, 601)

    data = (gaussian(x, 21, 8.1, 1.2) +
            lorentzian(x, 10, 9.6, 2.4) +
            random.normal(scale=0.23, size=x.size) + x*0.5)

    pfit = Parameters()
    pfit.add(name='amp_g', value=10)
    pfit.add(name='cen_g', value=9)
    pfit.add(name='wid_g', value=1)
    pfit.add(name='amp_tot', value=20)
    pfit.add(name='amp_l', expr='amp_tot - amp_g')
    pfit.add(name='cen_l', expr='1.5+cen_g')
    pfit.add(name='wid_l', expr='2*wid_g')
    pfit.add(name='line_slope', value=0.0)
    pfit.add(name='line_off', value=0.0)

    sigma = 0.021  # estimate of data error (for all data points)

    myfit = Minimizer(residual, pfit, fcn_args=(x,),
                      fcn_kws={'sigma': sigma, 'data': data})

    result = myfit.leastsq()
    init = residual(pfit, x)
    fit = residual(result.params, x)

    report_fit(result)





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

 Out:

 .. code-block:: none

    [[Fit Statistics]]
        # fitting method   = leastsq
        # function evals   = 53
        # data points      = 601
        # variables        = 6
        chi-square         = 71878.3055
        reduced chi-square = 120.803875
        Akaike info crit   = 2887.26503
        Bayesian info crit = 2913.65660
    [[Variables]]
        amp_g:       21.1877707 +/- 0.32191819 (1.52%) (init = 10)
        cen_g:       8.11125925 +/- 0.01162984 (0.14%) (init = 9)
        wid_g:       1.20925847 +/- 0.01170853 (0.97%) (init = 1)
        amp_tot:     30.6003727 +/- 0.36481395 (1.19%) (init = 20)
        amp_l:       9.41260191 +/- 0.61672676 (6.55%) == 'amp_tot - amp_g'
        cen_l:       9.61125925 +/- 0.01162984 (0.12%) == '1.5+cen_g'
        wid_l:       2.41851694 +/- 0.02341706 (0.97%) == '2*wid_g'
        line_slope:  0.49615727 +/- 0.00170178 (0.34%) (init = 0)
        line_off:    0.04128604 +/- 0.02448064 (59.30%) (init = 0)
    [[Correlations]] (unreported correlations are < 0.100)
        C(amp_g, wid_g)         = 0.866
        C(amp_g, cen_g)         = 0.750
        C(line_slope, line_off) = -0.714
        C(cen_g, amp_tot)       = -0.695
        C(cen_g, wid_g)         = 0.623
        C(amp_g, amp_tot)       = -0.612
        C(amp_tot, line_off)    = -0.588
        C(wid_g, amp_tot)       = -0.412
        C(cen_g, line_off)      = 0.387
        C(amp_g, line_off)      = 0.183
        C(amp_g, line_slope)    = 0.183
        C(wid_g, line_slope)    = 0.174




.. GENERATED FROM PYTHON SOURCE LINES 59-63

.. code-block:: default

    plt.plot(x, data, '+')
    plt.plot(x, init, '--', label='initial fit')
    plt.plot(x, fit, '-', label='best fit')
    plt.legend()



.. image-sg:: /examples/images/sphx_glr_example_fit_with_algebraic_constraint_001.png
   :alt: example fit with algebraic constraint
   :srcset: /examples/images/sphx_glr_example_fit_with_algebraic_constraint_001.png, /examples/images/sphx_glr_example_fit_with_algebraic_constraint_001_3_0x.png 3.0x
   :class: sphx-glr-single-img






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

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


.. _sphx_glr_download_examples_example_fit_with_algebraic_constraint.py:


.. only :: html

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



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

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



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

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


.. only:: html

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

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