The Museum of HP Calculators

# Polynomial Solutions for the HP-67

This program is Copyright © 1976 by Hewlett-Packard and is used here by permission. This program was originally published in the HP-67 Math Pac 1.

This program is supplied without representation or warranty of any kind. Hewlett-Packard Company and The Museum of HP Calculators therefore assume no responsibility and shall have no liability, consequential or otherwise, of any kind arising from the use of this program material or any part thereof.

 Polynomial Solutions Shift ak STO K(0-4) Label 5th 4th 3rd 2nd Key A B C D E

## Overview

This program will solve polynomial equations with real coefficients of degree 5 and below, provided the high-order coefficient is 1. The equation may be represented as

```    xn + an-1xn-1 +...+ a1x + a0 = 0  ,  n=2, 3, 4, or 5.
```

If the leading coefficient is not 1, it should be made 1 by dividing the entire equation by that coefficient.

The user must store the coefficients of the equation beforehand, beginning with a0 in R0 through an-1, in Rn-1. Zero must be input for those coefficients which are zero. It is not necessary to store the leading coefficient as 1, or any ak where k > n.

After the coefficients have been stored, the user-definable key (A through D) which represents the order of the polynomial should be pressed. All roots of the equation, real and complex, will then be computed. For example, if coefficients a0, a1, a2, and a3 have been stored in registers R0 through R3, then key B should be pressed to compute the four roots of the fourth degree polynomial equation

```    x4 + a3x3 + a2x2 + a1x + a0 = 0.
```

## Equations

The routines for third and fifth degree equations use an iterative routine under LBL a to find one real root of the equation. This routine requires that the constant term a0 not be zero for these equations. (If a0 = 0, then zero is a real root and by factoring out x, the equation may be reduced by one order.) After one root is found, synthetic division is performed to reduce the original equation to a second or fourth degree equation.

To solve a fourth degree equation, it is first necessary to solve the cubic equation

```    y3 + b2y2 + b1y + b0 = 0
```
```where b2 = -a2
b1 = a3a1 - 4a0
b0 = a0(4a2 - a32) - a12.
```

Let y0 be the largest real root of the above cubic.

Then the fourth degree equation is reduced to two quadratic equations:

```x2 + (A + C)x + (B + D) = 0
x2 + (A - C)x + (B - D) = 0
a3       y0
where A = --,  B = --
2        2

D = sqrt (B2 - a0)

(AB - a1/2) / D      if D != 0
C =   or
sqrt(A2 - a2 + y0)   if D = 0
```

Roots of the fourth degree equation are found by solving the two quadratic equations.

A quadratic equation x2 + a1x + a0 is solved by the formula x1,2 =

```  - a1/2 +/- sqrt(a12/4 - a0)
```

If D = (a12)/4 - a0 > 0, the roots are real; if D < 0, the roots are complex being

``` u +/- iv = - a1/2 +/- sqrt(-D)i
```

A real root is output as a single number. Complex roots always occur in pairs of the form u +/- iv. They are output by loading the stack with u, v, u, and -v in registers T, Z, Y, and X, respectively, and then executing the command Print Stack. If these roots are being output through a Pause (HP-67) rather than a Print (HP-97), some attention may be required to make sure that no roots go unnoticed.

## Remarks

1. Long execution times (~1-2 minutes) may be expected for equations of degree 3, 4, or 5, as these use an iterative routine once or more.
2. There is one condition in the solution of fourth or fifth degree polynomials that can cause the program to halt displaying Error. It is a very rare condition and you may never encounter it. It will occur when b0 = a0(4a2-a32)-al2 = 0 in the solution of the cubic to find y0. If the calculator halts at line 161 displaying Error, then b0 has been found to be zero and the following key sequence should be performed to recover from the error: 0 STO 7 RCL 1 STO 0 RCL 2 STO 1 D. After execution of D, press GTO . 044 R/S. The program will now continue to execute normally.

