Thermistor calibration.¶
Step 1: Calibration data collection¶
- Use your therm_plotter.ipynb script to help you make a file of datetime, temperature, and resistence.
- Place your thermistor in the bath and then start logging with your thermplotter.
- Collect measurements of Resistence in a bath of ice water. Collect a minimum of N = 100 measurements over several minutes. This means your delay() should be 1 second or so.
- Move your thermistor to the other bath and collect measurements of Resistence in boiling water. Collect a minimum of N = 100 measurements over several minutes.
- Save the data in a .csv file for loading with this .ipynb.
Step 2: Complete this worksheet.¶
You will use the measurements of temperature and resistance to determine your own value of a,b, and c from the Steinhart-Hart equation:
$$ \frac{1}{T_{bath}} = a + b \cdot log_e(R) + c \cdot log_e(R)^3 $$Use the Steinhart-Hart equation. Solve for the three coefficients, a, b, c that are in the Steinhart-Hart equation.
NOTE: The output for the Steinhart-Hart equation is in Kelvin, so you should use Kelvin when generating a,b,c, but report your values of temperature in deg C.
# Load the libraries we need.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import datetime as dt
import numpy.linalg as linalg
from scipy.optimize import curve_fit
#%matplotlib qt
from matplotlib.dates import DateFormatter
myFmt = DateFormatter("%H:%M")
# Load your recorded measurements and plot the time series to confirm clean data.
# df = pd.read_csv(filename, parse_dates=[0]); # Note, use the parse_dates input to tell pandas which column has
# datetimes in it.
# df.info()
# Plot the Resistance vs. datetime to determine if there is some data that needs to be cut out.
# This line makes the datetime axis easier to read
# ax.xaxis.set_major_formatter(myFmt)
Clean up your data as needed¶
(if your data is already clean, proceed).
# Cut out bad data points from the beginning and end, or from the transition between ice bath and boiling bath.
# These bad data will introduce bias into your results.
# Note, pandas lets you compare datetime values against text strings, e.g. df['time'] > '2021-10-19 09:51:00'
#cut.sum()
# df.drop(df.index[cut])
What is $T_{bath}$?¶
We explicitly chose to calibrate in boiling and icy water, because we know the temperature at these two phase transitions, on the celcius scale without having to measure it. Therefore, these are the values of $T_bath$. You need to create a column vector, called $T_bath$. Every row (time) that you measured $R_{100}$ the value of $T_bath$ should be 100. Every row (time) that measured $R_0$, the value of $T_{bath}$ should be 0.
Hint: If you also measured temperature, you can round to the 10's place to create a vector or 0 and 100 that matches your measured resistences.
Tbath = np.round(df['Temp'],-1)+273.16;
Fit the model using the Resistence and $T_{bath}$ data.¶
Choose one of the two approaches:
Use curve_fit() from scipy.optimize. Refer to the Scipy help for details of curve_fit()
solve the linear algebraic equation below, with design matrix A. Assemble the elements of $ x = \left(A^TA)^{-1}\times A^T T_{bath} \right) $. The columns in A are a column of 1's and a column of measurements of $R_{100}$ and $R_{0}$, or resistance at 100 and 0 celcius.
# Choice 1, define the objective function, which defines the inputs to the Steinhar-Hart equation. You will pass
# this function to curve_fit. You can name this function, what you want.
# https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html
#def steinhart_hart(R,a,b,c):
# # Return the output of the Steinhart-Hart equation, given R and the parameters, a,b,c
# return
# Make a column vector with values of Tbath.
# Make a pandas pd.series() of values of Resistence from the bath calibration. You will input these to curve_fit()
# Set up and solve the curve_fit. Capture the values of a,b,c which are output from curve_fit()
# pp,__ = curve_fit(objective_fun,R, 1/Tbath)
#a,b,c = pp
# Report the values of a,b,c - how do they compare with coefficients we used last week?
# Choice 2, use linear algebra to solve for a, b, c where
# x = [a,b,c]^T
# A = [1, np.log(R_i), np.log(R_i)^3]
# x = inv(A^T*A)*A^T*Tbath
# Assemble the columns of the design matrix, A.
# Use linalg.inv() and np.matmul() to carry out the matrix multiplication.
Plot and quantify the fit.¶
# Model the temperature.
# Use your measured resistence during calibration and the new S-H coefficients to compute Tmod,
# the modeled temperature.
# Make a plot of Resistance vs. Tbath and Resistence vs. Tmod. Are Tmod and Tbath visually consistent?
Compute and report the Pearson correlation coefficient to show how well your curve fits to the data.¶
$$ R^2 = 1 - \frac{ \sum\left(T_{bath,i} - T_{mod,i} \right)^2}{\left(\sum T_{bath,i} - n\bar{T_{bath}}\right)^2} $$Here, the values of $T_{bath,i}$ are the 'observations' of temperature at 0 and 100 C and $\hat{T}_i$ are the predicted values from the least-squares fit.
What to turn in? Turn in your completed .ipynb and make sure to attach the files containing data you recorded during the calibration in class.