Post Reply 
Generating a Polynomial Given Its Roots
12-23-2018, 02:11 PM (This post was last modified: 12-23-2018 09:56 PM by Namir.)
Post: #4
RE: Generating a Polynomial Given Its Roots
Here is my version of the HP-41C program that can handle up to 19 roots.

The code basically implements repeatedly multiplying a polynomial with a simple 1st degree polynomial. The initial form of the polynomial is also a simple first degree polynomial. As you enter more roots, the degree of the polynomial increases.

Code:

Pseudo-Code
========
A(1) = 1
A(2) = -first root
Do
  B = input("Enter root?")
  IF B = 0 Then Exit Loop
  B = -B
  For I=2 to N+1
    C(I) = A(I) + B * A(I-1)
  Next I
  C(N+2) = A(N+1)*B
  N = N + 1
  For I = 1 To N + 1
    A(I) = C(I)
  Next I
Loop

Display array A(1) to A(N+1)

Memory Map
==========

R00 = I loop control variable
R01 = A(1)
R02 = A(2)
...
R20 = A(20)
R21 = C(1)
R22 = C(2)
...
R40 = C(20)
R41 = N
R42 = B
R43 = index I

HP-41C Listing
==============

01 LBL "POLY"        
02 LBL A        
03 1        
04 STO 41        
05 STO 01        
06 STO 21        
07 FIRST ROOT?        
08 PROMPT        
09 CHS        
10 STO 02        
11 STO 22        
12 LBL 00    # Main loop
13 ROOT? 0=END        
14 PROMPT        
15 X=0?        
16 GTO 01    # go to display results
17 CHS        
18 STO 42    # store minus root
19 XEQ 09        
20 ISG 00    # store value for loop conttrol variable
21 STO X    # a virtual NOP
22 LBL 02    # stert inner loop
23 RCL 00        
24 INT        
25 STO 43    # store index I
26 1        
27 -        
28 RCL IND X    # Get A(I-1)    
29 RCL 42        # Get B
30 *        
31 RCL IND 43    # Get A(I)    
32 +        
33 RCL 00        
34 INT        
35 20        
36 +        
37 X<>Y        
38 STO IND Y    # Store in C(I)    
39 ISG 00        
40 GTO 02    # End of loop
41 RCL 41        
42 1        
43 +        
44 RCL IND X        
45 RCL 42        
46 *        
47 RCL 41        
48 2        
49 +        
50 X<>Y        
51 STO IND Y        
52 XEQ 09        
53 LBL 03    # Loop for A(I) = C(I)
54 RCL 00        
55 INT        
56 STO 43        
57 20        
58 +        
59 RCL IND X        
60 STO IND 43        
61 ISG 00        
62 GTO 03        
63 1        
64 STO+ 41    # N = N + 1 increment polynomial order
65 GTO 00        
66 LBL 01        
67 XEQ 09        
68 LBL 04        
69 RCL 00        
70 INT        
71 RCL IND X        
72 R/S        
73 ISG 00        
74 GTO 04        
75 END        
76 PROMPT        
77 RTN        
78 LBL 09    # set up loop control variable
79 RCL 41        
80 1        
81 +        
82 1.00E+03        
83 /        
84 1        
85 +        
86 STO 00        
87 RTN

Run the program by executing XEQ "POLY" (you can instead press the key [A] when you are inside the program POLY):

1) The first prompt will ask you for the first root. Enter a root.
2) Subsequent prompts will ask you for additional roots OR to enter 0 when you are done.
3) The program displays the coefficients of the polynomial starting with the coefficient of the highest term. Press [R/S] to view the next coefficient of the sequentially lower term. When you have viewed all of the coefficients, the program displays END.

Inspect the value in register 41 to view the degree of the resulting polynomial.

Enjoy!
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Generating a Polynomial Given Its Roots - Namir - 12-23-2018 02:11 PM



User(s) browsing this thread: 1 Guest(s)