Presentation is loading. Please wait.

Presentation is loading. Please wait.

Curve Fitting the Calibration Data of a Thermistor Voltage Divider Portland State University Department of Mechanical Engineering ME 121: Engineering Problem.

Similar presentations


Presentation on theme: "Curve Fitting the Calibration Data of a Thermistor Voltage Divider Portland State University Department of Mechanical Engineering ME 121: Engineering Problem."— Presentation transcript:

1 Curve Fitting the Calibration Data of a Thermistor Voltage Divider Portland State University Department of Mechanical Engineering ME 121: Engineering Problem Solving

2 Thermistor Calibration Measurements To make measurements with an Arduino, the thermistor is located in the upper leg of a voltage divider. thermistor 10 kΩ 5V Analog input For the calibration measurements, the thermistor is placed in an insulated thermos, along with a thermometer and water. Voltage divider for Arduino input +5V or digital output analog input Insulated Coffee mug Thermistor probe Reference Thermometer ME 121: Engineering Problem Solvingpage 1

3 Calibration Data Set Data storage Repeated readings of the voltage divider are averaged.The averaged values are displayed in the Arduino Serial Monitor. The displayed data is copied and pasted into columns of an Excel spreadsheet. The first row of the spreadsheet is the temperature for the data set. Columns under the first row are the averaged raw analog input values. After the measurements are complete, the data inthe Excel worksheet is saved as a tab- delimited text file. 123...nc Temperature in first row Analog input values in other rows ME 121: Engineering Problem Solvingpage 2

4 Analysis of Calibration Data with Matlab The analysis of the calibration data involves these steps 1.Read the data into MATLAB. 2.Plot histograms of the raw readings to determine the variability of the calibration readings. 3.Compute the mean of the raw readings 4.Curve fit the raw voltage divider readings as a function of temperature 5.Swap the roles of the data to curve fit the temperature as a function of voltage divider readings. The quality of the curve fits is assessed with plots of the residuals. ME 121: Engineering Problem Solvingpage 3

5 Read the Calibration Data into MATLAB The built-in load function reads an array of plain text data into a matrix D = load(’thermistor_data.txt’) 123...nc D is a matrix. The first row of D is the temperature. T = D(1,:); The remaining rows are voltage divider readings V = D(2:end,:); Most of the work involves analyzing the data in the new matrix V. Temperature in first row Analog input values in other rows ME 121: Engineering Problem Solvingpage 4

6 Read the Calibration Data into MATLAB Compute the average of each of the columns in V Vave = mean(V); Vave is a row vector with the same number of elements as T. The length (number of elements) in both T and Vave is equal to the number of columns in the data set. ME 121:: Engineering Problem Solvingpage 5

7 Plot the Calibration Data It is easy to plot the mean data. plot(T,Vave,’o’); xlabel(’Temperature (C)’); ylabel(’Analog reading’); But we are getting ahead of ourselves. We should first look at the quality of the data by making histograms of the readings for each calibration temperature. 01020 30 Temperature (C) 405060 300 350 400 450 500 550 600 650 700 750 Analog reading For convenience, the following page lists a MATLAB function to load and plot the data. ME 121: Engineering Problem Solvingpage 6

8 Plot Calibration Data function plot_calibration_data(fname) % plot_calibration_dataPlot raw calibration data for the thermistor % -- Supply a default file name if nargin<1, fname=’thermistor_data.txt’;end % -- Load the data and compute summary statistics for each column D = load(fname); T = D(1,:); V = D(2:end,:); Vave = mean(V); %Temperature values in first row %Voltage data in columns from second row until end %Mean of data in each column % -- Plot the averaged reading versus the calibration temperature plot(T,Vave,’o’); xlabel(’Temperature (C)’);ylabel(’Analog reading (10-bit scale)’); ME 121: Engineering Problem Solvingpage 7

9 Histogram of the Calibration Data (1) It is easy to plot a histogram of the data in the first column. hist(V(:,1)) xlabel(’Analog reading (10-bit scale)’) ylabel(’Number of readings’) V(:,1) is the vector of data in the first column of V. The : is a wildcard meaning all of the row indices. There appears to be some spread in the data, but look at the range! 739.1739.2 739.3739.4 Analog reading (10−bit scale) 739.5739.6 0 2 4 6 8 10 12 14 16 18 20 Number of readings The analog readings are integer values. The entire set of readings at this temperature fall between 739.1 and 739.6. The non-integer values occur because each of the readings in the data set is an average of 20 readings. ME 121: Engineering Problem Solvingpage 8

