如何解决绘图变量维度不匹配的错误?

How do I solve the error in plot variable dimension mismatch?

我正在尝试通过公式 2 拟合使用公式 1 生成的数据。前者有3个参数,而后者有5个拟合参数。但现在由于形状不匹配,我在绘制拟合曲线时出错。

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

def func(x, a, b, c, d, e):
    return (((a/e) * (2*x)**b) + (d * (2*x)**c))

y = []
x = []
A = 6.7
B = 2.0
C = 0.115
for N in np.logspace(1, 9., 100, base = 10.):
    x.append(int(N))
    y.append(np.exp((A-np.log(int(N)))/B)+C)
plt.loglog(x, y, 'b:*', label='data')

popt, pcov = curve_fit(func, x, y)
print(popt)
plt.loglog(x, func(x, *popt))

我想看看拟合曲线,但是最后一行有尺寸错误'''plt.loglog(x, func(x, *popt))'''

一种方法是创建一个列表 y_model,在其中添加对应于每个 x 的元素 y。

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

def func(x, a, b, c, d, e):
    return (((a/e) * (2*x)**b) + (d * (2*x)**c))

y = []
x = []
A = 6.7
B = 2.0
C = 0.115
for N in np.logspace(1, 9., 100, base = 10.):
    x.append(int(N))
    y.append(np.exp((A-np.log(int(N)))/B)+C)

popt, pcov = curve_fit(func, x, y)

y_model = []
for e in x:
    y_model.append(func(e, *popt))

plt.loglog(x, y, 'b:*', label='data')
plt.loglog(x, y_model)

结果: