Model - loadmodelΒΆ

model loadmodel
[[Model]]
    Model(mysine)
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 25
    # data points      = 101
    # variables        = 3
    chi-square         = 7.68903767
    reduced chi-square = 0.07845957
    Akaike info crit   = -254.107813
    Bayesian info crit = -246.262451
    R-squared          = 0.97252133
[[Variables]]
    amp:    2.32733694 +/- 0.03950824 (1.70%) (init = 3)
    freq:   0.50098739 +/- 5.7726e-04 (0.12%) (init = 0.52)
    shift:  0.53605324 +/- 0.03383110 (6.31%) (init = 0)
[[Correlations]] (unreported correlations are < 0.100)
    C(freq, shift) = -0.8663

# <examples/doc_model_loadmodel.py>
import os
import sys

import matplotlib.pyplot as plt
import numpy as np

from lmfit.model import load_model

if not os.path.exists('sinemodel.sav'):
    os.system(f"{sys.executable} doc_model_savemodel.py")


def mysine(x, amp, freq, shift):
    return amp * np.sin(x*freq + shift)


data = np.loadtxt('sinedata.dat')
x = data[:, 0]
y = data[:, 1]

model = load_model('sinemodel.sav', funcdefs={'mysine': mysine})
params = model.make_params(amp=dict(value=3, min=0),
                           freq=0.52,
                           shift=dict(value=0, min=-1, max=1))

result = model.fit(y, params, x=x)
print(result.fit_report())

plt.plot(x, y, 'o')
plt.plot(x, result.best_fit, '-')
plt.show()
# <end examples/doc_model_loadmodel.py>

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

Gallery generated by Sphinx-Gallery