【问题标题】:How to get a log function fit using Scipy curve_fit for the data如何使用 Scipy curve_fit 对数据进行对数函数拟合
【发布时间】:2021-03-11 19:31:14
【问题描述】:

我正在尝试让 a*log(b/x)^c 类型适合以下数据(简化为 10 个数据点)

我尝试了其他一些问题like this one using both curve_fit and lmfit 中描述的方法,但解决方案永远不会收敛。我的猜测是我的初始条件很糟糕。我能够将其他指数函数注释掉,但应用程序需要给定形式的对数拟合。附上适合的数据以供参考。

import numpy as np
from scipy.optimize import curve_fit


x=[0, 0.89790454, 1.79580908, 2.69371362, 3.59161816, 4.48952269, 5.38742723, 6.28533177, 7.18323631, 8.08114085]
y=[0.39599324, 0.10255828, 0.07094521, 0.05500624, 0.04636146, 0.04585985,  0.0398909,  0.03340628, 0.03041699, 0.02498938]
x = np.array(x,dtype=float) 
y = np.array(y,dtype=float)


def func(x, a, b, c):
    #return a*np.exp(-c*(x*b))+d

    return a*(np.log(b/x)**c)

popt, pcov = curve_fit(func, x, y, p0=[.5,.5,1],maxfev=10000)

print(popt)

a,b ,c = np.asarray(popt)

【问题讨论】:

  • a*(np.log(b/x)**c)x=0 意味着什么?
  • 嗯,好点。 a*(np.log(b-x)**c) 那么呢?老实说,我无法让任何日志适合收敛

标签: python scipy curve-fitting


【解决方案1】:

将你的函数替换为,

def func(x, a, b, c):
    #return a*np.exp(-c*(x*b))+d
    t1 = np.log(b/x)
    t2 = a*t1**c
    print(a,b,c,t1, t2)
    return t;

你会很快看到t1 = np.log(b / x) 可能是负数(只要 b nan 结果。

【讨论】:

    【解决方案2】:

    我的试衣软件没有问题(结果如下)。

    使用迭代回归方法进行非线性拟合的困难原因通常是设置参数的初始值以启动迭代过程。

    【讨论】:

    • 谢谢,这可能是一个非常无知的问题,但是就自然对数而言,它的等效函数是什么?我显然不是数学家,总是混淆这些。我想要一个基于自然对数的拟合的原因是因为这就是理论推导所说的,而我拥有的数据是实验性的。
    • 如果你不介意我问,那是哪个软件?
    • 用符号 ln=自然对数和 Log=以 10 为底的对数关系是 ln(y)=ln(10)*Log(y)。因此,上述拟合方程变为 ln(y)=ln(a)-b * x ** c 。或者,如果您更喜欢 Log(y)=(ln(a)-b * x ** c)/ln(10) = Log(a) - b * x ** c/ln(10)。
    • 或 Log(y) = Log(a) - Log(e) * b * x ** c
    • 我的软件是自制的,基于非常规方法:fr.scribd.com/doc/14674814/Regressions-et-equations-integrales.
    猜你喜欢
    • 2020-05-05
    • 2020-06-08
    • 2016-01-16
    • 2016-09-16
    • 2017-04-11
    • 2020-06-12
    • 2021-11-05
    • 2012-11-02
    • 2012-06-07
    相关资源
    最近更新 更多