## Instructions

 Step Instructions Input Data/Units Keys Output Data/Units 1 Load side 1 and side 2. 2 Input coefficients below order of polynomial (i.e., for degree n, input through an-1). Coefficients = 0 must be so input. a0 STO 0 a1 STO 1 a2 STO 2 a3 STO 3 a4 STO 4 3 Compute roots to polynomial of degree 5 A Roots 1-5 4 B Roots 1-4 3 C Roots 1-3 2 D Roots 1-2 4 A single number will be output for a real root; complex pairs of roots (u +/- iv) will output as shown: u v u -v 5 For a new equation, return to step 2.

## Examples

Solve x5 - x4 - 101x3 + 101x2 + 100x - 100 = 0.

```Keystrokes                     Outputs
100 CHS STO 0 100 STO 1
101 STO 2 101 CHS STO 3
1 CHS STO 4 A                  10.00 *** (root 1)
1.00 *** (root 2)
1.00 *** (root 3)
-1.00 *** (root 4)
-10.00 *** (root 5)
```

Solve 4x4 - 8x3 - 13x2 - 10x + 22 = 0. Rewrite the equation as

x4 - 2x3 - (13/4)x2 - (10/4)x + 22/4 = 0

```Keystrokes                     Outputs
22 ENTER 4 ÷ STO 0 10 ENTER
4 ÷ CHS STO 1 13 ENTER 4 ÷
CHS STO 2 2 CHS STO 3 B         -1.00     (Roots 1 & 2 are
1.00     -1.00 +/- 1.00i)
-1.00
-1.00
3.12 *** (root 3)
0.88 *** (root 4)
```

Solve x3 - 4x2 + 8x - 8 = 0.

```Keystrokes                     Outputs
8 CHS STO 0 8 STO 1
4 CHS STO 2 C                   2.00 *** (root 1)
1.00     (roots 2 & 3 are
1.73      1.00 +/- 1.73i)
1.00
-1.73
```

Solve 2x2 + 5x + 3 = 0.

Rewrite the equation as x2 + 2.5x + 1.5= 0.

```Keystrokes                     Outputs
1.5 STO 0 2.5 1 D              -1.00 *** (root 1)
-1.50 *** (root 2)
```

## The Program

