Article 4581 of comp.sys.handhelds:
Path: en.ecn.purdue.edu!noose.ecn.purdue.edu!samsung!uunet!tut.cis.ohio-state.edu!tortoise.cis.ohio-state.edu!adkins
From: adkins@tortoise.cis.ohio-state.edu (Brian Adkins)
Newsgroups: comp.sys.handhelds
Subject: Least Squares Approximation
Message-ID: <88889@tut.cis.ohio-state.edu>
Date: 26 Feb 91 18:59:10 GMT
Sender: news@tut.cis.ohio-state.edu
Reply-To: <adkins@tortoise.cis.ohio-state.edu>
Organization: Ohio State University Computer and Information Science
Lines: 52

The following is a program to perform a least squares fit on a set of 
data points.  The input is as follows:

level 3: a vector of x values ie.  [1 4 5 8 10 ...]
level 2: a vector of corresponding y values ie.  [3 4 5 7 6 ...]
level 3: a list of names of functions ie. {F1 F2 F3 ...}

The results are a user-defined function that approximates the data in 
level 2, and an error coefficient in level 1.  With the right choice of 
functions the program will do linear regression (I know it's already built
in!) or interpolating polynomials etc.  This is my first "real" program so
I'd rather you Email me for improvements etc. instead of blasting it on the
net.  Thanks.

Oh by the way, I typed this instead of uploading it so replace SQRT with
the square root key and THETA with alpha-right-shift F.

<<
  4 5 FOR I
    DUP SIZE EVAL I ROLLD I ROLLD
  NEXT
  5 ROLLD 0 0 0 0 0 -> X Y N F M THETA B A C PHI 
  <<
    1 M FOR I
      1 N FOR J
        'X(J)' EVAL 'F(I)' EVAL ->NUM
      NEXT
      N ->ARRY
    NEXT
    M ->LIST 'THETA' STO 
    1 M FOR I
      Y 'THETA(I)' EVAL DOT
    NEXT
    M ->ARRY 'B' STO 
    1 M FOR I
      1 M FOR J
        'THETA(J)' EVAL 'THETA(I)' EVAL DOT
      NEXT
    NEXT
    M DUP 2 ->LIST ->ARRY 'PHI' STO B PHI / 'C' STO
    'C(1)' EVAL { T } 'F(1)' EVAL APPLY EVAL *
    2 M FOR I
      'C(I)' EVAL { T } 'F(I)' EVAL APPLY EVAL * +
    NEXT
    ->STR 1 "=" REPL "'A(T)" SWAP + OBJ-> DEFINE
    'A(X(1))-Y(1)' ->NUM SQ
    2 N FOR I
      'A(X(I))-Y(I)' ->NUM SQ +
    NEXT
    N / SQRT 'A' RCL SWAP
  >>
>>


