FITTING 2 variable MODELS, POLYNOMIALS, Collocation

This note was prepared for use in an APL workspace. Read the text material for information. For full use, load the TAS APL workspace, go to APLie., and then enter: MMs'x10' and follow instructions.

A simple rate or linear model like a unit cost, commodity price, or rate of consumption is a straight line through the origin; so that when X is 0 then Y is 0. If an offset from the origin is added the model is still a straight line and a first degree polynomial. Higher degree polynomials can be curved lines.

"Polynomials are a fundemental tool in mathematics because most scalar functions of pratical interest can be approximated as closely as desired by polynomials, while at the same time they are relatively easy to analyze...

...polynomials are particularly simple functions of their coefficients. ..if C is a coefficient vector of a polynomial then it can be defined by:

    (Xø.*ý1+âûC)+.’C  ¦ •IO1"

   (or more simply by: XæC, where CèC used above)

 
[fm: Linear Functions; D.L.Orth; Teaching Mathematics via APL]; APL84, Helsinki, Finland.`

The "Vandermonde Array" produced by (Xø.*ý1+âûC) in effect is a vector of X raised to the 0, 1, 2,...n-1 powers where n is the number of terms in the coefficient vector C. Used in the form described by Orth the powers of X begin at 0 and increase.`

For the simple XæC definition the high exponent terms are on the left and the C vector must include a term for each power of X from highest to 0 on the right. Recall that X*0 is 1, so that the rightmost term is the constant.'

To evaluate a polynomial using the decode function the order of the coefficient vector must be reversed from Orth's definition. Since the decode function is most convenient it is essential to remember that the high exponent is on the left and the constant or coefficient of X*0 is on the right.`

Polynomials are fitted by: "theory, collocation, osculation, least squares, min.-max." [from: Numerical Analysis; F.Scheid; Schaumn's Outline Series; ref. 3]`

The collocation polynomial for equal or unequally spaced arguments can be expressed in determinant form. The solution by APL is similar to least squares. These two techniques are discussed below:`

A straight line through or offset from the origin is a first degree polynomial. If the X,Y coordinates of two points are known the collocation polynomial or straight line through them can be found as follows:

      C  Y ˜ Xø.*èý1+âûX
 i.e. C  Y ˜ Xø.*1,0
 The example below uses two points to demonstrate the technique:`

      X6 14  Y2 7    ¦ Data

      •CY˜Xø.*1,0     ¦ Fit straight line using Collocation

  0.625 ý1.75           ¦ 1st degree polynomial coefficients`

      6æC               ¦ Check Y for X=6

      (•Xø.+,0)æC       ¦ Evaluate for X6 14`

 
Note in using the above technique there is 1 more data points than the degree of the polynomial, i.e. if there are five points avaliable then the highest degree of polynomial that can be fitted is 4. The following eg. is from ref. 3 p57`

      X0 1 2 4 5  Y0 16 48 88 0  ¦ Data

      •C  Y ˜ Xø.*èý1+âûX         ¦ Collocation fit

 ý1 4 3 10 1.850025348Eý16          ¦ Note 5th coef. is approx 0`

 (Xø.+,0)æC ¦ Evaluation using decode for assigned X values

 1.850025348Eý16 16 48 88 2.222296075Eý13 ¦ Fit ok  `

      3æC  ¦ Test point given in example.

 84 ¦ ok, agrees with given answer.

  Note the use of (Xø.+,0) an idiom for making X
  the column vector required by æ.`

 
Note that the fit is not exact but is very close. A least squares fit will only be exact when the polynomial fits exactly and the precision of the calculations is sufficient. The fit is very good and as can be seen the errors at the two zero points are less than one part in 10*-12.`

The degree of the polynomial can be selected arbitrarily by selecting the shape of the Vandermonde array. Eg using the data above a second degree polynomial can be fitted to the 5 data points as follows:`

      •  C2  Y ˜ Xø.*2 1 0

 ý10.03896104 56.8961039 ý13.79220779`

      (Xø.+,0)æC2

 ý13.79220779 33.06493506 59.84415584 53.16883117 19.71428571`

 A plot of the original data points and the evaluated points from
the fitted polynomial reveals the quality of the fits.

Plot' polynomial fits' (Y,[1]((Xø.+,0)æC),[.1](Xø.+,0)æC2) ‘vs X

 The following data entry uses 'is'. This fn is used
 in the interactive functions such as NOTAX.

¦ enter Y values, X values and check vector length

 is 'Y X'  õ((•ûY)†•ûX)/'''Error: ûY must equal ûX'''`

 N1  is 'N' ¦ Degree of polynomial, default is 1.


 •AY˜Xø.*è0,âN ¦ [Enter] to Fit line, display coefficients.


 Plot'Y vs X data and line'  
(Y,[.1]Ym(Xø.+,0)æA) ‘vs X` 

 A brief review of APL statements 

 (Xø.+,0)æA ¦ Evaluate model at X 

 Y-(Xø.+,0)æA ¦ Model Y errors at X 

 +/(Y-(Xø.+,0)æA)*2 ¦ Sum of errors squared

 Plot'Model of Ym vs X'  Ym ‘vs X ¦ graph of Ym and X

 Ym((Xm•)ø.+,0)æA ¦ Eval. model at points Xm, calc. Ym

 (•ø.+,0)æA ¦ Evaluate model at entered points.`
 
Collocation polybomials are used for interpolation. If there are more than 8 or 9 points and/or the numbers are large the capacity of the machine will probably be exceeded. If an error occurs go back to the general polynomial fit proceedure and reduce the degree which is 1 less than the number of points for coolocation.`

The utility function FIT permits various degrees of polynomials and exponential functions to be fitted to paired data.

End to date, ams 990622