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

.. only:: html

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

        :ref:`Go to the end <sphx_glr_download_auto_examples_fitting_fit_fake_data.py>`
        to download the full example code

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

.. _sphx_glr_auto_examples_fitting_fit_fake_data.py:

Fitting Data (Nonlinear + Symbolic)
======================================

This example creates fake data with noise
then fits the exponential with the fitdata
function.

.. GENERATED FROM PYTHON SOURCE LINES 7-107



.. image-sg:: /auto_examples/fitting/images/sphx_glr_fit_fake_data_001.png
   :alt: fit with guess
   :srcset: /auto_examples/fitting/images/sphx_glr_fit_fake_data_001.png, /auto_examples/fitting/images/sphx_glr_fit_fake_data_001_2_00x.png 2.00x
   :class: sphx-glr-single-img


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

 .. code-block:: none

    {\bf Warning:} You have no error associated with your plot, and I want to flag this for now


    ----- Results for fitdata: -----
    output for fitdata: {'M_0': -102.87857607503906, 'M_inf': 102.23079377453595, 'R_1': 6.036261113706332}
    latex for fitdata: $f(\tau)=102.23 + \left(-1.03\times 10^{2} - 102.23\right) e^{- 6.04 \tau}$
    $T_1$ for fitdata, 0.1656654642936725
    ----- Results for lmfitdata: -----
    output for lmfitdata: {'M_inf': 102.23079359781903, 'M_0': -102.8785775851992, 'R_1': 6.0362612172913375}
    latex for lmfitdata: $f(\tau)=(102.23) + \left((-1.03\times 10^{2}) - (102.23)\right) e^{- (6.04) \tau}$
    $T_1$ for lmfitdata, 0.1656654614507773
    1: fit with guess |||None







|

.. code-block:: Python


    import pylab as pl
    import pyspecdata as psd
    import sympy as sp
    from numpy import r_,exp

    # {{{ this is the contents of pylab.py -- works
    # need to go through and figure out which lines
    # are actually needed and which are not
    # -- I have already stripped out some


    def list_symbs(f):
        # {{{ this is just to show all the parameters
        list_symbs = []
        for j, k in f.output().items():
            s_repr = sp.latex(sp.Symbol(j))
            list_symbs.append(f"${s_repr} = {k:0.5g}$")
        list_symbs = "\n".join(list_symbs)
        # }}}
        return list_symbs


    # }}}
    # {{{creating a fake data recovery curve
    tau = psd.nddata(r_[0:2:256j], "tau")
    fake_data = 102 * (1 - 2 * exp(-tau * 6.0))
    fake_data.add_noise(5.0)
    # }}}
    # {{{ define the expression of the functional form once, and then use it
    #     for both types of classes
    M0, Mi, R1, vd = sp.symbols("M_0 M_inf R_1 tau", real=True)
    functional_form = Mi + (M0 - Mi) * sp.exp(-vd * R1)
    # }}}
    with psd.figlist_var() as fl:
        fl.next("fit with guess")
        fl.plot(fake_data, "o", alpha=0.5, label="fake data")

        def show_guess_and_fit(fitinst, thislabel, x_text, y_text):
            """show the guess and the fit -- group as a function so we're sure we
            do this consistently"""
            fl.next("fit with guess")
            fitinst.settoguess()
            guess_line = fl.plot(
                fitinst.eval(100), ":", alpha=0.5, label=f"{thislabel} guess"
            )
            thiscolor = guess_line[0].get_color()
            fitinst.fit()
            print("-" * 5, f"Results for {thislabel}:", "-" * 5)
            print(f"output for {thislabel}:", fitinst.output())
            print(f"latex for {thislabel}:", fitinst.latex())
            T1 = 1.0 / fitinst.output("R_1")
            print(f"$T_1$ for {thislabel}, {T1}")
            this_ls = "-"
            if thislabel == "fitdata":
                this_ls = "--"
            fl.plot(
                fitinst.eval(100),
                ls=this_ls,
                color=thiscolor,
                alpha=0.5,
                label=f"{thislabel} fit",
            )
            ax = pl.gca()
            pl.text(
                x_text,
                y_text,
                f"{thislabel} RESULT: %s" % fitinst.latex(),
                ha="center",
                va="center",
                color=thiscolor,
                transform=ax.transAxes,
            )
            pl.text(
                x_text,
                y_text,
                (3 * "\n") + list_symbs(fitinst),
                ha="center",
                va="top",
                size=10,
                color=thiscolor,
                transform=ax.transAxes,
            )

        # {{{ use fitdata
        fitdata_instance = psd.fitdata(fake_data)
        fitdata_instance.functional_form = functional_form
        fitdata_instance.set_guess({M0: -500, Mi: 500, R1: 2})
        show_guess_and_fit(fitdata_instance, "fitdata", 0.6, 0.5)
        # }}}
        # {{{ lmfitdata method
        lmfitdata_instance = psd.lmfitdata(fake_data)
        lmfitdata_instance.functional_form = functional_form
        lmfitdata_instance.set_guess(
            M_0=dict(value=-400, max=0, min=-500),
            M_inf=dict(value=400, max=500, min=0),
            R_1=dict(value=1, max=10, min=0),
        )
        show_guess_and_fit(lmfitdata_instance, "lmfitdata", 0.6, 0.25)
        # }}}


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

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


.. _sphx_glr_download_auto_examples_fitting_fit_fake_data.py:

.. only:: html

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

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

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

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

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


.. only:: html

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

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