scipy.optimize.curve_fit 引发 RuntimeWarning

scipy.optimize.curve_fit raises RuntimeWarning

我正在尝试通过更改两个参数(eA)来拟合曲线。目标曲线是通过赋值n0=0.395绘制的,但其实际值为0.0395。所以我希望通过更改 eA 来实现相同的曲线。

import numpy as np
from scipy.optimize import curve_fit

def func(x,e,A):
    return A*(e+x)**0.0395 

strain = np.linspace(0,15,3000) # variable
e = 0.773
A = 386.5
n0 = 0.395
y = A*(e+strain)**n0 # target to minimize
popt, pcov = curve_fit(func, strain, y)

但是,我在 运行 代码后不断收到此警告:

RuntimeWarning: invalid value encountered in power
  return A*(e+x)**0.0395

我想知道为什么会这样,应该如何改进代码?

我找到了一个我不喜欢的解决方案,但它确实消除了警告。我发现,令我感到奇怪的是,"e" 在 curve_fit() 中变为负数。我在函数中添加了一个 "brick wall" 来阻止它,但它应该是不必要的。我的代码是:

import numpy as np
from scipy.optimize import curve_fit

def func(x,e,A):
    if e < 0.0: # curve_fit() hits a "brick wall" if e is negative
        return 1.0E10 # large value gives large error, the "brick wall"
    return A*(e+x)**0.0395 

strain = np.linspace(0,0.1,3) # variable
e = 0.773
A = 386.5
n0 = 0.395
y = A*(e+strain)**n0 # target to minimize
popt, pcov = curve_fit(func, strain, y)