1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| import numpy as np import pylab as plt import pandas as pd from scipy.optimize import curve_fit exc = pd.read_excel("input.xlsx", usecols=range(0, 2)) data = exc.values
pos = np.array([[0, 0], [25, 55.5], [60.5, 91], [96, 126.5], [131.5, 162], [167, 197.5], [202.5, 233], [238, 268.5], [273.5, 304], [309, 339.5], [344.5, 375], [380, 410.5], [435.5, 460.5]] )
ranx = np.array([[0, pos[2][0]], [pos[2][0], pos[5][1]], [pos[5][1], pos[6][0]], [pos[6][0], pos[6][1]], [pos[6][1], pos[7][0]], [ pos[7][0], pos[7][1]], [pos[7][1], pos[8][0]], [pos[8][0], pos[9][1]], [ pos[9][1], pos[10][1]], [pos[10][1], pos[12][0]]])
def getab(i, tem): x1 = pos[i][1] x2 = pos[i+1][0] t1 = tem[i] t2 = tem[i+1] a = (t2-t1)/(x2-x1) b = t1-a*x1 return (a, b)
def getfx(x, tem): for i in range(0, 12): if (pos[i][0] <= x and x <= pos[i][1]): return (0, tem[i]) elif (pos[i][1] <= x and x <= pos[i+1][0]): return getab(i, tem=tem) return (-1, -1)
def f(tt, lmd): def gettem(t): a, b = getfx(t[0]*v, tem=tem) c = (T0-a*v*t0+a*v/lmd-b)*np.exp(lmd*t0) return a*v*t-a*v/lmd+b+c*np.exp(-lmd*t) return gettem(tt)
if __name__ == '__main__': tem = np.array([25, 173, 173, 173, 173, 173, 198, 230, 257, 257, 25, 25, 25]) v = 70/60
tT = data t = tT.T[0] T = tT.T[1] lmds = [] TT = [] x = t*v lj, j = 0, 0 for ranxi in ranx: while (j < len(x) and x[j] <= ranxi[1]): j += 1 t0 = t[lj] T0 = T[lj] popt, pcov = curve_fit(f, t[lj:j], T[lj:j]) lmds.extend(popt) TT.extend(f(t[lj:j], lmd=popt)) lj = j if (j >= len(x)): break
plt.scatter(t, T, s=1) plt.plot(t, TT, color='r') plt.savefig('getlmd.png', dpi=500) plt.show() print(lmds)
|