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

.. only:: html

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

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

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

.. _sphx_glr_examples_example_fit_multi_datasets.py:


Fit Multiple Data Sets
======================

Fitting multiple (simulated) Gaussian data sets simultaneously.

All minimizers require the residual array to be one-dimensional. Therefore, in
the ``objective`` function we need to ``flatten`` the array before returning it.

TODO: this could/should be using the Model interface / built-in models!

.. GENERATED FROM PYTHON SOURCE LINES 13-45

.. code-block:: default

    import matplotlib.pyplot as plt
    import numpy as np

    from lmfit import Parameters, minimize, report_fit


    def gauss(x, amp, cen, sigma):
        """Gaussian lineshape."""
        return amp * np.exp(-(x-cen)**2 / (2.*sigma**2))


    def gauss_dataset(params, i, x):
        """Calculate Gaussian lineshape from parameters for data set."""
        amp = params[f'amp_{i+1}']
        cen = params[f'cen_{i+1}']
        sig = params[f'sig_{i+1}']
        return gauss(x, amp, cen, sig)


    def objective(params, x, data):
        """Calculate total residual for fits of Gaussians to several data sets."""
        ndata, _ = data.shape
        resid = 0.0*data[:]

        # make residual per data set
        for i in range(ndata):
            resid[i, :] = data[i, :] - gauss_dataset(params, i, x)

        # now flatten this to a 1D array, as minimize() needs
        return resid.flatten()









.. GENERATED FROM PYTHON SOURCE LINES 46-47

Create five simulated Gaussian data sets

.. GENERATED FROM PYTHON SOURCE LINES 47-59

.. code-block:: default

    np.random.seed(2021)
    x = np.linspace(-1, 2, 151)
    data = []
    for _ in np.arange(5):
        params = Parameters()
        amp = 0.60 + 9.50*np.random.rand()
        cen = -0.20 + 1.20*np.random.rand()
        sig = 0.25 + 0.03*np.random.rand()
        dat = gauss(x, amp, cen, sig) + np.random.normal(size=x.size, scale=0.1)
        data.append(dat)
    data = np.array(data)








.. GENERATED FROM PYTHON SOURCE LINES 60-61

Create five sets of fitting parameters, one per data set

.. GENERATED FROM PYTHON SOURCE LINES 61-67

.. code-block:: default

    fit_params = Parameters()
    for iy, y in enumerate(data):
        fit_params.add(f'amp_{iy+1}', value=0.5, min=0.0, max=200)
        fit_params.add(f'cen_{iy+1}', value=0.4, min=-2.0, max=2.0)
        fit_params.add(f'sig_{iy+1}', value=0.3, min=0.01, max=3.0)








.. GENERATED FROM PYTHON SOURCE LINES 68-70

Constrain the values of sigma to be the same for all peaks by assigning
sig_2, ..., sig_5 to be equal to sig_1.

.. GENERATED FROM PYTHON SOURCE LINES 70-73

.. code-block:: default

    for iy in (2, 3, 4, 5):
        fit_params[f'sig_{iy}'].expr = 'sig_1'








.. GENERATED FROM PYTHON SOURCE LINES 74-75

Run the global fit and show the fitting result

.. GENERATED FROM PYTHON SOURCE LINES 75-78

.. code-block:: default

    out = minimize(objective, fit_params, args=(x, data))
    report_fit(out.params)





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

 Out:

 .. code-block:: none

    [[Variables]]
        amp_1:  6.32742039 +/- 0.02279129 (0.36%) (init = 0.5)
        cen_1:  0.68049265 +/- 0.00126458 (0.19%) (init = 0.4)
        sig_1:  0.25755568 +/- 4.9426e-04 (0.19%) (init = 0.3)
        amp_2:  6.98604785 +/- 0.02296772 (0.33%) (init = 0.5)
        cen_2:  0.50433700 +/- 0.00114536 (0.23%) (init = 0.4)
        sig_2:  0.25755568 +/- 4.9426e-04 (0.19%) == 'sig_1'
        amp_3:  7.11643543 +/- 0.02300454 (0.32%) (init = 0.5)
        cen_3: -0.08260273 +/- 0.00112437 (1.36%) (init = 0.4)
        sig_3:  0.25755568 +/- 4.9426e-04 (0.19%) == 'sig_1'
        amp_4:  6.10197450 +/- 0.02273463 (0.37%) (init = 0.5)
        cen_4:  0.07386098 +/- 0.00131130 (1.78%) (init = 0.4)
        sig_4:  0.25755568 +/- 4.9426e-04 (0.19%) == 'sig_1'
        amp_5:  9.23910596 +/- 0.02368909 (0.26%) (init = 0.5)
        cen_5:  0.34443079 +/- 8.6605e-04 (0.25%) (init = 0.4)
        sig_5:  0.25755568 +/- 4.9426e-04 (0.19%) == 'sig_1'
    [[Correlations]] (unreported correlations are < 0.100)
        C(sig_1, amp_5) = -0.374
        C(sig_1, amp_3) = -0.297
        C(sig_1, amp_2) = -0.292
        C(amp_1, sig_1) = -0.266
        C(sig_1, amp_4) = -0.258
        C(amp_3, amp_5) = 0.111
        C(amp_2, amp_5) = 0.109




.. GENERATED FROM PYTHON SOURCE LINES 79-80

Plot the data sets and fits

.. GENERATED FROM PYTHON SOURCE LINES 80-84

.. code-block:: default

    plt.figure()
    for i in range(5):
        y_fit = gauss_dataset(out.params, i, x)
        plt.plot(x, data[i, :], 'o', x, y_fit, '-')



.. image-sg:: /examples/images/sphx_glr_example_fit_multi_datasets_001.png
   :alt: example fit multi datasets
   :srcset: /examples/images/sphx_glr_example_fit_multi_datasets_001.png, /examples/images/sphx_glr_example_fit_multi_datasets_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.312 seconds)


.. _sphx_glr_download_examples_example_fit_multi_datasets.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_multi_datasets.py <example_fit_multi_datasets.py>`



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

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


.. only:: html

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

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