Temperature Control Lab for Python Introduction, Parts 3 and 4

The TCLab (Temperature Control Lab) has an LED, two heaters, and two temperature sensors that are controlled with an Arduino. The heater power output is adjusted to maintain a desired temperature. Thermal energy from the heater is transferred by conduction, convection, and radiation to the temperature sensor. Heat is also transferred away from the device to the surroundings. This lab is a resource to learn data analysis, visualization, regression, and model analysis in Python with real data. Temperature control is an important problem in many applications such as:

  • Maintain temperature of a home during the winter
  • Pre-heat an oven for baking
  • Regulate the temperature in a chemical reactor
  • Reduce temperature variations during semiconductor production
  • Adjust the infrared heater for a newborn baby in an incubator to maintain body temperature
  • Regulate natural gas to a water heater to provide consistent hot water
  • Adjust flow through a heat exchanger to maintain outlet temperature
  • Others?

The heaters and LED can be adjusted between 0-100% output. The heater effect is shown in the plot below.

Temperature Control Lab

This Python lab covers the following topics:

  • Part 3: Interpolation, Comparison to Regression
  • Part 4: Solve Linear, Nonlinear, and Differential Equations

Install TCLab and Load Packages

Retrieve tclab.py from Github if pip install is not successful such as for computers where the user does not have administrative privileges to install packages. Include tclab.py in the same directory as the Jupyter notebook.

In [20]:
# download tclab.py from 
try:
    import tclab
except:
    !pip install tclab
    import tclab

# import additional packages 
import time
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from scipy.optimize import fsolve
from scipy.integrate import odeint

Connect Test TCLab to Computer with USB Cable

Connect the temperature control lab with the USB cable to the computer (MacOS, Windows, or Linux). Also, attach the power supply to the top barrel jack as shown below. There is an installation guide if the connection is unsuccessful.

Temperature Control Lab Heaters

In [2]:
with tclab.TCLab() as lab:
    for i in range(5):
        lab.LED(100)        # Turn on LED (0-100%)
        time.sleep(0.5)     # Pause for 0.5 second
        lab.LED(0)          # Turn off LED
        time.sleep(0.5)     # Pause for 0.5 second
    print('T1: ' + str(lab.T1))
    print('T2: ' + str(lab.T2))    
TCLab version 0.4.6
Arduino Leonardo connected on port COM3 at 115200 baud.
TCLab Firmware Version 1.01.
T1: 28.31
T2: 27.96
TCLab disconnected successfully.

Parts 1 and 2

Parts 1 and 2 should be completed before this lab. Part 1 covers Python loops, files, and plotting. Part 2 covers linear, nonlinear, and nonlinear regression. A Jupyter notebook for Parts 1 and 2 is available at the following link:

Jupyter Notebook for TCLab Parts 1 and 2

Part 3: Interpolation

A: Heater Step Test Data

Turn on heater 1 to 100% and record $T_1$ and $T_2$ every 10 seconds for 3 minutes. The data should include a total of 19 data points for each temperature sensor and the recording time, starting at zero.

In [ ]:
 

B: Interpolation

Create an interpolation function between the measured points for $T_1$ using a linear interpolation. Show the recorded $T_1$ as red dots and the linear interpolation as a black line. Add x-label and y-labels as well as a plot legend.

In [ ]:
 

C: Interpolation versus Regression

Create an interpolation function between the measured points for $T_2$ using a cubic spline. Compare the piecewise cubic spline to a 3rd order polynomial regression. Show the recorded $T_2$ as blue dots, the cubic spline interpolation as a black dashed line, and the polynomial regression as a red dotted line. Add appropriate labels to the plot.

In [ ]:
 

Part 4: Solve Equations

A: Linear Equations

Three points are required to specify a quadratic polynomial of the form $y =a_0 + a_1 \; x + a_2 \; x^2$. Create a quadratic regression of $T_2$ by using only the first, middle, and last data points. Suppose these were the following data points for $T_2$:

Time (sec) Temperature (degC)
0 23.0
90 33.0
180 43.0

Solve the linear regression as a set of three equations that are derived by plugging in the three data points to the polynomial equation to create three separate equations with $y=T_2$ and $x=time$.

$a_0 + a_1 \; 0 + a_2 \; 0^2 = 23.0$

$a_0 + a_1 \; 90 + a_2 \; 90^2 = 33.0$

$a_0 + a_1 \; 180 + a_2 \; 180^2 = 43.0$

In matrix form, the set of linear equations becomes:

$\begin{bmatrix}1 & 0 & 0 \\ 1 & 90 & 90^2 \\ 1 & 180 & 180^2 \end{bmatrix}\begin{bmatrix}a_0\\a_1\\a_2\end{bmatrix} = \begin{bmatrix}23.0\\33.0\\43.0\end{bmatrix}$

Solve this set of equations for the quadratic parameters $a_0$, $a_1$, and $a_2$ with the data collected at the beginning of this notebook. Plot the quadratic fit with the data to ensure that the curve goes through the three specified data points.

In [ ]:
 

B: Nonlinear Equations

Fit the $T_1$ data to a nonlinear correlation using only three data points.

$T_1 = a + b \exp{(c \, time)}$

Three points are required to uniquely specify a model with three parameters. When there are more than the minimum required number of points, a least squares regression is typically performed to minimize the squared error between the measured and predicted values as done in Part 2 of this lab. For this exercise, use only 3 points (first, middle, last) of the $T_1$ data. Suppose these were the following data points for $T_1$:

Time (sec) Temperature (degC)
0 22.0
90.3 42.0
180.5 52.0

Solve for the three parameters from the three equations that exactly intersect the required data points.

$22.0 = a + b \exp{(c \, 0)}$

$42.0 = a + b \exp{(c \, 90.3)}$

$52.0 = a + b \exp{(c \, 180.5)}$

Solve this set of equations for the unknown parameters $a$, $b$, and $c$ with the data collected at the beginning of this notebook. Use guess values of $a=100$, $b=-100$, and $c=-0.01$. Plot the nonlinear fit with the data to ensure that the curve goes through the three specified data points. Add appropriate labels to the plot.

In [ ]:
 

C: Differential Equation

Use the parameters $a$, $b$, and $c$ from the prior problem to solve the following ordinary differential equation (ODE).

$\frac{dx}{dt} = c (x-a)$

The initial condition is $x_0 = a + b$. Show the ODE solution at 100 time points between the initial time and the final time. Plot the measured temperature 1 on the same plot as the ODE predicted temperature $x$. Add appropriate labels to the plot.

In [ ]: