ENG 06 – Lecture 10; 2/7/2013
Interpolation and Curve Fitting
Least squares regression: minimizes area between data and line you predict
polyfit: fits line to data
polyval: takes coefficients of polynomial (for example, from polyfit) and a vector of x
points to evaluate at that polynomial
Regression example: linear fit
o Torque needed to turn torsion spring of mousetrap through an angle is given in
data points
o Find constants for model given by T = k1 + k2x
>> xp=[0.698132, 0.959931, 1.134464, 1.570796, 1.919862];
>> yp=[0.188224, 0.209138, 0.230052, 0.250965, 0.313707];
>> coeffs = polyfit(xp,yp,1);
>> xfit=[0.6:0.01:2];yfit = polyval(coeffs,xfit);
>> plot(xp,yp,'O'); hold on; plot(xfit,yfit); hold off;
>> coeffs
coeffs = 0.0961 0.1177
Interpolation or Regression?
o For acceleration/velocity/distance problems, need to differentiate and integrate
o Best to fit a known function that can be “easily differentiated or integrated
o Use a spline fit
physics of gravity tells us that it should follow a smooth curve
Gives us just one function to work with
Fitting to More complicated functions
o Linearizing: making a few substitutions for variables which result in an equation
for a line
Linear vs. Nonlinear fitting
o Linear in terms of fitting constants:
Y = a + bt
Y = a + bt + ct^2
y = asin(t) + bcos(t)
y = asin(3t)
y = ae^-t
o Not linear in terms of all the fitting constants
y = asin(bt) + ccos(dt)
y = asing(bt)
y = ae^-bt
Linearizing Commonly Found Nonlinear functions
o Log functions:
y = Klog(x) + c
Looks like a straight line when you plot y values vs. log of x values
plot(log(x),y)
also semilogx(x,y)
creates a logarithmic x axis
o Exponential functions: y = Ce^Kx
Plot log of y values vs. x values to make it look like a straight line plot(x,log(y))
can also use semilogy(x,y)
creates an exponential y axis

More
Less