我应该绘制强度与(cos(theta))** 2函数的关系图(对于Marlus Law) 但是我很固执地应该如何绘制模型函数
请告诉我以下内容是否正确:
def cos (x,a, b):
return a*(np.cos(x))**2+ b
#Load the data
ex1_data= np.loadtxt("1 data2.txt", skiprows=2)
angle1 = [row[0] for row in ex1_data]
intensity = [row[1] for row in ex1_data]
Intensity=np.array(intensity)
cos1=np.cos(angle1)
cos1_square=(cos1)**2
percision_error_I = Intensity * 0.0025
accuracy_error_I = 0.001
erry = []
for i in range(len(percision_error_I)):
erry.append(max(percision_error_I[i], accuracy_error_I))
#find optimized function and cov of x and y
p_opt , p_cov = curve_fit ( cos ,
cos1_square , Intensity , p0,
erry , True )
print(erry)
a_opt = p_opt[0]#find parameter for a
b_opt = p_opt[1]#find parameter for b
print("p_cov is ", p_cov)
print("a_opt is ", a_opt,"and b_opt is ", b_opt)
#plot model function
plt.plot(cos1_square, cos(cos1_square, a_opt,b_opt ), 'r-',label="cos1_square")
#plot error bar
plt.errorbar(cos1_square, Intensity,np.array(erry),
linestyle="",marker='+',label="error bar",lw=.7)
plt.legend()#show legend
#label current and voltage
plt.ylabel(' Intensity ')
plt.xlabel('cos1_square')
#provide a title for graph
plt.title("Voltage vs. cos1_square")
plt.show()
我收集了强度和角度的数据,但是我不知道如何绘制模型函数或误差线...请帮助