10 Histogram of the Calibration Data (2) There is no significant variability for the data in the first column. Let’s check the other columns (other temperatures). 739.1739.2 739.3739.4 Analog reading (10−bit scale) 739.5739.6 0 2 4 6 8 10 12 14 16 18 20 Number of readings The histogram of the second column is obtained with hist(V(:,2)) which produces the plot to the right. Again, the readings fall in a very narrow of analog input values. 665665.1 665.2665.3 Analog reading (10−bit scale) 665.4665.5 0 5 10 15 20 25 30 Number of readings ME 121: Engineering Problem Solvingpage 9

11 Histogram of the Calibration Data (3) We can automate the creation of histograms with a loop over all columns in V. First, extract the number of columns for the data set with the built-in size command nc = size(V,2) This is nice: our data analysis automatically adjusts to the size – number of columns and number of rows – of the data set. No mouse clicks involved! With nc known, the following loop computes histograms for each of the columns in V nc = size(V,2) for j=1:nc figure hist(V(:,j)) %Open a new plot window %Histogram of data in column j xlabel(’Analog reading (10-bit scale)’) ylabel(’Number of readings’) end ME 121: Engineering Problem Solvingpage 10

12 Histogram of the Calibration Data (4) We can combine the histograms in a single window by using the subplot command to create an array of plots.. The syntax is subplot(nrow,ncol,p) where nrow is the number of rows in the array, ncol is the number of columns in the array, and p is a counter for the plot number in the array. The diagram to the right shows three possible layouts of subplots. All practical combinations of nrow and ncol are allowed. 1 2 subplot(2,1,p) p = 1,2 subplot(3,2,p) p = 1,2,...6 12 3 5 4 6 subplot(nprow,2,p) p = 1,2,...nc 12 34 nc nprow... ME 121: Engineering Problem Solvingpage 11

13 Histogram of the Calibration Data (5) The following code snippet uses the subplot command to layout the histograms for the columns of V in an nprow × 2 array end The plot_thermistor_histograms.m function (on the next slide) mass-produces histograms: one for each column in the data set. ME 121: Engineering Problem Solvingpage 12 nc = size(V,2); nprow = ceil(nc/2); %% number of columns number of rows for array of subplots for j=1:nc subplot(nprow,2,j); %Move to the next subplot hist(V(:,j)); ylim( [0, 40] ); %% Bin the data and plot the histogram Set the same y-axis limits in all plots

14 Histogram of the Calibration Data (6) function plot_thermistor_histograms(fname) % plot_thermistor_histogramsPlot histograms of calibration data for thermistor if nargin<1, fname=’thermistor_data.txt’;end%Default name of data file % -- Load the data and compute summary statistics for each column D = load(fname); T = D(1,:); V = D(2:end,:); Vave = mean(V); Vmed = median(V); Vstd = std(V); nc = size(V,2); %Temperature values in first row %Voltage data in columns from second row until end %Mean of data in each column %Median of data in each column %Standard deviation of each column %Number of columns in V Number of rows in the array of histograms nprow = ceil(nc/2); % fprintf(’\nT(C) for j=1:nc mean(V) median(V) std_dev(V)\n’); text(min(V(:,j))+0.1, 33, sprintf(’T = %-6.1f’,T(j)));%label the subplot fprintf(’%8.1f %8.1f %8.1f %8.2f\n’,T(j),Vave(j),Vmed(j),Vstd(j)); end ME 121: Engineering Problem Solvingpage 13 subplot(nprow,2,j);%Move tothe next subplot hist(V(:,j));%Bin thedata and plot the histogram ylim( [0, 40] );%Set thesame y-axis limits in all plots

15 Histogram of the Calibration Data (7) Variability at all temperatures the data sets is negligible. 0 40 20 40 T = 51.9 0 40 20 40 T = 42.4 578578.2578.4578.6578.8579 0 20 T = 32.4 528528.2528.4528.6528.8529 0 20 T = 27.1 493493.5494494.5495 0 20 40 470470.2470.4470.6470.8471 0 20 40 T = 21.3 447447.5448448.5449 0 20 40 T = 19.0 429429.2429.4429.6429.8430 0 20 40 T = 17.3 386386.5387387.5388 0 20 325325.5326326.5327 0 20 T = 6.4 314314.5315315.5316 0 40 20 ME 121: Engineering Problem Solvingpage 14 T = 23.7 T = 13.2 T = 6.0

16 Summary data The plot_thermistor_histograms.m function also prints the mean, median and standard deviation of each column of data. Remember: each column contains the analog readings for a given calibration temperature. plot_thermistor_histograms The next step is to create a curve fit of the analog readings versus the temperature. ME 121: Engineering Problem Solvingpage 15 T(C)mean(V)median(V)std_dev(V) 51.9739.3 0.08 42.4665.3 0.08 32.4578.4 0.07 27.1528.7 0.06 23.7494.1 0.07 21.3470.5 0.07 19.0448.1 0.07 17.3429.6 0.08 13.2386.9 0.07 6.4325.9 0.15 6.0315.0 0.07

