{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Temperature Control Lab for Python Introduction, Parts 3 and 4\n", "\n", "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:\n", "\n", "* Maintain temperature of a home during the winter\n", "* Pre-heat an oven for baking\n", "* Regulate the temperature in a chemical reactor\n", "* Reduce temperature variations during semiconductor production\n", "* Adjust the infrared heater for a newborn baby in an incubator to maintain body temperature\n", "* Regulate natural gas to a water heater to provide consistent hot water\n", "* Adjust flow through a heat exchanger to maintain outlet temperature\n", "* Others?\n", "\n", "The heaters and LED can be adjusted between 0-100% output. The heater effect is shown in the plot below.\n", "\n", "![Temperature Control Lab](http://apmonitor.com/pdc/uploads/Main/pid_control.gif \"TCLab Test\")\n", "\n", "This Python lab covers the following topics:\n", "\n", "* Part 3: Interpolation, Comparison to Regression\n", "* Part 4: Solve Linear, Nonlinear, and Differential Equations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Install TCLab and Load Packages\n", "\n", "[Retrieve tclab.py from Github](https://github.com/APMonitor/arduino/blob/master/0_Test_Device/Python/tclab/tclab.py) 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." ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# download tclab.py from \n", "try:\n", " import tclab\n", "except:\n", " !pip install tclab\n", " import tclab\n", "\n", "# import additional packages \n", "import time\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "from scipy.optimize import fsolve\n", "from scipy.integrate import odeint" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Connect Test TCLab to Computer with USB Cable\n", "\n", "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](http://apmonitor.com/pdc/index.php/Main/ArduinoSetup) if the connection is unsuccessful.\n", "\n", "![Temperature Control Lab Heaters](http://apmonitor.com/pdc/uploads/Main/tclab_cables.jpg \"USB Cable and Heater Connection\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "TCLab version 0.4.6\n", "Arduino Leonardo connected on port COM3 at 115200 baud.\n", "TCLab Firmware Version 1.01.\n", "T1: 28.31\n", "T2: 27.96\n", "TCLab disconnected successfully.\n" ] } ], "source": [ "with tclab.TCLab() as lab:\n", " for i in range(5):\n", " lab.LED(100) # Turn on LED (0-100%)\n", " time.sleep(0.5) # Pause for 0.5 second\n", " lab.LED(0) # Turn off LED\n", " time.sleep(0.5) # Pause for 0.5 second\n", " print('T1: ' + str(lab.T1))\n", " print('T2: ' + str(lab.T2)) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Parts 1 and 2\n", "\n", "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:\n", "\n", "[Jupyter Notebook for TCLab Parts 1 and 2](https://nbviewer.jupyter.org/url/apmonitor.com/che263/uploads/Main/TCLab_A.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Part 3: Interpolation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### A: Heater Step Test Data\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### B: Interpolation\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### C: Interpolation versus Regression\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Part 4: Solve Equations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### A: Linear Equations\n", "\n", "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$:\n", "\n", "| Time (sec) | Temperature (degC) |\n", "|------|------|\n", "| 0 | 23.0 |\n", "| 90 | 33.0 |\n", "| 180 | 43.0 |\n", "\n", "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$.\n", "\n", "$a_0 + a_1 \\; 0 + a_2 \\; 0^2 = 23.0$\n", "\n", "$a_0 + a_1 \\; 90 + a_2 \\; 90^2 = 33.0$\n", "\n", "$a_0 + a_1 \\; 180 + a_2 \\; 180^2 = 43.0$\n", "\n", "In matrix form, the set of linear equations becomes: \n", "\n", "$\\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}$\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### B: Nonlinear Equations\n", "\n", "Fit the $T_1$ data to a nonlinear correlation using only three data points.\n", "\n", "$T_1 = a + b \\exp{(c \\, time)}$\n", "\n", "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$:\n", "\n", "| Time (sec) | Temperature (degC) |\n", "|------|------|\n", "| 0 | 22.0 |\n", "| 90.3 | 42.0 |\n", "| 180.5 | 52.0 |\n", "\n", "Solve for the three parameters from the three equations that exactly intersect the required data points.\n", "\n", "$22.0 = a + b \\exp{(c \\, 0)}$\n", "\n", "$42.0 = a + b \\exp{(c \\, 90.3)}$\n", "\n", "$52.0 = a + b \\exp{(c \\, 180.5)}$\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### C: Differential Equation\n", "\n", "Use the parameters $a$, $b$, and $c$ from the prior problem to solve the following ordinary differential equation (ODE).\n", "\n", "$\\frac{dx}{dt} = c (x-a)$\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.3" } }, "nbformat": 4, "nbformat_minor": 1 }