Next: E208 Least Squares Up: CERNLIB Previous: E106 Binary Search

## E201 Least Squares Polynomial Fit

Routine ID: E201
Author(s): K.S. KölbigLibrary: MATHLIB
Submitter: Submitted: 01.12.1994
Language: FortranRevised:

Subroutine subprograms RLSQPM and DLSQPM fit a polynomial

$p$m(x) = ∑j=0majxj

of degree m to n equally-weighted data points ($x$i,yi ). The calculated coefficients $a$j are such that

$S$m2 = ∑i=1n(yi-pm(xi))2 = min.

Subroutine subprograms RLSQP1 and DLSQP1 fit a straight line $p$1(x) = a0+a1x to n such points.

Subroutine subprograms RLSQP2 and DLSQP2 fit a parabola $p$2(x) = a0+a1x+a2x2 to n such points.

An estimate $s =S$m2/(n-m-1)

of the standard deviation $\sigma$ is calculated.

On CDC and Cray computers, the double-precision versions DLSQPM, DLSQP1 and DLSQP2 are not available.

Structure:

SUBROUTINE subprograms
User Entry Names: RLSQPM, RLSQP1, RLSQP2, DLSQPM, DLSQP1, DLSQP2
External References: RVSET, DVSET, DVSUM, DVMPY (F002), DSEQN (F012)

Usage:

For $t=R$ (type REAL), $t=D$ (type DOUBLE PRECISION),

```    CALL tLSQPM(N,X,Y,M,A,SD,IFAIL)
CALL tLSQP1(N,X,Y,A0,A1,SD,IFAIL)
CALL tLSQP2(N,X,Y,A0,A1,A2,SD,IFAIL)
```
N
( INTEGER) Number n of data points.
X
(type according to t) One-dimensional array of length $\ge N$ . On entry, X(i) contains the abscissas $x$i, (i=1,2,...,n) .
Y
(type according to t) One-dimensional array of length $\ge N$ . On entry, Y(i) contains the ordinates $y$i, (i=1,2,...,n) .
M
( INTEGER) Degree m of the polynomial to be fitted.
A
(type according to t) One-dimensional array of dimension (0:d), where $d \ge M$ . Contains, on exit, in A(j) the coefficients $a$j, (j = 0,1,...,m) .
A0,A1,A2
(type according to t) Contain, on exit, the coefficients $a$0 , $a$1 for $p$1(x)=a0+a1x or $a$0,a1,a2 for $p$2(x)=a0+a1x+a2x2 , respectively.
SD
(type according to t) Contains, on exit, the estimate s.
IFAIL
( INTEGER) Error flag.
$= 0:$ Normal case,
$= 1:$ $N \le 1$ or $M<0$ or $M \ge N$ or $M>20$ ,
$= -1:$ The matrix of normal equations is numerically singular.
In the case $IFAIL \ne 0$ : $M=0$ , $A\left(j\right)=0$ and $A0=A1=A2=0$ on exit.

Method:

The normal equations are solved. On computers other than CDC or Cray, double-precision mode arithmetic is used internally for RLSQPM, RLSQP1 and RLSQP2.

Notes:

Meaningful results can usually be obtained only for small values of m (typically $<10$ ).

References:

1. D.H. Menzel, Fundamental formulas of physics, v. 1, (Dover, New York 1960) 116--122.
$•$

Next: E208 Least Squares Up: CERNLIB Previous: E106 Binary Search

Janne Saarela
Mon Apr 3 15:06:23 METDST 1995