%%HP: T(3)A(R)F(.); @ Short & Fast PolyFitter, by Joe Horn @ BYTES: 704.5 #2275h DIR P1 @ sample input #1 [[ -2 -3 ] [ 0 7 ] [ 1 6 ] [ 2 9 ]] P2 @ sample input #2 [[ -1 3 ] [ 0 2 ] [ 1 4 ] [ 2 0 ] [ 3 5 ]] PFIT @ a = matrix of points \<< DUP SIZE 1 GET \-> a s @ s = number of points \<< 1 s FOR j a { j 2 } GET @ make array of Y values NEXT s \->ARRY 1 s FOR j a { j 1 } GET s 1 - 0 @ make matrix of X powers FOR k DUP k ^ SWAP -1 STEP DROP NEXT { s s } \->ARRY SWAP DUP2 OVER / @ solve system of equations DUP 5 ROLLD RSD SWAP / + @ + RSD for higher accuracy \>> \>> SHOP @ Graphically Show Points & Polynomial Fit \<< STO\GS ERASE { # 0h # 0h } PVIEW @ SigmaDAT = point array MIN\GS V\-> MAX\GS V\-> @ Find mins & maxes ROT OVER - 1.24 * OVER + YRNG @ Adjust Y for menu area OVER - 1.03 * OVER + XRNG @ and set YRNG & XRNG 1 N\GS FOR j PICT \GSDAT DUP { j 1 } GET @ get next X and Y and turn SWAP { j 2 } GET R\->C @ into (X,Y) GROB 5 5 F1F070B011 REPL @ display arrow there NEXT RCL\GS PFIT @ Find polynomial fit OBJ\-> 0 SWAP OBJ\-> + 2 @ Create algebraic polynomial FOR j 'X' * j ROLL + -1 @ using Horner's Method STEP STEQ FUNCTION DRAW DRAX LABEL @ plot it with labeled axes { } PVIEW { \GSDAT PPAR EQ } PURGE @ freeze display & clean up \>> END