© 2001 by CRC Press LLC
Sample Application
MATLAB APPLICATION
A m file in MATLAB called polyfit.m can be applied to fit a set of given points
(X
i
,Y
i
) for i = 1 to N by a linear equation Y = C
1
X + C
2
based on the least-squares
criterion. The function polyfit has three arguments, the first and second arguments
are the X and Y coordinate arrays of the given points, and the third argument specifies
to what degree the fitted polynomial is required. For linear fit, the third argument
should be set equal to 1. The following shows how the results obtained for the sample
problem used in the FORTRAN and QuickBASIC program LeastSq1:
>> X = [1,2,3,5,8]; Y = [2,5,8,11,24]; A = polyfit(X,Y,1)
C = 3.0195 – 1.4740
If the third argument for the function polyfit is changed (from 1) to 2, 3, and
4, we also can obtain the least-squares fits of the five given points with a quadratic,
cubic, and quartic polynomials, respectively. When the third argument is set equal
to 4, we then have the case of exact curve-fit of five points by a fourth-order
polynomial. Readers are referred to the program ExactFit for more discussions.
Also, it is of interest to know whether one may select an arbitrary set of
functions and linearly combine them for least-squares fit, instead of the unbroken
set of polynomial terms X
0
, X
1
, X
2
, …, X
N
. Program LeastSqG to be presented
in the next section will discuss such generalized least-squares curve-fit. But before
we do that, let us first look into a situation where program LeastLQ1 can be
applied for a given set of data after some mathematical transformations are
employed to modify the data.
Transformed Least-Squares Curve-Fit
There are occasions when we know in advance that a given set of data supposed
to fall on a curve described by exponential equations of the type: