如何使用 lmfit 修复此拟合程序(2 个自变量)?

我目前正在尝试用 实现曲线拟合例程lmfit,尽管我的编码技能有限,而且我以前的经验curve_fit也无济于事。另外,我一直在浏览https://lmfit.github.io/lmfit-py/model.html上的文档,但我仍然无法修复它。


正如您在下面看到的,我试图拟合以下方程:R2avg*(np.sin(thetas))**2 + ((np.sin(thetas))**2)*(phi_ex*k_ex/(k_ex**2 + omega_eff**2)),它有 2 个自变量(omega_eff和thetas),而我想拟合剩余的三个参数。


import lmfit as lf

from lmfit import Model, Parameters

import numpy as np

import matplotlib.pyplot as plt

from math import atan


def on_res(omega_eff, thetas, R2avg=5, k_ex=0.1, phi_ex=500):

    return R2avg*(np.sin(thetas))**2 + ((np.sin(thetas))**2)*(phi_ex*k_ex/(k_ex**2 + omega_eff**2))


model = Model(on_res,independent_vars=['omega_eff','thetas'])


model.set_param_hint('R2avg',value=5)

model.set_param_hint('k_ex',value=0.1)

model.set_param_hint('phi_ex',value=500)


carrier = 6146.53

O_1 = 5846

spin_locks = (1000, 2000, 3000, 4000, 5000) 

delta_omega = (O_1 - carrier)


omega_eff1 = ((delta_omega**2) + (spin_locks[0]**2))**0.5

omega_eff2 = ((delta_omega**2) + (spin_locks[1]**2))**0.5

omega_eff3 = ((delta_omega**2) + (spin_locks[2]**2))**0.5

omega_eff4 = ((delta_omega**2) + (spin_locks[3]**2))**0.5

omega_eff5 = ((delta_omega**2) + (spin_locks[4]**2))**0.5


theta_rad1 = atan(spin_locks[0]/delta_omega)

theta_rad2 = atan(spin_locks[1]/delta_omega)

theta_rad3 = atan(spin_locks[2]/delta_omega)

theta_rad4 = atan(spin_locks[3]/delta_omega)

theta_rad5 = atan(spin_locks[4]/delta_omega)


x = (omega_eff1/1000, omega_eff2/1000, omega_eff3/1000, omega_eff4/1000, omega_eff5/1000)# , omega_eff6/1000)# , omega_eff7/1000)

theta = (theta_rad1, theta_rad2, theta_rad3, theta_rad4, theta_rad5)

R1rho_vals = (7.9328, 6.2642, 6.0005, 5.9972, 6.1988)

e = (0.33, 0.31, 0.32, 0.33, 0.5)


如果我在发布时运行脚本,我会得到:


result = model.fit(R2avg, k_ex, phi_ex, thetas=thetas, omega_eff=omega_eff)

NameError: name 'R2avg' is not defined

我不太明白。我做了一些故障排除,并通过检查:


print(model.param_names)和print(model.independent_vars)


似乎一切都被适当地定义了。


非常欢迎任何帮助!


眼眸繁星
浏览 286回答 1
1回答

子衿沉夜

您lmfit.Model用于定义独立参数的用途看起来不错。您没有做的是定义一组要在拟合中使用的参数。你做:model = Model(on_res,independent_vars=['omega_eff','thetas'])model.set_param_hint('R2avg',value=5)model.set_param_hint('k_ex',value=0.1)model.set_param_hint('phi_ex',value=500)但set_param_hint告诉模型如何制作参数,但它不制作参数。你必须明确地这样做。在我看来,这样做会更好model = Model(on_res,independent_vars=['omega_eff','thetas'])params = model.make_params(R2avg=5, k_ex=0.1, phi_ex=500)部分原因是a)您需要一个Parameters对象才能使其适合工作,并且b)这些值实际上并不是模型的一部分(Parameter或约束表达式的边界可能是,但值很少是)。然后为了拟合独立的 ( y) 数据,你想做result = model.fit(data, params, thetas=thetas, omega_eff=omega_eff)或者(如果您真的坚持不创建参数)您可以显式声明每个参数的起始值:result = model.fit(data, R2avg=5, k_ex=0.1, phi_ex=500,                   thetas=thetas, omega_eff=omega_eff)但不是result = model.fit(param1, param2, ..., thetas=thetas, omega_eff=omega_eff) # NO!通常,首选显式使用参数对象。看起来(但我不确定)这R1rho_vals是要拟合的数据,所以这意味着你想要这样做:result = model.fit(R1rho_vals, params, thetas=thetas, omega_eff=omega_eff)要包括不确定性(您的e),您可以这样做:result = model.fit(R1rho_vals, params, weights=1.0/e,                    thetas=thetas, omega_eff=omega_eff)然后您可以打印和绘制结果:print(result.fit_report())plt.errorbar(x, R1rho_vals, yerr = e, fmt = ".k", markersize = 8, capsize = 3)plt.plot(new_x, result.best_fit, label="Two sites fast exchange")  plt.show()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python