```LINE  KEYS
001  *LBL A     5th degree
002   4
003   STO E
004   STO I
005   GSB a     Find one real root.
006   RCL 7
007   PRTX
008   RCL 4
009   1
010   GSB b     Synthetic division to find new
011   GSB b     ai, i=3,2,1,0.
012   GSB b
013   GSB b
014  *LBL B     4th degree (quartic).
015   RCL 2
016   STO C
017   CHS
018   STO 2
019   RCL 1     Compute coefficients
020   STO B     b2, b1, b0 for cubic equation
021   RCL 3     to find y0.
022   STO D
023   x
024   RCL 0
025   STO A
026   4
027   x
028   -
029   STO 1
030   RCL C
031   4
032   x
033   RCL D
034   X2
035   -
036   RCL A
037   x
038   RCL B
039   X2
040   -
041   STO 0     If b0 = 0, Error will occur.
042   CF 0
043   GSB C     Do no print roots.
044   F2?       Solve cubic.
045   GTO 0     If only one real root, branch.
046   RCL 7
047   RCL 3
048   X>Y?
049   STO 7     Else find largest real root from
050   RCL 7     among R3, R4, R7.
051   RCL 4
052   X>Y?
053   STO 7
054  *LBL 0     y0->R7.
055   RCL D
056   STO 6
057   2
058   STO ÷ 6   A
059   STO ÷ 7   B
060   RCL 7
061   X2
062   RCL A
063   -
064   sqrt      D
065   STO 9
066   X=0?
067   GTO 0
068   RCL 6
069   RCL 7
070   x         Compute C for D != 0
071   RCL B
072   2
073   ÷
074   -
075   RCL 9
076   ÷
077   GTO 1
078  *LBL 0
079   RCL 6
080   X2        Compute C if D = 0
081   RCL C
082   -
083   RCL 7
084   2
085   x
086   +
087   sqrt
088  *LBL 1
089   STO 8     C
090   SF 0      Print roots.
091   GSB 7
092  *LBL 7
093   RCL 6     First time through, set
094   RCL 8     a1 = A-C and a0 = B-D.
095   CHS
096   STO 8
097   +
098   STO 1
099   RCL 7     Second time through, set
100   RCL 9     a1 = a+C and a0 = B+D
101   CHS
102   STO 9
103   +
104   STO 0
105   GSB D
106   RTN       Solve Quadratic X2 + a1x + a0 = 0.
107  *LBL C
108   2
109   STO E     3rd degree (cubic).
110   STO I
111   GSB a     Find one real root.
112   RCL 7
113   F0?
114   PRTX
115   RCL 2
116   1         Synthetic division to find new a1, a0.
117   GSB b
118   GSB b
119  *LBL D     2nd degree (quadratic).
120   RCL 1
121   CHS
122   2         -a1/2
123   ÷
124   ENTER
125   ENTER
126   X2        (a1^2)/4
127   RCL 0
128   -         D = (a1^2)/4 - a0
129   X<0?      If D<0, complex roots.
130   GTO 0
131   sqrt      If D>=0, roots are real.
132   +
133   STO 3     Root 1 = (-a1/2) + sqrt(D)
134   F0?
135   PRTX
136   roll up   -a1/2
137   LST X
138   -         sqrt(D)
139   STO 4
140   F0?       Root 2 = (-a1/2) - sqrt(D)
141   PRTX
142   CF 2      F2 clear for real roots.
143   RTN
144  *LBL 0
145   CHS       D<0, roots are complex.
146   sqrt
147   STO 5     V = sqrt(-D)
148   roll up
149   RCL 5     u =-a1/2
150   CHS
151   F0?       Stack contains -v, u, v, u.
152   PRT STK
153   SF 2
154   RTN       F2 set for complex roots.
155  *LBL a
156   0         Find one real root for 3rd or 5th degree poly.
157   STO 7
158   RCL 0
159   ENTER
160   ABS       Root will be in R7.
161   ÷
162   STO 8
163   LST X     a0/|a0| = +/- 1
164   RCL (i)   |a0|
165   ABS
166   +         |a2| or |a4| (k)
167   LOG       Let E = |a0| + k
168   INT
169   1
170   +         Delta X will be larger of the pair (1, 10^E)
171   10X
172   1         R6<-delta X
173   X<=Y?
174   X<>Y
175   STO 6
176  *LBL 9
177   1
178   0
179   STO ÷ 6   R6<-R6/10
180   RCL 8
181   CHS
182   STO 8     R8<- - R8
183  *LBL 8
184   RCL 7
185   RCL 7
186   RCL 6
187   RCL 8
188   x
189   +
190   STO 7     R7<-R7 + R6 R8
191   X=Y?
192   RTN       If no change, done.
193   ENTER
194   ENTER
195   GSB c
196   X=0?      Evaluate f(R7).
197   RTN       If f(R7) = 0, R7 is a root; done.
198   RCL 8
199   x
200   X<0?
201   GTO 8     Else loop again.
202   GTO 9
203  *LBL c
204   RCL (i)   Evaluate the polynomial.
205   +         E.g., for cubic, I=2
206   x         f(x) = ((x+a2)x + a1)x + a0
207   DSZ I
208   GTO c
209   RCL 0
210   +
211   RCL E
212   STO I     Restore I before exiting.
213   roll dn
214   RTN
215  *LBL b
216   DSZ I     Synthetic division.
217  *LBL 0     E.g. for degree 5, let ci be coeffs. of new poly.
218   RCL 7     of degree 4; c3=R7+a4; c2=c3 R7 + a3; c1 = c2 R7 + a2
219   x         C0=c1 R7 + a1.
220   +
221   RCL (i)
222   X<>Y
223   STO (i)
224   RTN
```

## Register Use

```R0  a0, b0
R1  a1, b1
R2  a2, b2
R3  a3, Root
R4  a4, Root
R5  sqrt(-D)
R6  A, delta X
R7  B, Root
R8  C +/- 1
R9  D
A   a0
B   a1
C   a2
D   a3
E   2 or 4
I   Counter
```