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

.. only:: html

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

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

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

.. _sphx_glr_auto_examples_ksigmasmax.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-95



.. image-sg:: /auto_examples/images/sphx_glr_ksigmasmax_001.png
   :alt: fit with guess
   :srcset: /auto_examples/images/sphx_glr_ksigmasmax_001.png
   :class: sphx-glr-single-img


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

 Out:

 .. code-block:: none

    c:\users\johnf\notebook\pyspecdata\pyspecdata\core.py:2680: UserWarning: marker is redundantly defined by the 'marker' keyword argument and the fmt string "o" (-> marker='o'). The keyword argument will take precedence.
      retval = myplotfunc(*plotargs,**kwargs)
    c:\users\johnf\notebook\pyspecdata\pyspecdata\core.py:2680: UserWarning: linestyle is redundantly defined by the 'linestyle' keyword argument and the fmt string ":" (-> linestyle=':'). The keyword argument will take precedence.
      retval = myplotfunc(*plotargs,**kwargs)
    ----- Results for fitdata: -----
    output for fitdata: {'phalf': 1.096308584085903, 'ksC': 105.47247235550563}
    latex for fitdata: $f(p)=\frac{105.47 p}{p + 1.10}$






|

.. code-block:: default

    from pyspecdata import *
    import sympy as sp

    # {{{ 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
    from lmfit import Parameters, minimize
    from matplotlib.pyplot import figure, subplot, show, xlim, ylim, plot, gca
    from numpy import *  # I think it wasn't importing from numpy b/c it seems we're inside sphinx


    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


    # }}}
    fl = figlist_var()
    # {{{creating a fake data recovery curve
    p = nddata(r_[0:4:30j], "p")
    fake_data = 102 * p / (1.0 + p)
    fake_data.add_noise(5.0)
    # }}}
    # {{{ define the expression of the functional form once, and then use it
    #     for both types of classes
    # ksC is k_σ sₘₐₓ C
    ksC, phalf, p = sp.symbols("ksC phalf p", real=True)
    functional_form = ksC*p/(phalf+p)
    # }}}
    with figlist_var() as fl:
        fl.next("fit with guess")
        fl.plot(fake_data, "o", alpha=0.5, label="fake data")


        # {{{ lmfitdata method
        lmfitdata_instance = lmfitdata(fake_data)
        lmfitdata_instance.functional_form = functional_form
        lmfitdata_instance.set_guess(
            phalf=dict(value=0.5, min=0, max=5),
            ksC=dict(value=50, min=0, max=500),
        )
        # }}}
        fl.next("fit with guess")
        lmfitdata_instance.settoguess()
        guess_line = fl.plot(
            lmfitdata_instance.eval(100), ":", alpha=0.5, label="fitdata guess"
        )
        thiscolor = guess_line[0].get_color()
        lmfitdata_instance.fit()
        print("-" * 5, "Results for fitdata:", "-" * 5)
        print("output for fitdata:", lmfitdata_instance.output())
        print("latex for fitdata:", lmfitdata_instance.latex())
        this_ls = "-"
        fit_line = fl.plot(
            lmfitdata_instance.eval(100),
            ls=this_ls,
            color=thiscolor,
            alpha=0.5,
            label="fitdata fit",
        )
        ax = gca()
        x_text, y_text = 0.5,0.5 # where we want the text to go
        text(
            x_text,
            y_text,
            "fitdata RESULT: %s" % lmfitdata_instance.latex(),
            ha="center",
            va="center",
            color=thiscolor,
            transform=ax.transAxes,
        )
        text(
            x_text,
            y_text,
            (3 * "\n") + list_symbs(lmfitdata_instance),
            ha="center",
            va="top",
            size=10,
            color=thiscolor,
            transform=ax.transAxes,
        )


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

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


.. _sphx_glr_download_auto_examples_ksigmasmax.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: ksigmasmax.py <ksigmasmax.py>`



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

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


.. only:: html

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

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