将积分拟合到数据中

时间:2017-10-12 16:30:38

标签: scipy integral

我正在尝试将数据拟合到函数f(x),该函数是T的整数.x是积分的上边界。我试图用scipy.curve_fit()来做,但我不知道如何将我的积分写为可以传递给curve_fit的函数。 我看过类似的问题,但我没有看到任何符合我问题的东西。

我无法为A和Ea提供任何猜测值,因为我完全不知道它们到底是什么。

from scipy import optimize
import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate
from scipy import integrate

class get_Ton:
 def __init__(self):
  self.data=np.genfromtxt('test3.csv', delimiter=',', skip_header=8)
 def loop(self):
    def fit_tangent():
      tck = interpolate.splrep(self.x, self.y, k=2, s=0)
      dev_1 = interpolate.splev(self.x, tck, der=1)

      def integrand(T, A, Ea):
        return A*np.exp(-Ea/(8.314*T))
      def polyn(x, A, Ea):
        return integrate.quad(integrand, 25, x, args=(A, Ea))[0]

      vcurve = np.vectorize(polyn)
      p, e = optimize.curve_fit(vcurve, self.x, self.y, [2000, 150])
      xd = np.linspace(50, 70, 100)

      plt.plot(self.x, self.y, "o")
      plt.plot(xd, vcurve(xd, *p))

    for i in range((list(np.shape(self.data)[1:2]))[0]):
        if i % 2 == 0:
         self.temp=self.data[:,i]
         self.scat=self.data[:,i+1]
         self.x=[26.192, 26.861000000000001, 27.510000000000002, 28.178000000000001, 28.856000000000002, 29.515000000000001, 30.183, 30.823, 31.5, 32.158999999999999, 32.856000000000002, 33.515000000000001, 34.145000000000003, 34.823, 35.491, 36.168999999999997, 36.837000000000003, 37.533999999999999, 38.164000000000001, 38.832000000000001, 39.481000000000002, 40.158999999999999, 40.826999999999998, 41.496000000000002, 42.164000000000001, 42.832000000000001, 43.500999999999998, 44.188000000000002, 44.837000000000003, 45.505000000000003, 46.173000000000002, 46.832000000000001, 47.500999999999998, 48.188000000000002, 48.828000000000003, 49.496000000000002, 50.173999999999999, 50.813000000000002, 51.481000000000002, 52.112000000000002, 52.808999999999997, 53.439, 54.116, 54.765999999999998, 55.453000000000003, 56.101999999999997, 56.761000000000003, 57.429000000000002, 58.078000000000003, 58.737000000000002, 59.442999999999998, 60.082999999999998, 60.770000000000003, 61.448, 62.125999999999998, 62.756, 63.414999999999999, 64.082999999999998, 64.742000000000004, 65.420000000000002, 66.087999999999994, 66.747, 67.415000000000006]
         self.y=[1553.5, 1595.0, 1497.8, 1695.5999999999999, 1328.7, 1279.0, 1547.8, 1412.8, 1037.0, 1473.5, 1447.4000000000001, 1532.5999999999999, 1484.2, 1169.5, 1395.2, 1183.5999999999999, 949.01999999999998, 1238.0999999999999, 1225.5999999999999, 924.80999999999995, 1650.5999999999999, 803.96000000000004, 1245.7, 1190.0, 1207.0, 1294.0, 1174.9000000000001, 1229.8, 1260.0, 1129.2, 1142.9000000000001, 987.63999999999999, 1389.5999999999999, 1366.0, 1102.0999999999999, 1325.5, 1258.9000000000001, 1285.7, 1217.5, 871.47000000000003, 820.24000000000001, 1388.7, 1391.0, 1400.3, 2482.5999999999999, 3360.5999999999999, 7013.5, 11560.0, 16525.0, 22538.0, 32556.0, 43878.0, 59093.0, 67977.0, 75949.0, 82316.0, 90213.0, 90294.0, 99928.0, 128240.0, 181280.0, 226380.0, 223260.0]
         fit_tangent()
         plt.ylim((-100,1000000))
         plt.show()

def main():
    this=get_Ton()
    this.loop()

if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:2)

这里有三个问题。首先,函数polyn不依赖于积分T的变量,因为该变量被集成。从参数列表中删除T.因此,在trueydata计算中删除一个数值:

trueydata = vcurve(truexdata, 3, 4) 

其次,quad返回元组(integral_value, integral_error)。使用[0]仅返回整数值。

def polyn(x, A, Ea):
    return integrate.quad(integrand, 25, x, args=(A, Ea))[0]

第三,向curve_fit提供参数值的初始猜测。如果不这样做,可能会报告无法确定要适合的参数数量。即使成功,它也会盲目地使用all-one进行初步猜测。理解优化问题的人提供的初始猜测通常是多变量优化成功与失败之间的差异。

popt, pcov = optimize.curve_fit(vcurve, xdata, ydata, [2, 3])