[英]Fit data with a function that equals 0 and could not be converted to the form f(x) = x
I have 2 columns and 31 rows in a pandas dataframe. I want to plot this x,y data and fit them to a complex function with 4 parameters.我在 pandas dataframe 中有 2 列和 31 行。我想要 plot 这个 x,y 数据并将它们拟合到具有 4 个参数的复杂 function。 The function looks something like this. function 看起来像这样。 The function has to be 0 function 必须为 0
# Data:
T,p = df["T"], df["p"] #31 rows
# known constants: a,b,Ta,c0,x
def c(T,v,VP,a=...,b=...,Ta=...,c0=...):
c = c0 + a*(T-Ta) + b*t_r(T)**v/(t_r(T)**v-K(T,VP))
return c
# t_r and K are other functions
def function(p,T,p0,v,N,VP,a,b,c0,x):
return np.log(1-p) + p + (x*c(T,v,VP))*p**2 + (p0/N)*(R-0.5*R**3) # =0
I am interested in fitting the parameters N,p0,Vp我有兴趣拟合参数 N,p0,Vp
I tried to use Lmfit and changed my function to -> function(params,T,p)我尝试使用 Lmfit 并将我的 function 更改为 -> function(params,T,p)
from Lmfit import minimize, Parameters
## add Parameters
params = Parameters() ##Class with a list of parameters
# add all constant Parameters with vary = False
params.add("a", value=...,vary=False)
...
## add variables to fit with vary = True, limits with min,max
params.add("N",value=..., vary=True,min=0,max=...)
...
output = minimize(function,params) #Fit Results
output.params.pretty_print() #Show Results
Now I acquired the parameters, but I want to check if this makes sense by plot(T,p) for a more continous array, like:现在我获得了参数,但我想通过 plot(T,p) 来检查这对于更连续的数组是否有意义,例如:
Ts = np.linspace(10,60,1000) # x-array
ps = ... ? # y-array
plt.plot(Ts,ps,label="Fit") # Plot Data
How could I obtain a function to calculate p for T on each point to plot it?我怎样才能得到一个 function 来计算 p for T 的每个点到 plot 呢?
I find an answer myself.我自己找到答案。
First I wrap my function in a way that the y-value p is the first argument.首先,我将 function 包装成 y 值 p 是第一个参数。 And I used lmfit parameter class as arguments. Lmfit Parameters are basically dictionaries.而我用的lmfit参数class为arguments。lmfit参数基本都是字典。
p_solvable = lambda p,T,parameter : function(p,T,parameter["p0"],parameter["v"],...)
Then I solve the equation by scipy.optimize.root_scalar with the brentq method.然后我用 scipy.optimize.root_scalar 用 brentq 方法求解方程。 The brentq method needs Brackets where the signs are changing. brentq 方法需要符号变化的括号。 I chose 1 as lower limit so np.log(1-p > 0) is defined and just the doubled maximum as upper limit.我选择 1 作为下限,因此 np.log(1-p > 0) 被定义并且只是最大值的两倍作为上限。
p_max = np.max(p)
def p(T):
P_Init = [root_scalar(p_solvable,args=(T,parameter), method="brentq", bracket=[1,p_max*2]).root for T in T]
return P_Init
Now I have a function where I can input f(x) = y and fit it with lmfit or scipy curve_fit现在我有一个 function,我可以在其中输入 f(x) = y 并使用 lmfit 或 scipy curve_fit 对其进行拟合
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.