Post Reply 
A quartic, cubic, quadratic equation solver
12-18-2023, 11:37 PM (This post was last modified: 12-19-2023 12:45 AM by MickM.)
Post: #1
A quartic, cubic, quadratic equation solver
I have a program that solves general quadratic, cubic and quartic equations. It uses exact formulas, not any iterative numerical algorithms. I wrote it back in the late 70's for my HP41C and recently found a printout of it in the bowels of my filing cabinet. It uses the standard 4-level stack, I imagine it'd probably break in calculators using more stack levels. The HP41C didn't have any native complex capability so this program uses a somewhat convoluted way to describe what's real and what's complex (see below). I currently have a HP42S that can do native complex mathematics - I suspect the program could be made to work in a much cleaner way on that platform. I might tackle that in my copious free time (sarcasm) but for now if this program is useful to anybody then you're welcome to it. It's highly likely that one or more people have already posted their own version of this kind of thing - apologies for the clutter if so...

Example 1 - A quadratic equation of the form aX^2 + bX + c = 0
A quadratic equation has either a pair of real roots or a complex conjugate pair.
6X^2 + 2X + 7 = 0
6 ENTER 2 ENTER 7 XEQ "QUAD"
In this case the result is complex so it displays "COMPLEX". You can clear that alpha text to reveal the X-register with the real part of the answer (-0.1667) and when you press R/S you get the imaginary part (1.0672).
In this case the roots are -0.1667 ± i1.0672 (results stored in registers 0 & 1)

Example 2 - A cubic equation of the form aX^3 + bX^2 + cX + d = 0
A cubic equation always has 1 real root and either a pair of real roots or a complex conjugate pair.
8X^3 - 6X^2 + 2X + 7 = 0
8 ENTER -6 ENTER 2 ENTER 7 XEQ "CUBIC"
The first result is always the real one so it returns that root (-0.6962) in the X-register. Now press R/S to get the next pair of roots (which can be either REAL or COMPLEX). In this case it's COMPLEX so "COMPLEX" is displayed. You can clear that alpha text to reveal the X-register with the real part of the answer (0.7231) and when you press R/S again you get the imaginary part (0.8567).
In this case the roots are -0.6962 and 0.7231 ± i0.8567 (results stored in registers 2, 0 & 1)

Example 3 - A quartic equation of the form X^4 + aX^3 + bX^2 + cX + d = 0 (The coefficient of x^4 must be 1)
A quartic equation has two pairs of roots and either pair can be real or a complex conjugate.
X^4 - 3X^3 + X - 20 = 0
-3 ENTER 0 ENTER 1 ENTER -20 XEQ "QRTC"
The first pair of results are REAL so "REAL" is displayed in the Alpha-register. You can clear that alpha text to reveal the X-register with 1st real root (3.4160) and when you press R/S you get the 2nd real root (-1.6681). Now press R/S again for the next pair and this time "COMPLEX" is displayed. You can clear that alpha text to reveal the X-register with the real part (0.6260) and when you press R/S again you get the imaginary part (1.7657).
In this case the roots are 3.4160, -1.6681, and 0.6260 ± i1.7657 (results stored in registers 2, 3, 0 & 1)

The END statement is line 200 of the program. Thanks to Valentin Albillo for the tip on putting the program into a scrollable window :-)

Code:
LBL "QRTC"
STO 05
RDN
STO 04
RDN
STO 02
RDN
STO 03
X^2
*
X<>Y
X^2
+
RCL 05
4
*
STO 05
R↑
*
-
RCL 03
RCL 04
*
RCL 05
-
X<>Y
1
SF 07
XEQ 01
RCL 02
X^2
RCL 05
FC?C 06
X<Y?
GTO 00
R↑
X^2
X>Y?
GTO 00
R↑
+
LBL 00
1
LASTX
CHS
RCL 05
4
/
SF 07
XEQ 02
STO 02
-
FS?C 06
CLX
1/X
RCL 03
RCL 02
*
RCL 04
+
*
ENTER
X<> 03
+
1
X<>Y
RCL 00
CHS
XEQ 02
-1
RCL 01
X<> 03
RCL 00
X<> 02
GTO 02
LBL "CUBIC"
R↑
ST/ T
ST/ Z
LBL 01
/
2
/
X<>Y
STO 00
R↑
STO 01
3
ST/ Z
/
STO 02
X^2
-
RCL 00
2
ST/ Y
CLX
LASTX
-
RCL 02
*
R↑
-
STO Z
X^2
X<>Y
3
Y^X
+
X<0?
GTO 04
SQRT
ST- Z
+
SIGN
LASTX
ABS
3
1/X
Y^X
*
X<>Y
SIGN
LASTX
ABS
3
1/X
Y^X
*
GTO 05
LBL 04
CHS
SQRT
X<>Y
R-P
3
ST/ Z
1/X
Y^X
P-R
ENTER
LBL 05
+
RCL 02
-
STO 02
1
X<>Y
RCL 01
+
ENTER
ENTER
RCL 02
*
RCL 00
+
FC? 07
SF 05
LBL "QUAD"
LBL 02
X<> Z
STO 01
ENTER
+
/
CHS
STO 00
ENTER
X^2
R↑
RCL 01
/
-
X<0?
SF 06
ABS
SQRT
FS? 06
GTO 03
+
STO 00
X<>Y
LASTX
-
LBL 03
STO 01
FS?C 07
RTN
RCL 02
FS?C 05
STOP
"REAL"
FS?C 06
"COMPLEX"
RCL 00
AVIEW
STOP
RCL 01
STOP
END
Find all posts by this user
Quote this message in a reply
12-19-2023, 12:17 AM
Post: #2
RE: A quartic, cubic, quadratic equation solver
(12-18-2023 11:37 PM)MickM Wrote:  Sorry for the length of the listing below - I couldn't figure out how to put it into a scrollable sub-window like several others have done.

Simply include the listing inside CODE tags:

1) Select the text of the listing
2) Press the Insert formatted text button ( [#], 7th button from the left or 3rd from the right)

Your listing will appear inside CODE .. /CODE tags and will be displayed in a scrollable sub-window, as you named it.

Quote:[...]
RCL 01
STOP
RTN
END

That's overkill, the final END will suffice, you don't need the RTN.

V.

  
All My Articles & other Materials here:  Valentin Albillo's HP Collection
 
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 




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