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

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

.. _sphx_glr_examples_example_expression_model.py:


Using an ExpressionModel
========================

ExpressionModels allow a model to be built from a user-supplied expression.
See: https://lmfit.github.io/lmfit-py/builtin_models.html#user-defined-models

.. GENERATED FROM PYTHON SOURCE LINES 9-14

.. code-block:: Python

    import matplotlib.pyplot as plt
    import numpy as np

    from lmfit.models import ExpressionModel








.. GENERATED FROM PYTHON SOURCE LINES 15-16

Generate synthetic data for the user-supplied model:

.. GENERATED FROM PYTHON SOURCE LINES 16-23

.. code-block:: Python

    x = np.linspace(-10, 10, 201)
    amp, cen, wid = 3.4, 1.8, 0.5

    y = amp * np.exp(-(x-cen)**2 / (2*wid**2)) / (np.sqrt(2*np.pi)*wid)
    np.random.seed(2021)
    y = y + np.random.normal(size=x.size, scale=0.01)








.. GENERATED FROM PYTHON SOURCE LINES 24-25

Define the ``ExpressionModel`` and perform the fit:

.. GENERATED FROM PYTHON SOURCE LINES 25-28

.. code-block:: Python

    gmod = ExpressionModel("amp * exp(-(x-cen)**2 /(2*wid**2))/(sqrt(2*pi)*wid)")
    result = gmod.fit(y, x=x, amp=5, cen=5, wid=1)








.. GENERATED FROM PYTHON SOURCE LINES 29-30

this results in the following output:

.. GENERATED FROM PYTHON SOURCE LINES 30-32

.. code-block:: Python

    print(result.fit_report())





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

 .. code-block:: none

    [[Model]]
        <lmfit.ExpressionModel('amp * exp(-(x-cen)**2 /(2*wid**2))/(sqrt(2*pi)*wid)')>
    [[Fit Statistics]]
        # fitting method   = leastsq
        # function evals   = 52
        # data points      = 201
        # variables        = 3
        chi-square         = 0.01951689
        reduced chi-square = 9.8570e-05
        Akaike info crit   = -1851.19580
        Bayesian info crit = -1841.28588
        R-squared          = 0.99967271
    [[Variables]]
        amp:  3.40625133 +/- 0.00512077 (0.15%) (init = 5)
        cen:  1.80121155 +/- 8.6847e-04 (0.05%) (init = 5)
        wid:  0.50029616 +/- 8.6848e-04 (0.17%) (init = 1)
    [[Correlations]] (unreported correlations are < 0.100)
        C(amp, wid) = +0.5774




.. GENERATED FROM PYTHON SOURCE LINES 33-37

.. code-block:: Python

    plt.plot(x, y, 'o')
    plt.plot(x, result.init_fit, '--', label='initial fit')
    plt.plot(x, result.best_fit, '-', label='best fit')
    plt.legend()



.. image-sg:: /examples/images/sphx_glr_example_expression_model_001.png
   :alt: example expression model
   :srcset: /examples/images/sphx_glr_example_expression_model_001.png, /examples/images/sphx_glr_example_expression_model_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.355 seconds)


.. _sphx_glr_download_examples_example_expression_model.py:

.. only:: html

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

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

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

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

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

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

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


.. only:: html

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

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