我有一组点。基本上,我有 P = f(t)。
比方说,我有 50 次测量。 P 的 50 个值,时间的函数。这些值(value)观遵循既定法则。
我要做的就是在规律中找到参数的值,仅此而已。基本上,我必须用最好的曲线来拟合这些点。这是法律:
P = V.t - ((V - W)(1 - exp(-k.t)) / k)
我需要做的是找到 V、W 和 k 的数值。我有 t 和 P。你知道怎么做吗?
编辑:
下面是我要获取的截图:
上图:
- V 是 Vs
- W 是 Vi
- k是k
这就是我在 reptilicus 的帮助下获得的:
http://i.imgur.com/f59Eo29.png
import numpy as np
from scipy.optimize import curve_fit
from matplotlib.pyplot import *
import xlrd
def myFunc(t, V, W, k):
y = V * t - ((V - W) * (1 - np.exp(-k * t)) / k)
return y
classeur = xlrd.open_workbook(path)
names_sheets = classeur.sheet_names()
sheet = classeur.sheet_by_name(names_sheets[0])
row_start = 2
time = sheet.col_values(0, row_start)
fluo = feuille.col_values(4, row_start)
time = [ index for index in time if index ]
fluo = [ index for index in fluo if index ]
# this generates some fake data to fit. For youm just read in the
# data in CSV or whatever you've
x = np.array(time)
y = np.array(fluo)
#fit the data, return the best fit parameters and the covariance matrix
#popt, pcov = curve_fit(myFunc, x, yn)
popt, pcov = curve_fit(myFunc, x, y)
print(popt)
print(pcov)
#plot the data
clf() #matplotlib
plot(x, y, "rs")
#overplot the best fit curve
plot(x, myFunc(x, popt[0], popt[1], popt[2]))
grid(True)
show()
还不错。我设法提取了我的 excel 工作簿的数据,并绘制了它。但如您所见,我得到了线性回归,这是我不想要的。我的目标是重现他们与 Origin 8 的契合度。
编辑:
我有一些消息。我团队中最后一个这样做的人告诉我他是如何处理 Origin 的。事实上,他们也使用最小二乘法,但他们找到了 chi 2 最小化的参数。该软件会进行一些迭代,并优化参数。
编辑 2:
因为我花了很长时间才弄明白,所以我在这里分享我的研究结果。我面临的主要问题是我的值(value)观“太小”了。实际上,我的 y 值大约为 10^-7。如此处所述 Fitting curve: why small numbers are better? , 1 量级的数字更适合拟合。
此外,至少就我而言,因为我的数据是按此顺序排列的,所以我不需要提供一些初始参数(默认情况下,它们设置为 1)。所以我只是“规范化”了我的值(value)观。例如,我将时间值从秒转换为小时,并将我的 y 值乘以 10^7,其数量级为 10^-7。 然后,我将获得的参数转换回所需的单位。这是我的代码:
import numpy as np
from scipy.optimize import curve_fit, leastsq
from matplotlib.pyplot import *
def myFunc(t, Vs, Vi, k):
y = Vs * t - ((Vs - Vi) * (1 - np.exp(-k * t)) / k)
return y
raw_x = some_input
raw_y = some_input
# scaling data
time = [ index /3600 for index in raw_x if index or index==0 ]
fluo = [ index*10**7 for index in raw_y if index or index==0 ]
x = np.array(temps)
y = np.array(fluo)
popt, pcov = curve_fit(myFunc, x, y, maxfev=3000)
# Good unities
popt2 = list()
popt2 = [ popt[0] / 3600 * 10**-7, popt[1] / 3600 * 10**-7, popt[2] / 3600 ]
#plot the data
clf() #matplotlib
plot(raw_x, raw_y, "rp")
plot(raw_x, myFunc(raw_x, popt2[0], popt2[1], popt2[2]), 'b')
grid(True)
show()
这是一张说明区别的图片:
http://i.imgur.com/YXkJG5j.png
蓝色图是使用通过单位重新缩放(并转换回良好单位)获得的参数的拟合曲线。绿色的是在原单位拟合得到的曲线。
感谢大家的帮助。
最佳答案
只需在 scipy.optimize
中使用 curve_fit
:
import numpy as np
from scipy.optimize import curve_fit
from pylab import *
def myFunc(t, V, W, k):
y = V * t - ((V - W) * (1 - np.exp(-k * t)) / k)
return y
# this generates some fake data to fit. For youm just read in the
# data in CSV or whatever you've
x = np.linspace(0,4,50)
y = myFunc(x, 2.5, 1.3, 0.5)
# add some noise to the fake data to make it more realistic. . .
yn = y + 0.2*np.random.normal(size=len(x))
#fit the data, return the best fit parameters and the covariance matrix
popt, pcov = curve_fit(myFunc, x, yn)
print popt
print pcov
#plot the data
clf()
plot(x, yn, "rs")
#overplot the best fit curve
plot(x, myFunc(x, popt[0], popt[1], popt[2]))
grid(True)
show()
这给出了如下图所示的内容。红点是(嘈杂的)数据,蓝线是最佳拟合曲线,具有针对该特定数据的最佳拟合参数:
[ 2.32751132, 1.27686053, 0.65986596]
这非常接近 2.5、1.3、0.5 的预期参数。不同之处在于我添加到假数据中的噪音。
关于python - 用一组点求解方程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18619131/