scipy optimize.curve_fit未执行线性拟合

byqmnocz  于 2023-08-05  发布在  其他
关注(0)|答案(1)|浏览(88)

我一直在尝试使用curve_fit来用我提供的线性拟合函数拟合我的数据。下面是一个简化版本:

from scipy.optimize import curve_fit
import numpy as np

def line(x,m,c):
    return x*m + c

x = [10.55977951, 11.45686089, 12.35423473, 13.30408861, 14.3528772,  15.45035217,
     16.64817782, 17.99661252, 19.49184541, 21.18832373, 23.08427703, 25.27645953,
     27.91555605, 31.14343828, 35.16744841, 40.31943045, 47.22644384, 57.23413423, 73.51606363]

y = [ 4.50873148e-04, 2.29554869e-04, 4.62602769e-04, 1.57525181e-04,
     1.16334543e-04, 9.42105291e-05, 2.86606379e-04, 2.40194287e-04,
     4.74193673e-04, 6.83848270e-04, 6.73286482e-04, 2.03506304e-04,
     3.58126867e-04, 1.88155439e-04, 5.14133854e-04, 2.39990293e-04,
     -3.60391884e-05, -1.17866329e-04, 2.68954649e-05]

y_err = [8.23397676e-05, 7.54222285e-05, 7.05355053e-05, 6.30493368e-05,
         5.73241555e-05, 5.56298862e-05, 5.00181328e-05, 4.76554758e-05,
         4.45081313e-05, 4.23716792e-05, 4.10516842e-05, 3.87066834e-05,
         3.67639901e-05, 3.51162489e-05, 3.39993704e-05, 3.29275562e-05,
         3.24743967e-05, 3.15296789e-05, 3.09144126e-05]

x = np.array(x)
y = np.array(y)
y_err = np.array(y_err)

popt, cov = curve_fit(line, x, y, sigma=y_err)
m = popt[0]
c = popt[1]
print(m,c, cov)

line1 = m*np.array(x) + c

import matplotlib.pyplot as plt
plt.plot(x, line1, color="red", )
plt.plot(x, [0] * len(x), color="black")
plt.errorbar(x , y,  label="g1", fmt="s", markersize=5, color="red")
        
plt.xscale("log")
plt.xlabel("dummy x ")
plt.ylabel("dummy y")
plt.legend()
plt.tight_layout()
plt.show()

字符串
基本上,配件仅正确地工作(即,进行线拟合),并且在其它情况下,如在上面的代码块中,将给予一些多项式拟合。我已经尝试输入p0的不同初始猜测值,但它实际上并没有解决这个问题--只是稍微改变了参数。理想情况下,我不希望给予p0,因为我想传递一堆不同的数据数组。我很乐意解决这个问题!我也愿意接受提供协方差的替代拟合建议。谢谢大家

oalqel3c

oalqel3c1#

“一些多项式拟合”也许你认为这是一个2阶或更高阶的多项式,因为你的输出是弯曲的,但这实际上只是因为你有对数尺度。从输出中删除对数刻度。加上雅可比矩阵因为它很简单。在你的errorbar里你没有传递错误。为什么?为什么?

from scipy.optimize import curve_fit
import numpy as np
import matplotlib.pyplot as plt

def line(x: np.ndarray, m: float, c: float) -> np.ndarray:
    return x*m + c

def jacobian(x: np.ndarray, m: float, c: float) -> np.ndarray:
    return np.stack((
        x,                # dy/dm
        np.ones_like(x),  # dy/dc
    ), axis=1)

x = np.array([
    10.55977951, 11.45686089, 12.35423473, 13.30408861, 14.3528772, 15.45035217,
    16.64817782, 17.99661252, 19.49184541, 21.18832373, 23.08427703, 25.27645953,
    27.91555605, 31.14343828, 35.16744841, 40.31943045, 47.22644384, 57.23413423,
    73.51606363,
])

y = np.array([
    4.50873148e-04, 2.29554869e-04, 4.62602769e-04, 1.57525181e-04,
    1.16334543e-04, 9.42105291e-05, 2.86606379e-04, 2.40194287e-04,
    4.74193673e-04, 6.83848270e-04, 6.73286482e-04, 2.03506304e-04,
    3.58126867e-04, 1.88155439e-04, 5.14133854e-04, 2.39990293e-04,
    -3.60391884e-05, -1.17866329e-04, 2.68954649e-05,
])

y_err = np.array([
    8.23397676e-05, 7.54222285e-05, 7.05355053e-05, 6.30493368e-05,
    5.73241555e-05, 5.56298862e-05, 5.00181328e-05, 4.76554758e-05,
    4.45081313e-05, 4.23716792e-05, 4.10516842e-05, 3.87066834e-05,
    3.67639901e-05, 3.51162489e-05, 3.39993704e-05, 3.29275562e-05,
    3.24743967e-05, 3.15296789e-05, 3.09144126e-05,
])

(m, c), cov = curve_fit(f=line, jac=jacobian, xdata=x, ydata=y, sigma=y_err)

fig, ax = plt.subplots()
ax.plot(x, line(x, m, c), label='fit')
ax.errorbar(x, y, y_err, fmt='s', label='g1')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.legend()

plt.show()

字符串


的数据

相关问题