熱電対の温度と電圧の変換式を作る2
前回の記事では温度から熱起電力を求める式と熱起電力から温度を求める関数を用意していたが,逆が遅い.
とりあえず多項式近似なら,
前回
import numpy as np import scipy as scipy from scipy.optimize import fmin import matplotlib import matplotlib.pyplot as plt import time def GetK_Type_T2E(t): return -17.600413686 + 38.921204975 * t + 0.018558770032 * t ** 2 \ + -0.000099457592874 * t ** 3 + 0.00000031840945719 * t ** 4 \ + -5.6072844889E-10 * t ** 5 + 5.6075059059E-13 * t ** 6 \ + -3.2020720003E-16 * t ** 7 + 9.7151147152E-20 * t ** 8 \ + -1.2104721275E-23 * t ** 9 \ + 118.5976 * np.exp(-0.0001183432 * (t - 126.9686) ** 2); def f(t, *args): return np.abs(GetK_Type_T2E(t) - args[0]) def GetK_Type_E2T(e): t = e / 38.921204975; res = scipy.optimize.minimize_scalar(f, bracket=None, bounds=(t - 0.7, t + 0.7), args=(e), method='brent', tol=1e-10); #res = scipy.optimize.minimize_scalar(f, bracket=None, bounds=(t - 0.7, t + 0.7), args=(e), method='golden', options={'xtol': 1e-15}); print(res) return res.x; if __name__ == '__main__': start = time.time() t = 20 e = GetK_Type_T2E(t); print(t) e0 = GetK_Type_T2E(20) print(e0) t0 = GetK_Type_E2T(e0) print(t0) elapsed_time = time.time() - start print ("elapsed: {0}".format(elapsed_time) + " s")
今回
import numpy as np import scipy as scipy from scipy.optimize import fmin import matplotlib import matplotlib.pyplot as plt import time def GetK_Type_T2E(t): return -17.600413686 + 38.921204975 * t + 0.018558770032 * t ** 2 \ + -0.000099457592874 * t ** 3 + 0.00000031840945719 * t ** 4 \ + -5.6072844889E-10 * t ** 5 + 5.6075059059E-13 * t ** 6 \ + -3.2020720003E-16 * t ** 7 + 9.7151147152E-20 * t ** 8 \ + -1.2104721275E-23 * t ** 9 \ + 118.5976 * np.exp(-0.0001183432 * (t - 126.9686) ** 2); def GetK_Type_E2T(e): global w; return np.polyval(w, e); if __name__ == '__main__': start = time.time() ts = np.linspace(-40, 240, num=2801); es = GetK_Type_T2E(ts); w = np.polyfit(es, ts, 18) pvts = np.polyval(w, es) elapsed_time = time.time() - start print ("elapsed: {0}".format(elapsed_time) + " s") 今回の方が2801点計算しているのに同じ程度の時間で処理できている.
タグ:Python