1
©2009 by L. Lagerstrom
Curvefitting
• For an introduction to the basic concepts, see the
associated video clip
• Guessing the fitted line
• Checking the fit: the MSE and RMSE
• Finding the best fit line using polyfit
• Fitting higher order polynomials
Matlab code
Figure window display
©2009 by L. Lagerstrom
Guessing the Fitted Line
%Consider some (x,y) values repre
%senting temperature vs. time
%measurements.
x = 0:5
y = [0 20 60 68 77 110]
%Make an educated guess that the
%line y_hat = 20*x will fit the
%data well (i.e., slope of 20 and
%yintercept = 0). So plot the
%(x,y) data and the yhat line
%on the same plot to check the fit.
%We will plot the data points as
%blue squares and the y_hat
%estimate line in red.
y_hat = 20*x;
figure(1), clf
plot(x,y,'s',x,y_hat,'r')
title('Linear Estimate')
xlabel('Time (s)')
ylabel('Temp (deg F)')
grid, axis([1, 6, 2, 120])
legend('measured','estimated',4)
0
2
4
6
0
20
40
60
80
100
120
Linear Estimate
Time (s)
Temp (deg F)
measured
estimated
Matlab code
Command window display
©2009 by L. Lagerstrom
Checking the Fit: the MSE
%To see how well the y_hat line fits
%the data, calculate the mean
%squared error by:
%1. Calculating the vertical
%distance from each data point to
%the line (this is the "error").
%2. Squaring each vertical distance
%(this is the "squared error").
%3. Summing the squarederror values
%and calculating the mean (this is
%the "mean squared error," or MSE).
%Redisplay (x,y) data and y_hat line
%values for reference
x = 0:5, y = [0 20 60 68 77 110]
y_hat = 20*x
%Calculate the errors and MSE
err = y_hat  y
MSE = mean(err.^2)
x =
0
1
2
3
4
5
y =
0
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '10
 Lagerstrom
 Mean squared error, L. Lagerstrom

Click to edit the document details