[comp.sys.handhelds] Curve fit programs for the HP28

d89-bfr@sm.luth.se (d89-bfr) (12/19/89)

I've noticed that my previous article doesn't look the way I wrote it, so
here's another try. I've also received a note on the HP28C compatibility.
The programs listed below are corrected to work on the 28C too.
 
 
Curve fitting programs.
 
How many times have you been annoyed by the fact that linear regression isn't
enough. These programs can handle polynomial, logarithmic and exponential
curves.
 
PFIT for functions like
 
Y = a0 + a1*x + a2*x^2 + a3*x^3 ...
 
If you have m samples and you think the function is of degree n then enter
 
[[ x1 y1]
 [ x2 y2]
 [ .  . ]
 [ xm ym]]
 
n
 
PFIT           The answer looks like.
 
[[ a0 ]
 [ a1 ]
 [ . ]
 [ an ]]
 
LFIT for functions like
 
Y = a*x^b
 
Again with m samples
 
[[x1 y1]
 [x2 y2]
 [ .  .]
 [xm ym]]
 
LFIT              The answer will be
 
[[ a ]
 [ b ]]
 
EFIT for functions like
 
Y = a*b^x
 
m samples gives
 
[[x1 y1]
 [x2 y2]
 [ . . ]
 [xm ym]]
 
EFIT             The answer is
 
[[ a ]
 [ b ]]
 
 
Time for examples. These are the samples required by measurment.
 
Y  4.0  17.0  62.0 157.0 569.0 320.0 1397.0 2012.0
X  1.0   2.0   3.0   4.0   6.0   5.0    8.0    9.0
 
I suspect that this is a polynomial function. Let's try.
 
Enter
[[1 5]
 [2 17]
 [3 62]
 [4 157]
 [6 569]
 [5 320]
 [8 1397]
 [9 2012]]
 
My guess is that it's a polynom of degree 4.
4 [PFIT]
 
[[5.00]
 [-2.00]
 [-2.00]
 [3.00]
 [3.88E-9]]
 
So. The furmula is, y=3*x^3-2*x^2-2*x+5.
 
Let's try another curve.
 
Y  0.5 0.177 0.096 0.063 0.045 0.034
 
X  1   2     3     4     5     6
 
ENTER
 
[[ 1 0.5   ]
 [ 2 0.177 ]
 [ 3 0.096 ]
 [ 4 0.063 ]
 [ 5 0.045 ]
 [ 6 0.034 ]]
 
[LFIT]
 
[[ 0.500]
 [ -1.498 ]]
 
The curve is y=0.5*x^-1.5
 
And the third and last example
 
Y    2.25 1.69 1.27 0.95 0.71 0.53
 
X    1    2    3    4    5    6
 
[[ 1 2.25 ]
 [ 2 1.69 ]
 [ 3 1.27 ]
 [ 4 0.95 ]
 [ 5 0.71 ]
 [ 6 0.53 ]]
 
[EFIT]
 
[[ 3.01 ]
 [ 0.75 ]]
 
the curve is y = 3*0.75^x
 
 
PFIT [2FEE]
<< SWAP TRN -> n s
  <<
[[ 0 1 ]]
s * TRN
[[ 1 0 ]]
s * -> b sa
    << 1 sa SIZE 2
GET
      FOR x 0 n
        FOR p sa 1 x 2
->LIST GET p ^
        NEXT
      NEXT sa SIZE 2
GET n 1 + 2 ->LIST
->ARRY DUP TRN -> a at
      << at b * at a
* /
      >>
    >>
  >>
>>
 
 
LFIT [615B]
<< -> a
  << a SIZE LIST->
DROP -> r c
    << 1 r
      FOR x 1 c
        FOR y a x y
2 ->LIST GET LN a
SWAP x y 2 ->LIST
SWAP PUT 'a' STO
        NEXT
      NEXT a 1 PFIT
DUP { 1 1 } GET EXP
{ 1 1 } SWAP PUT
    >>
  >>
>>
 
EFIT [A445]
<< -> a
  << a SIZE LIST->
DROP -> r c
    << 1 r
      FOR x a x 2 2
->LIST GET LN a SWAP
x 2 2 ->LIST SWAP PUT
'a' STO
      NEXT a 1 PFIT
DUP { 1 1 } GET EXP
{ 1 1 } SWAP PUT DUP
{ 2 1 } GET EXP { 2
1 } SWAP PUT
    >>
  >>
>>
 
No calculation of error yet. It might come in the future.
 
Have fun.
 
   _
/Bjorn.