LeastSquares915.mws

LeastSquares915.mws
Thu Sep 15 08:34:32 MDT 2005

>    restart;

These "with" commands make items available from the Maple packages "plots" and "CurveFitting":

>    with(plots, display);   To make "display" available to this worksheet.

[display]

>    with(CurveFitting, LeastSquares); Ditto for "LeastSquares"

[LeastSquares]

Maple has recently overhauled/redesigned their Least-Squares.  The "with(stats)" environment described in chapter 3 is still available.  Here we look at the new ways Maple has come up with to do least-squares curve fitting.

We begin with a couple of famous list-processing functions, "map" and "zip":

Here are data cribbed from page 42 of our text.  The square brackets delimit Maple lists (as oppsed to Maple sets, delimited by { and }:

>    Ex2_xRow := [1,2,3,4,5,6,7];

Ex2_xRow := [1, 2, 3, 4, 5, 6, 7]

>    Ex2_yRow := [2.2, 3.7, 5.5, 5.9, 5.6, 4.2, 3.1];

Ex2_yRow := [2.2, 3.7, 5.5, 5.9, 5.6, 4.2, 3.1]

To make a scatter plot of these data, we need to match them up in points.  The Maple command "zip" does this as follows.

First we set up the function "makepoint" which takes an ordered pair of values and resturns a Maple list of two items.  Such a list is how Maple expresses points in the two-dimensional Cartesian plane.   We follow the

"makepoint" definition with a test invocation just to make sure "makepoint" is "there" and doing what we expect.

>    makepoint := (x,y) -> [x,y];

makepoint := proc (x, y) options operator, arrow; [x, y] end proc

>    makepoint(3,Pi);

[3, Pi]

Nopw we "zip" our "makepoint" function through the pair of lists.  The Maple "zip" command  feeds

"makepoint" its x-value from the first list, and the y-value from the second list.  In this case, "zip" makes us a list of Maple points.    You might want to check what happens if we change "makepoint" to add x and y....

>    PTS := zip( makepoint, Ex2_xRow, Ex2_yRow);

PTS := [[1, 2.2], [2, 3.7], [3, 5.5], [4, 5.9], [5, 5.6], [6, 4.2], [7, 3.1]]

Next we go for a scatterplot of our data points:

>    plot(PTS);

[Maple Plot]

We'd rather a plot of the discrete points, so we add on plot option "style=POINT", and then

plot option "symbolsize=20"  to make the data points bigger.

>    plot(PTS, style=POINT);

[Maple Plot]

>    plot(PTS, style=POINT, symbolsize=20);

[Maple Plot]

Later on, we'll want to superimpose plots, so we assign this scatter-plot structure to a Maple variable we have named "Scatter" (note the ":" at the end of this command).    We have to use the "display" command to see what "Scatter "looks like

>    Scatter := plot(PTS, style=POINT, symbolsize=20):

>    display(Scatter);

[Maple Plot]

Here we have Maple do us the least-squares LINE for our data.  We've told Maple to return its result as an expression in variable x.    This is DANGEROUS because x might already be in play.   Try putting an assignment statement "x := -13;" in ahead of the following -- watch the ensuing carnage.   We should probably use some other variable name...

>    LSQ_line := LeastSquares(PTS, x);

LSQ_line := 3.771428571+.135714285714286314*x

Now we plot the least-squares line and store its plot in the variable "PA":

>    PA := plot(LSQ_line, x):

Now we use the paste-together aspect of  "display" to show the least-squares line in relation to the data points:

>    display([Scatter, PA]);

[Maple Plot]

The above graph should disappoint us as to how well the least-squares line fits the data.  So we try for a least-squares parabola by applying the "curve=A*x^2+B*x+C" option:

>    LSQ_parabola := LeastSquares(PTS,x, curve=A*x^2+B*x+C);

LSQ_parabola := -.5714285714+3.03095238095238173*x-.361904761904762007*x^2

We don't have absolute freedom in what we can tell "curve" to do.   In the following command, we supply a "curve" which we know to be a parabola.  But parameter "h" will be squared, not "linear", so "LeastSquares" yarks at us, and refuses the job:

>    LeastSquares(PTS,x, curve=A*(x-h)^2+k);

Error, (in CurveFitting:-LeastSquares) curve to fit is not linear in the parameters

Here we plot the parabola and the data points as we did for the least-squares line.  The parabola fits better:

>    P_LSQ_parabola := plot(LSQ_parabola, x=1..7):

>    display([P_LSQ_parabola, Scatter]);

[Maple Plot]

We can interpolate in our table of values using our parabola.    Our x-values are all integers.   But, what if we needed an idea about a y-value for x = 2.4?

>    Ex2_xRow;

[1, 2, 3, 4, 5, 6, 7]

>    f := unapply(LSQ_parabola, x);

f := proc (x) options operator, arrow; -.5714285714+3.03095238095238173*x-.361904761904762007*x^2 end proc

>    Interpolated_Value := f(2.4);

Interpolated_Value := 4.618285714

>