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" |
|||
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:[...] 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 |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)