HP Forums
(15C) Nth Degree Polynomial Program - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (15C) Nth Degree Polynomial Program (/thread-14091.html)



(15C) Nth Degree Polynomial Program - 15-lennyphelan - 12-03-2019 01:42 AM

Hello all, this program calculates a y value for any degree polynomial (between 1 and 56 theoretically, or more with a memory expansion) given an x value and matrix A containing the coefficients of the expression. This is useful for using the SOLVE and ∫yx functions, and for using Linear regression to plot secant lines. The program uses Horner's Method, which is outlined on pg. 79 of the latest 15c manual. Unfortunately, this program completely obliterates the stack so don't put anything important in there!

If you're keying this program in manually; be mindful of the USER mode instructions. A handy trick for keying the program in at some location other than 000 is to add however many R/S instructions is necessary to make the line number a multiple of 10, making it much easier to compare line numbers and ensure accurate entry. Once finished (leave them in until after a test run) you may optionally execute GTO A in run mode followed by a BST in program mode to easily position the cursor on the extraneous instructions for removal.

Usage
  • Dimension matrix A such that the area is equal to the degree of the desired polynomial. The shape of the matrix doesn't matter. For instance, a 1x6 matrix works just the same as a 2x3 matrix. The program uses the area to automatically calculate the degree of the expression.
  • As an aside, the area of a matrix is obtained with RCL DIM [A-E], multiply.
  • Fill the matrix with the polynomial cooeficients, with the most significant term at (1,1), in decending order
  • key a value into X and run the program by calling label A. (remember you're probably already in user mode)

Code:

   001 {    42 21 11 } f LBL A
   002 {    42 16  1 } f MATRIX 1
   003 {    45 23 11 } RCL DIM A
   004 {          20 } ×
   005 {           2 } 2
   006 {    43 30  9 } g TEST x≥y
   007 {    22 48  8 } GTO .8
   008 {          30 } −
   009 {    44 48  9 } STO .9
   010 {    32 48  6 } GSB .6
   011 {    45 11  u } RCL A
   012 {          20 } ×
   013 { 42 21 48  9 } f LBL .9
   014 {    45 11  u } RCL A
   015 {          40 } +
   016 {          20 } ×
   017 { 42  5 48  9 } f DSE .9
   018 {    22 48  9 } GTO .9
   019 {          36 } ENTER
   020 {    45 11  u } RCL A
   021 {          40 } +
   022 {          40 } +
   023 {       43 32 } g RTN
   024 { 42 21 48  8 } f LBL .8
   025 {       43 35 } g CLx
   026 {           1 } 1
   027 {       43 10 } g x≤y
   028 {    22 48  7 } GTO .7
   029 {          33 } R⬇
   030 {    32 48  6 } GSB .6
   031 {    45 11  u } RCL A
   032 {          20 } ×
   033 {       45 11 } RCL A
   034 {          40 } +
   035 {       43 32 } g RTN
   036 { 42 21 48  7 } f LBL .7
   037 {          33 } R⬇
   038 {    32 48  6 } GSB .6
   039 {       45 11 } RCL A
   030 {       43 32 } g RTN
   031 { 42 21 48  6 } f LBL .6
   032 {          33 } R⬇
   033 {          36 } ENTER
   034 {          36 } ENTER
   035 {          36 } ENTER
   036 {       43 32 } g RTN

if you're using this 15c emulator and a DM15, you can simply copy this code as-is into a .15c file and upload it. Hope you enjoy!!


RE: 15C Nth Degree Polynomial Program - Gamo - 12-03-2019 06:08 AM

15-lennyphelan

Can you provide some example from this program?

Thank You

Gamo


RE: 15C Nth Degree Polynomial Program - 15-lennyphelan - 12-03-2019 09:51 PM

(12-03-2019 06:08 AM)Gamo Wrote:  15-lennyphelan

Can you provide some example from this program?

Thank You

Gamo

No problem.

Suppose I have the function f(x)=16x^5-4x^4+8x^2-4x-9 and I want to find ∫f(x)dx from 1 to 7

The coefficients of this polynomial in order of descending significance are 16, -4, 0, 8, -4, and -9 (notice there are six of them)

I'm going to put the calc in user mode, execute f MATRIX 1, and dimension matrix A to 1x6 with 1, ENTER, 6, f DIM A, then load the coefficients into the matrix by typing 16 STO A, 4 CHS STO A, ... so on. As you're storing the constant term, the calc should display [A 1,6] as you're holding the button.

Then I simply type 1, ENTER, 7, f ∫yx A. Sit back and relax for a minute.

Mathematica tells me the answer is 301045.2, and with exact precision, so does my 15c!


RE: 15C Nth Degree Polynomial Program - Thomas Klemm - 11-06-2022 02:15 PM

We don't have to use a separate counter.
The RCL user A skips the next command once the end of the matrix is reached.

Make sure to set user mode while entering the program:
Code:
001 - 42,21,11  LBL A           
002 - 42,16, 1  MATRIX 1        
003 -        0  0               
004 -    22  1  GTO 1           
005 - 42,21, 0  LBL 0           
006 -       40  +               
007 -       20  x               
008 - 42,21, 1  LBL 1           
009 u    45 11  RCL user A      
010 -    22  0  GTO 0           
011 -       40  +               
012 -    43 32  RTN

Both SOLVE and \(\int_y^x\) fill the stack before calling the function.
If the function is used otherwise add an ENTER command right after LBL A to fill the stack.



For those using Torsten's HP-15C simulator:
Code:
   000 {             }
   001 {    42 21 11 } f LBL A
   002 {    42 16  1 } f MATRIX 1
   003 {           0 } 0
   004 {       22  1 } GTO 1
   005 {    42 21  0 } f LBL 0
   006 {          40 } +
   007 {          20 } ×
   008 {    42 21  1 } f LBL 1
   009 {    45 11  u } RCL A
   010 {       22  0 } GTO 0
   011 {          40 } +
   012 {       43 32 } g RTN