【发布时间】:2019-03-14 02:59:24
【问题描述】:
我需要输入 chi_square 函数,但运行时总是显示语法无效,想知道我应该如何编写脚本?以及如何输入“v”?
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
data = np.loadtxt("214 ohm.txt", skiprows=1)
xdata = [row[0] for row in data ]#x represents current unit is "V"
ydata = [row[1] for row in data]#y represents voltage unit is "mA"
percision_error_V = np.array(xdata) * 0.0025 #we are using last digit of reading and multiply by measured voltage
accuracy_error_V = 0.01#we are using DC Vlotage, so use the error it provided online
erry = []
for i in range(len(percision_error_V)):
#to compare percision_error and accuracy_error for Voltage and use the larger one
erry.append(max(percision_error_V[i], accuracy_error_V))
def model_function (x, a, b):
return a*x + b
p0 = [0 , 0.]#214ohm is measured by ohmeter
p_opt , p_cov = curve_fit ( model_function ,
xdata , ydata , p0,
erry , True )
print(erry)
a_opt = p_opt[0]
b_opt = p_opt[1]
print(p_cov)
print("diagonal of P-cov is",np.diag(p_cov))
print("a_opt, b_opt is ",a_opt, b_opt)
xhat = np.arange(0, 16, 0.1)
plt.plot(xhat, model_function(xhat, a_opt, b_opt), 'r-', label="model function")
plt.errorbar(xdata, ydata,np.array(erry),linestyle="",marker='s', label="error bar")
plt.legend()
plt.ylabel('Current (mA)')
plt.xlabel('Voltage(V)')
plt.title("Voltage vs. Current with 220ohm Resistor")
plt.show()
p_sigma = np.sqrt(np.diag(p_cov))
print("p_sigma is" ,p_sigma)
for i in range(len(xdata)):
sum=sum((ydata[i]-model_function(xdata[i], a_opt, b_opt))
chi.append(sum)
this is the required function I'm supposed to put on python
谢谢
我的代码在卡方方程之前都没有问题,我想知道我应该如何解决它?
【问题讨论】:
-
1.append(sum) 中的 1 是从哪里来的?发布完整代码。
-
你的代码也没有缩进,这在python中是无效的语法
-
不要将变量命名为与内置函数相同的名称,在这种情况下为
sum -
chi_square=sum((ydata[i]-model_function(xdata[i], a_opt, b_opt)-p_sigma)/v您在这一行中缺少一个)。然后,您使用数字1作为变量并尝试附加到它。那也错了 -
我刚从1改成chi,程序还是没有运行
标签: python python-3.x python-2.7 python-requests