The Museum of HP Calculators

FITTER for the HP 42s

This program was written by Jose M.Perez, Xose.

This program is supplied without representation or warranty of any kind. The author and The Museum of HP Calculators therefore assume no responsibility and shall have no liability, consequential or otherwise, of any kind arising from the use of this program material or any part thereof.

Overview

'Fitter' is the proper tool to FIT any data to any function in the way: y = a1 * f1(x) + a2 * f2(x) + a3 * f3(x) + .... The functions f1, f2, f3,... must be programmed in a very simple way: the index will be passed to the functions program through register 00, the program will then jump to the specified function that will operate over X (at register X) and Y (at register Y). A sample:

Imagine we are trying to fit some data to a function of the kind: y = a + b * sin(x) + c * cos(x), all we have to do is to write a program like this:

```LINE	KEYS
000     { 19-Byte Prgm }
001    LBL "FNC"       ;
002     GTO IND 00      ;

003     LBL 01          ; f1(x)
004     1               ;
005     RTN             ;

006     LBL 02          ; f2(x)
007     SIN             ;
008     RTN             ;

009     LBL 03          ; f3(x)
010     COS             ;
011     RTN             ;

012     END             ;
```

Notice the first function is index 1, not 0!!
Although this is the general template to use sometimes we can save a few bytes.
For instance, a program to fit data to any polinomy can be written this way:

```LINE	KEYS
000     { 14-Byte Prgm }
001    LBL "POLY"      ;
002     RCL 00          ;
003     1               ;
004     -               ;
005     Y^X             ;
006     RTN             ;
007     END             ;
```

Later, on the menu we will be able to specify the grade of the polinomy by setting the number of functions.

```  Sum+:  enters data pair (just like normal sum+)
Sum-:  deletes or corrects data.
#F:    enters number of funcions (X register), resets data.
FNC:   allows you to enter the name of the program with the functions definitions.
CLsum: deletes global variables from memory.
GO:    calculates coeficients.

(EXIT to EXIT ;)
```

Output:
X: [ ?x1 Matrix ], matrix with the calculated coeficients (a1 = 1:1, a2 = 2:1, ...)

Using the program

Once inside the program menu the first thing you have to do is indicate the name the program which contains the functions and the number of them (options 'FNC' and '#F'). Then begin to enter data pairs the same way as usual statistics (X value, Y value and 'sum+'), if you enter wrong data by mistake just press 'Rv' (the data pair remains in registers Y and Z) and then 'sum-'.
One important thing: exiting the program (by pressing 'EXIT' or 'GO') does not delete previous entered data. '#F' resets data and 'CLsum' purges data variables.

How to manipulate data before enter it with functions such as 'LN' or '1/X' which are on the top row?
Since exiting the program does not clear data variables, just press 'EXIT', manipulate the data and re-enter 'FITTER', then press 'sum+' (do not re-inicialize with '#F'!)

How to reset data variables
Just re-enter the number of functions and press '#F'.

Sample problem

We've got the following data:

```      X  | Y
----+----
3  | 26
4  | 46
5  | 72
6  | 104
7  | 142
```
and we want to fit it to a second degree polynomy or parabola. First introduce the program 'POLY' we've already talked about. Then enter FITTER (XEQ FTTR).

Introduce the number of functions (3, they are f1(x)=1, f2(x)=x and f3(x)=x^2) on the stack and press '#F'. Then press 'FNC' and enter the name of the program (POLY)

