用一组点求解方程

JPF*_*oia 2 python math numpy

我有一组积分。基本上,我有 P = f(t)。

比方说,我有 50 次测量。50 个 P 值,时间函数。这些价值观遵循既定的规律。

我要做的就是找到规律中参数的值,仅此而已。基本上,我必须用最佳曲线拟合点。这是法律:

P = V.t - ((V - W)(1 - exp(-k.t)) / k)
Run Code Online (Sandbox Code Playgroud)

我需要做的是找到 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()
Run Code Online (Sandbox Code Playgroud)

不错。我设法提取了我的 excel 工作簿的数据,并绘制了它。但正如你所看到的,我得到了一个线性回归,这是我不想要的。我的目标是不重现他们使用 Origin 8 获得的适合度。

编辑:

我有一些消息。我团队中最后一个这样做的人告诉我他是如何使用 Origin 的。事实上,他们也使用最小二乘法,但他们找到了 chi 2 最小化的参数。软件会进行一些迭代,并优化参数。

编辑2:

因为我花了很长时间才弄明白,我在这里分享我的研究结果。我面临的主要问题是我的价值观“太小”。实际上,我的 y 值大约为 10^-7。如此处所述拟合曲线:为什么小数字更好?,1 阶的数字更适合拟合。

此外,至少在我的情况下,因为我的数据是这个顺序,所以我不需要给出一些初始参数(默认情况下,它们设置为 1)。所以我只是“标准化”了我的价值观。例如,我将时间值从秒转换为小时,并将我的 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()
Run Code Online (Sandbox Code Playgroud)

这是一张说明差异的图片:

http://i.imgur.com/YXkJG5j.png

蓝色图是使用单位重新缩放(并转换回良好单位)获得的参数的拟合曲线。绿色的是通过拟合原始单位获得的曲线。

感谢大家的帮助。

rep*_*cus 6

只需curve_fit在scipy.optimize:

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()
Run Code Online (Sandbox Code Playgroud)

这给出了类似于下面的情节。红点是(嘈杂的)数据,蓝线是最佳拟合曲线,该特定数据的最佳拟合参数为:

[ 2.32751132, 1.27686053, 0.65986596]
Run Code Online (Sandbox Code Playgroud)

这非常接近预期参数 2.5、1.3、0.5。差异是由于我添加到假数据中的噪声。

适合的例子