我必须绘制一个图表,对以下公式进行曲线拟合:
2*np.pi*np.sqrt((lx/g)*1+1/16*a**2)
问题是它给了我一个矩阵,因此如果我正确解释以下错误,绘图将不再起作用:
ValueError:操作数无法与形状一起广播 (100,) (2,)
老实说,我真的已经解决了这个问题,因为它是在星期二到期的。
这也是我到目前为止使用的代码:
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
import numpy as np
##defining function of mathematical pendulum
my_data =genfromtxt('lundd_3.csv',delimiter=',',skip_header=1)
##defining my variables
lx=my_data[:,0].reshape(1,-1).flatten()
ty=my_data[:,1]
Xerr=my_data[:,2]
Yerr=my_data[:,3]
a=my_data[:,4]
def mathPen(lx,a,g):
##here must be the error because it somehow creates here the matrix but I don't know why??
return 2*np.pi*np.sqrt((lx/g)*1+1/16*a**2)
Fit=curve_fit(mathPen, lx, ty)
fitParameter = Fit[0]
fehlerMatrix = Fit[1]
##defining variable again
g = fitParameter
##creating new values
xFunk = np.linspace(np.min(lx),np.max(lx),100)
print(xFunk)
yFunk = mathPen(xFunk,a,g)
plt.plot(xFunk,yFunk)
plt.plot(lx,ty,markers='x',linestyle = 'none',color='blue')
plt.show()
现在跟随错误代码,它告诉我错误必须在函数中。 ** 那么有没有办法重写函数,这样我就得不到矩阵,或者我能以某种方式重塑这些值以便绘制它们吗? **