The TCLab is a hands-on application of machine learning and advanced temperature control with two heaters and two temperature sensors. The labs reinforce principles of model development, estimation, and advanced control methods. This is the fifth exercise and it involves estimating parameters in a multi-variate energy balance model with added empirical elements. The predictions are aligned to the measured values through an optimizer that adjusts the parameters to minimize a sum of squared error or sum of absolute values objective. This lab builds upon the TCLab C by using the estimation of parameters in an energy balance but also adding additional equations that give a 2nd order response. The 2nd order response comes from splitting the finite element model of the heaters and temperature sensors. This creates a total of 4 differential equations with parameters that are adjusted to minimize the difference between the predicted and measured temperatures.
Estimation is the process of inferring internal states or parameters of a dynamic system using limited measurements and a model. This is essential in control applications, where direct measurement of all states is often impractical. By fusing models with sensor data, estimation provides the best possible approximation of the system's true state. However, estimation is not without its challenges. Noise in measurements, uncertainty in models, nonlinear system dynamics, and constraints on states complicate the task of designing robust and reliable estimators.
Dynamic Estimation Objectives
At the core of estimation lies the definition of an objective function that quantifies the discrepancy between model predictions and actual measurements. A common choice is the sum of squared errors, represented mathematically as `J_{2}=\sum (y_{\text{meas}}-y_{\text{pred}})^2`. This formulation is widely used due to its differentiability, which simplifies optimization. Alternatively, the sum of absolute errors (L1-norm), `J_{1}=\sum |y_{\text{meas}}-y_{\text{pred}}|`, is preferred in applications requiring robustness against outliers. The general optimization formulation for dynamic estimation is given by:
subject to the system dynamics `\hat{x}_{k+1} = f(\hat{x}_k, u_k) ` and constraints that enforce physical feasibility. The choice of objective function impacts estimation accuracy and computational efficiency, influencing how well the estimator can reject noise and adapt to changing conditions.
Overview of Estimation Methods
Several approaches exist for state and parameter estimation. Moving Horizon Estimation (MHE) is an optimization-based technique that processes a sliding window of past data to estimate current states and parameters while incorporating constraints. The Kalman Filter (KF) is a recursive estimator that provides optimal state estimates for linear systems under Gaussian noise assumptions. Bayesian estimation methods, such as particle filters, explicitly apply Bayes' theorem to maintain probability distributions over states rather than single estimates. Least Squares (LS) estimation, particularly in its recursive form, is widely used for parameter identification and system modeling.
Comparison of Estimation Methods
The choice of estimation method depends on the problem at hand. The Kalman filter is computationally efficient but assumes linearity and Gaussian noise, making it less suitable for highly nonlinear systems. MHE, while more computationally demanding, naturally incorporates constraints and nonlinearity, often leading to superior accuracy in complex systems. Bayesian methods such as particle filters can handle non-Gaussian noise and multi-modal distributions but require significant computational resources. Least Squares estimation is effective for batch processing but lacks the adaptability needed for real-time applications.
Hybrid Model Development
Hybrid modeling combines physics-based first-principles equations with empirical data-driven corrections. This approach leverages known physical laws to ensure consistency while incorporating empirical elements to enhance accuracy. Hybrid models are particularly useful when the available physical models are incomplete or when data-driven techniques alone fail to generalize beyond the training set. Examples of hybrid modeling applications include climate simulations, manufacturing process control, and robotics, where physics-based constraints and data-driven flexibility are both required.
Case Study: TCLab Hybrid Model Estimation
The Temperature Control Lab (TCLab) is an Arduino-based system with two heaters and temperature sensors, providing a controlled environment for testing estimation and control algorithms. In this case study, a hybrid model is developed, integrating an energy balance formulation with empirical lag dynamics to better capture system behavior. Moving Horizon Estimation is employed to fine-tune key parameters, ensuring that the model predictions closely align with measured data.
The TCLab hybrid model consists of four ordinary differential equations describing the heater and sensor temperatures. The energy balance equations for the heaters are:
$$m C_p \frac{dT_{h1}}{dt} = U A (T_{\infty} - T_{h1}) + \epsilon \sigma A (T_{\infty}^4 - T_{h1}^4) + \alpha_1 Q_1$$
$$m C_p \frac{dT_{h2}}{dt} = U A (T_{\infty} - T_{h2}) + \epsilon \sigma A (T_{\infty}^4 - T_{h2}^4) + \alpha_2 Q_2$$
where $T_{\infty}$ represents ambient temperature, $U$ is the heat transfer coefficient, and $\alpha_1,\alpha_2$ are heater power-to-heat conversion factors. To account for sensor delays, additional empirical lag equations are introduced:
MHE optimizes the parameters (U, `\tau`, `\alpha_1`, `\alpha_2`) to minimize the estimation error while maintaining physically meaningful constraints.
Python Implementation of MHE
The estimation framework is implemented using the GEKKO library in Python. The process involves defining system equations, incorporating measurement data, configuring solver settings, and iteratively updating estimates. The sliding horizon approach continuously updates state and parameter estimates as new data become available, ensuring real-time adaptability.
Results and Interpretation
Applying MHE to TCLab data results in agreement between predicted and measured temperatures. The hybrid model successfully captures both fast and slow thermal responses. Estimated parameters, including heat transfer coefficients and lag constants, converge to stable and physically meaningful values. The choice between L1 and L2 norms in the objective function influences noise sensitivity, with L1 providing superior robustness to outliers.
Challenges in Estimation and Hybrid Modeling
Despite their advantages, estimation methods face challenges such as handling noisy data, computational complexity, and ensuring real-time performance. MHE and Bayesian methods require significant processing power, making them less feasible for high-speed applications without optimization techniques. Model uncertainty remains a key issue, as estimators are only as accurate as the models they rely on. Careful model selection, robust tuning, and noise rejection strategies are necessary to achieve reliable estimation.
import numpy as np import matplotlib.pyplotas plt import pandas as pd from gekko import GEKKO
# Import or generate data
filename ='tclab_dyn_data2.csv' try:
data = pd.read_csv(filename) except:
url ='https://apmonitor.com/do/uploads/Main/tclab_dyn_data2.txt'
data = pd.read_csv(url)
# Create GEKKO Model
m = GEKKO()
m.time= data['Time'].values
# Parameters to Estimate
U = m.FV(value=10,lb=1,ub=20)
tau = m.FV(value=20,lb=15,ub=25)
alpha1 = m.FV(value=0.01,lb=0.003,ub=0.03)# W / % heater
alpha2 = m.FV(value=0.005,lb=0.002,ub=0.02)# W / % heater
Ta = m.Param(value=19.0+273.15)# K
mass = m.Param(value=4.0/1000.0)# kg
Cp = m.Param(value=0.5*1000.0)# J/kg-K
A = m.Param(value=10.0/100.0**2)# Area not between heaters in m^2
As = m.Param(value=2.0/100.0**2)# Area between heaters in m^2
eps = m.Param(value=0.9)# Emissivity
sigma = m.Const(5.67e-8)# Stefan-Boltzmann
Note: Switch to make_mp4 = True to make an MP4 movie animation. This requires imageio and ffmpeg (install available through Python). It creates a folder named figures in your run directory. You can delete this folder after the run is complete.
import numpy as np importtime import matplotlib.pyplotas plt importrandom # get gekko package with: # pip install gekko from gekko import GEKKO # get tclab package with: # pip install tclab from tclab import TCLab
# Connect to Arduino
a = TCLab()
# Make an MP4 animation?
make_mp4 =False if make_mp4: import imageio # required to make animation importos try: os.mkdir('./figures') except: pass
# Final time
tf =10# min # number of data points (1 pt every 3 seconds)
n = tf * 20 + 1
# Configure heater levels # Percent Heater (0-100%)
Q1s = np.zeros(n)
Q2s = np.zeros(n) # Heater random steps every 50 sec # Alternate steps by Q1 and Q2
Q1s[3:]=100.0
Q1s[50:]=0.0
Q1s[100:]=80.0
Q2s[25:]=60.0
Q2s[75:]=100.0
Q2s[125:]=25.0
# rapid, random changes every 5 cycles between 50 and 100 for i inrange(130,180): if i%10==0:
Q1s[i:i+10]=random.random() * 100.0 if(i+5)%10==0:
Q2s[i:i+10]=random.random() * 100.0
######################################################### # Initialize Model as Estimator #########################################################
m = GEKKO(name='tclab-mhe',remote=False)
# 60 second time horizon, 20 steps
m.time= np.linspace(0,60,21)
# Parameters to Estimate
U = m.FV(value=10,name='u')
U.STATUS=0# don't estimate initially
U.FSTATUS=0# no measurements
U.DMAX=1
U.LOWER=5
U.UPPER=15
tau = m.FV(value=5,name='tau')
tau.STATUS=0# don't estimate initially
tau.FSTATUS=0# no measurements
tau.DMAX=1
tau.LOWER=4
tau.UPPER=8
alpha1 = m.FV(value=0.01,name='a1')# W / % heater
alpha1.STATUS=0# don't estimate initially
alpha1.FSTATUS=0# no measurements
alpha1.DMAX=0.001
alpha1.LOWER=0.003
alpha1.UPPER=0.03
alpha2 = m.FV(value=0.0075,name='a2')# W / % heater
alpha2.STATUS=0# don't estimate initially
alpha2.FSTATUS=0# no measurements
alpha2.DMAX=0.001
alpha2.LOWER=0.002
alpha2.UPPER=0.02
# State variables
TH1 = m.SV(value=T1m[0],name='th1')
TH2 = m.SV(value=T2m[0],name='th2')
# Measurements for model alignment
TC1 = m.CV(value=T1m[0],name='tc1')
TC1.STATUS=1# minimize error between simulation and measurement
TC1.FSTATUS=1# receive measurement
TC1.MEAS_GAP=0.1# measurement deadband gap
TC1.LOWER=0
TC1.UPPER=200
TC2 = m.CV(value=T2m[0],name='tc2')
TC2.STATUS=1# minimize error between simulation and measurement
TC2.FSTATUS=1# receive measurement
TC2.MEAS_GAP=0.1# measurement deadband gap
TC2.LOWER=0
TC2.UPPER=200
Ta = m.Param(value=23.0+273.15)# K
mass = m.Param(value=4.0/1000.0)# kg
Cp = m.Param(value=0.5*1000.0)# J/kg-K
A = m.Param(value=10.0/100.0**2)# Area not between heaters in m^2
As = m.Param(value=2.0/100.0**2)# Area between heaters in m^2
eps = m.Param(value=0.9)# Emissivity
sigma = m.Const(5.67e-8)# Stefan-Boltzmann
# Turn off heaters
a.Q1(0)
a.Q2(0) # Save figure
plt.savefig('tclab_mhe.png')
# generate mp4 from png figures in batches of 350 if make_mp4:
images =[]
iset =0 for i inrange(1,n):
filename='./figures/plot_'+str(i+10000)+'.png'
images.append(imageio.imread(filename)) if((i+1)%350)==0:
imageio.mimsave('results_'+str(iset)+'.mp4', images)
iset +=1
images =[] if images!=[]:
imageio.mimsave('results_'+str(iset)+'.mp4', images)
# Allow user to end loop with Ctrl-C exceptKeyboardInterrupt: # Disconnect from Arduino
a.Q1(0)
a.Q2(0) print('Shutting down')
a.close()
plt.savefig('tclab_mhe.png')
# Make sure serial connection still closes when there's an error except: # Disconnect from Arduino
a.Q1(0)
a.Q2(0) print('Error: Shutting down')
a.close()
plt.savefig('tclab_mhe.png') raise
Emerging research in estimation focuses on integrating machine learning with traditional methods. Learning-based Kalman filters, hybrid models with adaptive online learning, and uncertainty-aware Bayesian estimators are gaining attention. Improved solver efficiency and distributed estimation techniques enable applications in large-scale systems such as industrial automation and autonomous robotics. Future estimators will likely leverage deep learning and probabilistic modeling to enhance robustness and adaptability.
Conclusion
Estimation is a cornerstone of control and optimization, enabling real-time decision-making based on incomplete information. The choice of estimation technique depends on computational constraints, system complexity, and noise characteristics. Hybrid modeling offers a promising avenue for improving estimation accuracy by integrating physics-based and data-driven methods. As research advances, estimation frameworks will continue to evolve, incorporating learning-based adaptations and advanced uncertainty quantification techniques to enhance applicability in real-world systems.