```     3 #F FNC NOPQ P NOPQ O JKLM L WXYZ Y R/S
```
Then begin to enter data pairs (there's an about 6 seconds delay after each entry):
```     3 ENTER 26 SUM+
4 ENTER 46 SUM+
5 ENTER 62 SUM+
Rv SUM-
5 ENTER 72 SUM+
6 ENTER 104 SUM+
7 ENTER 142 SUM+
```
As you can see, I made a mistake when entering the third pair, so I deleted it and re-entered the correct data.
Then press 'GO' to calculate the coeficients. The program will exit leaving a 3x1 matrix on the stack. To see the answers edit the matrix.
```     GO SHIFT MATRIX EDIT
(element 1:1 is 2)
-> (element 2:1 is -1)
-> (element 3:1 is 3)
EXIT EXIT
```
So the parabola is y = 2 - X + 3 * X^2, which fits exactly (it does not have to fit exactly in general, the program will give the best aproximation, but in this case the data pairs were calculated from the parabola itself so the answer is exact).
To add, modify or delete any data just re-enter the program. To purge the variables re-enter the program and press 'CLsum':
```     XEQ FTTR CLSUM EXIT
```
As I have already told there's a short delay after each entry (about 6 seconds for the sample problem). The delay depends mainly on the number of functions and their complexity. Of course you can accelerate the calculator in fast-mode (but pressing 'EXIT' will return it to its normal state...)

The Program

```LINE    KEYS
000  { 316-Byte Prgm }
001    LBL "FTTR"      ;
003     "sum+"          ;
004     KEY 1 XEQ 22    ;
005     "sum-"          ;
006     KEY 2 XEQ 21    ;
007     "#F"            ;
008     KEY 3 XEQ 23    ;
009     "FNC"           ;
010     KEY 4 XEQ 24    ;
011     "CLsum"         ;
012     KEY 5 XEQ 25    ;
013     "GO"            ;
014     KEY 6 GTO 10    ;
015     KEY 9 GTO 11    ;
017     LBL 01          ;
018     STOP            ;
019     GTO 01          ;

020    LBL 10          ; GO
021     RCL "c"         ;
022     RCL "a"         ;
023     /               ;

024    LBL 11          ; EXIT
026     EXITALL         ;
027     RTN             ;

028    LBL 21          ; sum-
029     RCL "n"         ;
030     X=0?            ;
031     GTO 99          ;
032     SF 81           ;

033    LBL 22          ; sum+
034     FS? 81          ;
035     Rv              ;
036     STO 10          ; stores data
037     X<>Y            ;
038     STO 09          ;
039     X<>Y            ;
040     ENTER           ;

041     SF 25           ; generates temporary matrix "b" and inizilizes row counter
042     RCL "f"         ;
043     FS?C 25         ;
044     X=0?            ;
045     GTO 99          ;
046     ENTER           ;
047     STO 00          ;
048     NEWMAT          ;
049     1               ;
050     +               ;
051     STO "b"         ;

052     LBL 02          ;
053     RCL 09          ;
054     RCL 10          ;
055     RCL 09          ;
056     SF 25           ;
057     XEQ IND "fx"    ; calculates fn(X)
058     FC?C 25         ;
059     GTO 99          ;
060     RCL 00          ;
061     XEQ 30          ; multiplies actual row and column of "b" by fn(X)
062     XEQ 30          ;
063     INDEX "c"       ; adds fn(X)*Y to actual row of "c"
064     1               ;
065     STOIJ           ;
066     Rv              ;
067     RCLEL           ;
068     RCL 10          ;
069     RCL* ST T       ;
070     FS? 81          ; substracts if 'sum-'
071     +/-             ;
072     +               ;
073     STOEL           ;
074     1               ;
075     STO- 00         ;
076     RCL 00          ;
077     X!=0?           ;
078     GTO 02          ;

079     Rv              ; adds or substracts "b" to "a"
080     RCL "b"         ;
081     FC?C 81         ;
082     GTO 03          ;
083     +/-             ;
084     X<>Y            ;
085     +/-             ;
086     X<>Y            ;
087     LBL 03          ;
088     STO+ "a"        ;
089     X<>Y            ;
090     STO+ "n"        ;

091     CLV "b"         ; restores stack
092     RCL 09          ;
093     RCL 10          ;
094     RCL "n"         ; shows actual data counter
095     RTN             ;

096    LBL 23          ; #F
097     IP              ;
098     ENTER           ;
099     X<=0?           ;
100     GTO 99          ;
101     STO "f"         ; stores new "f"
102     ENTER           ;
103     1               ;
104     NEWMAT          ;
105     STO "c"         ; resets "c"
106     Rv              ;
107     ENTER           ;
108     NEWMAT          ;
109     STO "a"         ; resets "a"
110     0               ;
111     STO "n"         ; resets data counter
112     RTN             ;

113    LBL 24          ; FNC
114     CLA             ;
115     SF 25           ;
116     RCL "fx"        ;
117     CF 25           ;
118     STR?            ;
119     ARCL ST X       ;
120     AON             ;
121     STOP            ;
122     AOFF            ;
123     ASTO "fx"       ;
124     Rv              ;
125     RTN             ;

126    LBL 25          ; CLsum
127     CLV "c"         ;
128     CLV "a"         ;
129     CLV "n"         ;
130     CLV "fx"        ;
131     CTV "f"         ;
132     RTN             ;

133    LBL 30          ; multiplies "b" matrix row (X) by a given number (Y)
134     INDEX "b"       ;
135     1               ;
136     STOIJ           ;
137     Rv              ;
138     LBL 04          ;
139     RCLEL           ;
140     RCL* ST Z       ;
141     STOEL           ;
142     J+              ;
143     Rv              ;
144     FC? 76          ;
145     GTO 04          ;
146     RCL "b"         ; 'rotates' the matrix to do the same with the columns
147     TRANS           ;
148     STO "b"         ;
149     Rv              ;
150     RTN             ;

151    LBL 99          ; error rutine
152     Rv              ;
153     TONE 0          ;
154     CF 81           ;
155     RTN             ;

156     END             ;
```

Variables

```a:       Matrix of Sum(fn(X)*fm(X))
b:       Temporary matrix of fn(X)*fm(X) products
c:       Matrix of Y*fn(X) products
fx:      Contains the name of the program with the functions definitions
f:       Number of functions in 'fx'
n:       Number of entered data pairs
00:      Actual function (use it in 'fx')
```

Flags

```25:      Desactivates errors (system flag)
76:      Indicates row jump on a matrix (system flag)
81:      Set if sum-, cleared if sum+ (user flag)
```