【问题标题】:Linear combination of temperature sensors curve fitting温度传感器曲线拟合的线性组合
【发布时间】:2018-09-18 20:01:44
【问题描述】:

我正在尝试对不同的温度传感器进行线性组合,并使用应变传感器对其进行曲线化。

我所做的是我可以将一个温度传感器与一个应变传感器配合使用。

但我不知道如何在一个应变传感器上进行不同温度传感器的线性组合。

这是我的尝试:

def process_data_curve_fitting(temperature, strain):

   #mean_T = (temperature[[i for i in temperature.columns.tolist() if str(i)[:2] == 'TW']].mean(axis=1))
   print("process data")

   T1 = temperature['T1'].tolist()
   T2 = temperature['T2'].tolist()
   T3 = temperature['T3'].tolist()
   T4 = temperature['T4'].tolist()
   T5 = temperature['T5'].tolist()
   T6 = temperature['T6'].tolist()
   T7 = temperature['T7'].tolist()
   T8 = temperature['T8'].tolist()
   T9 = temperature['T9'].tolist()
   T10 = temperature['T10'].tolist()

   df = pd.DataFrame(list(zip(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)))
   mean_T = df.mean(axis = 1)

   print(mean_T)
   Sensor_Names = [ 'W_A1', 'W_A2', 'W_F1', 'W_F2', 'W_F4', 'W_S1', 'W_S2', 'W_S3', 'W_S4', 'W_KF1', 'W_KF2', 'W_KF3', 'W_KF4', 'W_DB1', 'W_DB2']
   ys = []
   for i in range(len(strain)):
       cof = np.polyfit(mean_T, strain[i], 2)
       poly = np.polyval(cof, mean_T)
       ys.append(poly)
       print (cof)
       print (poly)

   for i in range(len(strain)):
       fig = plt.figure()
       plt.scatter(mean_T, strain[i],s=0.1)
      # fig.savefig(r'c:\\ahmed\\'+Sensor_Names[i]+'.png')
       plt.plot(mean_T, ys[i], color='r')
       fig.savefig(r'c:\\ahmed\\'+"Curve_fitting__" + Sensor_Names[i]+'.png',dpi=300)

       plt.ylabel('strain' + Sensor_Names[i])
       plt.xlabel('temperature')

请看方程

【问题讨论】:

  • 如果没有任何数据、示例输入、示例输出以及仅仅理解您的代码,很难尝试帮助您。
  • 您必须更清楚地解释您的问题。目前,您只是在np.polyfit(mean_T, strain[i], 2) 中适应变化应变的平均温度。你到底想适应什么?从您的问题来看,您似乎想要计算平均应变并使其适应不同的温度。您可以按照与平均温度拟合相同的方式进行操作。问题到底出在哪里?
  • @Bazingaa 我想对每个应变传感器拟合三个温度传感器的线性组合,现在我能做的是将所有温度传感器的平均值与每个应变传感器拟合
  • 你需要的可能是一个 3 度的 polyfit(方程 33)

标签: python pandas curve-fitting


【解决方案1】:

作为两个温度传感器的“概念验证” (这里既不放噪音也不考虑实际参数):

import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import leastsq

def strain( t, a, b, c, d ):
    return a * t**3 + b * t**2 + c * t + d

def residuals( params, x1Data, x2Data, yData ):
    s1, s2, a, b, c, d = params
    cxData = [ (s1**2 * x1 + s2**2 * x2) /( s1**2 + s2**2 ) for x1, x2 in zip( x1Data, x2Data) ]
    diff = [ strain( x, a, b, c, d ) -y for x, y in zip( cxData, yData ) ]
    return diff

timeList = np.linspace( 0, 25, 55 )
t1List = np.fromiter( (  5 + 25. * (1 - np.exp( -t / 9. ) )for t in timeList ), np.float )
t2List = np.fromiter( (30. * (1 - np.exp( -t / 7. ) ) * ( 1 - np.exp( -t / 3. ) ) for t in timeList ), np.float )

combinedList = np.fromiter( ( (.7 * a + .2 * b)/.9 for a, b in zip( t1List, t2List ) ), np.float )
strainList = np.fromiter( ( strain( t, .01, -.1, .88, .2 ) for t in combinedList  ), np.float )

fit, ier = leastsq( residuals, [.71,.22, 0,0, .1, .1 ], args=( t1List, t2List, strainList ), maxfev=5000 )
print fit 
fittedT = [ (fit[0]**2 * x1 + fit[1]**2 *x2 ) /( fit[0]**2 + fit[1]**2 ) for x1, x2 in zip( t1List, t2List) ]
fittedS = [ strain( t, *(fit[2:]) ) for t in fittedT ]

fig = plt.figure()
ax = fig.add_subplot( 3, 1, 1 )
bx = fig.add_subplot( 3, 1, 2 )
cx = fig.add_subplot( 3, 1, 3 )
ax.plot( timeList, t1List )
ax.plot( timeList, t2List )
ax.plot( timeList, combinedList )
bx.plot( combinedList, strainList, linestyle='', marker='x' )
bx.plot( fittedT, fittedS )
cx.plot( timeList, fittedT ,'--')
cx.plot( timeList, combinedList,':' )
plt.show()

给予

[ 4.21350842e+03  2.25221499e+03  1.00000000e-02 -1.00000000e-01 8.80000000e-01  2.00000000e-01]

并显示:

顶部:温度 1(蓝色)和 2(橙色)以及线性组合(绿色) 中心:“模拟数据”(蓝色)和拟合(橙色) 底部:拟合温度(蓝色),真实温度(橙色)

根据实际数据,可能需要进行一些调整。

【讨论】:

  • 非常感谢,但是你做了两个温度和一个应变的线性组合,是否可以添加三个温度列表的线性组合?
  • 这些常量 [.71,.22, 0,0, .1, .1 ]
  • ...关于常量...您必须为算法提供一些起始参数才能找到其(可能是局部的)最小值。如果您对初始参数没有合理的猜测,请查看here,或直接在docs
  • 对于三个温度的概括,这很简单:只需相应地修改residual函数,即发送也x3Data将其放入zip()并修改cxData的定义和添加s3。如果您无法管理,请随时询问。
  • 看起来不错,除了 fittedS = [ strain( t, *(fit[2:]) ) for t in fittedT ] 现在必须是 fittedS = [ strain( t, *(fit[3:]) ) for t in fittedT ]fittedS = [ strain( t, *(fit[-4:]) ) for t in fittedT ]
猜你喜欢
  • 2017-09-15
  • 2019-08-11
  • 1970-01-01
  • 1970-01-01
  • 2019-03-15
  • 2021-05-08
  • 2021-01-19
  • 2015-03-02
  • 2013-05-22
相关资源
最近更新 更多