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

    Click :ref:`here <sphx_glr_download_examples_documentation_builtinmodels_stepmodel.py>` to download the full example code
.. rst-class:: sphx-glr-example-title

.. _sphx_glr_examples_documentation_builtinmodels_stepmodel.py:


doc_builtinmodels_stepmodel.py
==============================



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


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

 Out:

 .. code-block:: none

    [[Model]]
        (Model(step, prefix='step_', form='erf') + Model(linear, prefix='line_'))
    [[Fit Statistics]]
        # fitting method   = leastsq
        # function evals   = 49
        # data points      = 201
        # variables        = 5
        chi-square         = 593.709622
        reduced chi-square = 3.02913072
        Akaike info crit   = 227.700173
        Bayesian info crit = 244.216698
    [[Variables]]
        line_slope:      1.87164656 +/- 0.09318713 (4.98%) (init = 0)
        line_intercept:  12.0964833 +/- 0.27606235 (2.28%) (init = 11.58574)
        step_amplitude:  112.858376 +/- 0.65392947 (0.58%) (init = 134.7378)
        step_center:     3.13494792 +/- 0.00516615 (0.16%) (init = 2.5)
        step_sigma:      0.67392841 +/- 0.01091168 (1.62%) (init = 1.428571)
    [[Correlations]] (unreported correlations are < 0.100)
        C(line_slope, step_amplitude)     = -0.879
        C(step_amplitude, step_sigma)     =  0.564
        C(line_slope, step_sigma)         = -0.457
        C(line_intercept, step_center)    =  0.427
        C(line_slope, line_intercept)     = -0.309
        C(line_slope, step_center)        = -0.234
        C(line_intercept, step_sigma)     = -0.137
        C(line_intercept, step_amplitude) = -0.117
        C(step_amplitude, step_center)    =  0.109





|


.. code-block:: default

    ##
    import warnings
    warnings.filterwarnings("ignore")
    ##
    # <examples/doc_builtinmodels_stepmodel.py>
    import matplotlib.pyplot as plt
    import numpy as np

    from lmfit.models import LinearModel, StepModel

    x = np.linspace(0, 10, 201)
    y = np.ones_like(x)
    y[:48] = 0.0
    y[48:77] = np.arange(77-48)/(77.0-48)
    np.random.seed(0)
    y = 110.2 * (y + 9e-3*np.random.randn(x.size)) + 12.0 + 2.22*x

    step_mod = StepModel(form='erf', prefix='step_')
    line_mod = LinearModel(prefix='line_')

    pars = line_mod.make_params(intercept=y.min(), slope=0)
    pars += step_mod.guess(y, x=x, center=2.5)

    mod = step_mod + line_mod
    out = mod.fit(y, pars, x=x)

    print(out.fit_report())

    plt.plot(x, y, 'b')
    plt.plot(x, out.init_fit, 'k--', label='initial fit')
    plt.plot(x, out.best_fit, 'r-', label='best fit')
    plt.legend(loc='best')
    plt.show()
    # <end examples/doc_builtinmodels_stepmodel.py>


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

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


.. _sphx_glr_download_examples_documentation_builtinmodels_stepmodel.py:


.. only :: html

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



  .. container:: sphx-glr-download

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



  .. container:: sphx-glr-download

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


.. only:: html

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

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