Polyfit v1.0: produce polynomial given N data points From: dfk@hp-lsd.cos.hp.com (David F. Kurth) Newsgroups: comp.sources.hp48 Date: 12 Aug 91 01:49:19 GMT I've always thought there should be a good use for a program like this, but I still haven't found it. It was a good challenge to program the algorithm, and maybe someone else can find a good use for it. POLYFIT produces the coefficients of an N-1 degree polynomial given N data points that lie on the curve of that polynomial. For example, consider the following table of data points: X Y ----------- -2 1 -1 -1 0 1 1 -1 2 1 There are 5 data points, and the Y value a very symmetric. POLYFIT produces the following polynomial: f(x) = 2/3*x^4 + 0*x^3 - 8/3*x^2 + 0*x^1 + 1*x^0 If you plot this with an X range between -2 and 2, you will see the curve "oscillate" between +1 and -1 as it hits all five data points. POLYFIT is not a least square's type curve fit program. There is no minimization of errors. The polynomial coefficients found by POLYFIT will exactly hit the given data points (to within the accuracy of the calculator). [Note: If you want a least-squares polyfit, see LEASTSQ on this disk. -jkh-] Using POLYFIT ------------- Enter the following code into your calculator. After loading, enter the new directory if not already there. Push the CST key load the custom menu which should reveal the following keys: POLY - Executes the main program which takes the input array of X-Y points from the stack and produces the K array of polymonial coefficients. VEIW - Displays the coefficients of the resulting polynomial. FX - Calculates f(x) using level 1 of the stack for x. X-Y - Recalls the input data points to the stack for editing or viewing. To begin, you must first enter the input data points array. This is easiest to do by using the Matrix Writer (Right-shift ENTER). Enter your first X-Y pair of points, then push the down arrow which informs the calculator that the array is only 2 elements wide. Continue entering the rest of your points until done. Then just push ENTER to return the array to the stack. Now push POLY to calculate the coefficients. The display should produce "DOING L:" which can be ignored (the program calculates a triangular array of difference values used for the rest of the computations). After this, the Ki coefficient values are calculated and briefly displayed. When done, the K values can be displayed again by using the VIEW key. Output values can be calculated by entering an X value on the stack and pressing the FX key. Plotting the equation can be done by pressing Left-shift PLOT, PLOTR, and then AUTO. For a small number of data points, the calculations are fairly quick. For each additional data point entered, calculation time roughly doubles. I've calculated 15 data points, but it took the calculator about 7.5 hours to finish. However, with sufficient memory and batteries, have at it. Dave Kurth dfk@hp-lsd.cos.hp.com