17 Calibration Curve Fit Equation The calibration process suggests a curve fit of the form V = f (T ): the water temperature was chosen by mixing warm and cold water supplies, and the voltage output was recorded with the Arduino. We could use a polynomial for V (T ). V = c 1 T n + c 2 T n−1 + · · · + c n-1 T + c n (1) However, to control the temperature of the fish tank, we will need T = f (V ). T = a 1 V n + a 2 V n−1 + · · · + a n-1 V + a n (2) ME 121: Engineering Problem Solvingpage 16

18 Polynomial Curve Fit in MATLAB Assume the data is in the T and V xlabel(’Temperature (C)’); ylabel(’Analog output’); 01020 30 Temperature (C) 405060 300 350 400 750 Analog output The fit looks good, but we should dig deeper ME 121: Engineering Problem Solvingpage 17 ave vectors. 700 c = polyfit(T,Vave,2); 650 600 Tfit = linspace(min(T),max(T)); 550 Vfit = polyval(c,Tfit); 500 plot(T,Vave,’o’,Tfit,Vfit,’r--’); 450

19 Residual of the Curve Fit (1) We often use R 2 as an indicator of the fit. There is more information in the residuals r i = y i − y fit (x i ) where (x i, y i ) are the measured data and y fit (x) is the fitting function, e.g., a polynomial. r i = y i - y fit (x i ) ME 121: Engineering Problem Solvingpage 18

20 Residual of the Curve Fit (1) Given the curve fit evaluated with polyfit, we can compute the residuals in one line Vave is a row vector of the average for each column polyval(c,T) is a row vector of the curve fit evaluated at each of the measured T values. Vave - polyval(c,T) is a row vector of residuals: the difference between the measured (average) V values and the V values predicted by the curve fit. The fit_thermistor_calibration function puts the curve fit and residual computation and plot together in one m-file. ME 121: Engineering Problem Solvingpage 19 c = polyfit(T,Vave,2);%coefficients of polynomial curve fit:V = f(T) rfit = Vave - polyval(c,T);%residuals of the fit:r(xi) = yi - f(xi)

21 Matlab Function for Curve Fit function fit_thermistor_calibration(npoly,fname) % fit_thermistor_calibrationPolynomial curve fits of calibration data for thermistor %... See fit_thermistor_calibration.m for missing code to read data and compute Vave % -- Perform curve fit c = polyfit(T,Vave,npoly); fprintf(’\nCurve fit coefficients\n’); fprintf(’\t%12.7e\n’,c); % -- Plot the curve fit Tfit = linspace(min(T),max(T)); vfit = polyval(c,Tfit); subplot(2,1,1) plot(T,Vave,’o’,Tfit,vfit,’r--’); xlabel(’Temperature (C)’);ylabel(’Analog output’); legend(’Data’,sprintf(’Degree %d poly fit’,npoly),’Location’,’Northwest’); % -- Evaluate residuals at the known (measured) input values rfit = Vave - polyval(c,T); subplot(2,1,2) plot(T,rfit,’o’); xlabel(’Temperature (C)’);ylabel(’Residual of the fit’); ME 121: Engineering Problem Solvingpage 20

22 Matlab Quadratic Curve Fit fit_thermistor_calibration(2) produces a quadratic fit and a residual plot 01020 30 Temperature (C) 405060 300 600 500 400 700 800 Data Degree 2 poly fit Analog output 01020 30 Temperature (C) 405060 −5 10 5 0 Residual of the fit ME 121: Engineering Problem Solvingpage 21

23 Matlab Quadratic Curve fit Notice that the residuals appear to have a pattern. The quadratic fit is missing information in the data. 01020 30 Temperature (C) 405060 300 600 500 400 700 800 Analog output Data Degree 2 poly fit 01020 30 Temperature (C) 405060 −5 10 5 0 Residual of the fit ME 121: Engineering Problem Solvingpage 22

24 Matlab Cubic Curve Fit fit_thermistor_calibration(3) produces a cubic fit. The residuals appear to be random When the residuals are random, stop increasing the order of the polynomial. 01020 30 Temperature (C) 405060 300 600 500 400 700 800 Data Degree 3 poly fit Analog output 01020 30 Temperature (C) 405060 −4 6 4 2 0 −2 Residual of the fit ME 121: Engineering Problem Solvingpage 23


Download ppt "Curve Fitting the Calibration Data of a Thermistor Voltage Divider Portland State University Department of Mechanical Engineering ME 121: Engineering Problem."

Similar presentations


Ads by Google