(42S) Short Quadratic Solver
|
04-24-2014, 12:32 AM
(This post was last modified: 05-17-2017 12:37 PM by Gerson W. Barbosa.)
Post: #1
|
|||
|
|||
(42S) Short Quadratic Solver
Code: 00 { 25-Byte Prgm } Examples: 1) Given x² - 5x + 6 = 0, compute 123456*x₁ and 123456*x₂ .................... 123456 ENTER 1 ENTER 5 +/- ENTER 6 XEQ Q -> Y: 3 ; x₂ = 3 ................................................................ X: 2 ; x₁ = 2 ........................................................ Rv * -> X: 370,368 ; 123456*x₂ ........................................................ Rv * -> X: 246,912 ; 123456*x₁ 2) 3x² - 12x + 87 = 0 ............................... 3 ENTER 12 +/- ENTER 87 XEQ Q -> Y: 2 .i5 ; x₂ = 2 + 5i ................................................................ X: 2 -i5 ; x₁ = 2 - 5i 3) x² - (4 + 6i)x + (-5 + 10i) = 0 .. 1 ENTER 4 ENTER 6 COMPLEX +/- 5 +/- ENTER 10 COMPLEX XEQ Q -> Y: 3 i4 ; x₂ = 3 + 4i ................................................................ X: 1 i2 ; x₁ = 1 + 2i Edited to change code. Now it is one step and one byte shorter. This is the older code: Code: 00 { 26-Byte Prgm } Edited again to change subject line. |
|||
05-25-2014, 03:30 PM
(This post was last modified: 05-26-2014 11:47 AM by Jeff_Kearns.)
Post: #2
|
|||
|
|||
RE: Short Quadratic Solver (HP-42S)
It was interesting to read the evolution of this routine in your 2010 thread (see Gerson's link below in post #3).
Jeff K - edited to address an invalid link |
|||
05-25-2014, 09:55 PM
(This post was last modified: 05-25-2014 09:56 PM by Gerson W. Barbosa.)
Post: #3
|
|||
|
|||
RE: Short Quadratic Solver (HP-42S)
(05-25-2014 03:30 PM)Jeff_Kearns Wrote: It was interesting to read the evolution of this routine in your 2010 thread here. Jeff, That's a Google Drive link and I cannot follow it. I think you mean this old forum thread: Short Quadratic Solver (HP-42S) The influence and inspiration that led me to this even shorter version are described in this thread from 2012: Fast Quadratic Formula for the HP-41C Thank you for your interest! Gerson. |
|||
05-27-2014, 05:30 PM
(This post was last modified: 05-28-2014 10:21 PM by Jeff_Kearns.)
Post: #4
|
|||
|
|||
RE: Short Quadratic Solver (HP-42S) --- and 32Sii??
(05-25-2014 09:55 PM)Gerson W. Barbosa Wrote: Thank you for your interest! Gerson, I am interested! The following routine for the HP-32sii and HP-33s (slightly modified from Eddie Shore's original to display the roots as x + iy) is the one I use but it is 39 steps long! Q0001 LBL Q ' Set label Q Q0002 INPUT A Q0003 INPUT B Q0004 INPUT C Q0005 SQ(B)-4xAxC ' Equation (enter by RS [right shift] EQN) Q0006 STO D Q0007 RCL D ' Is the discriminant negative? Q0008 x<0? Q0009 SF 1 ' Flag 1 - indicate that the roots are complex Q0010 RCL D Q0011 FS? 1 Q0012 +/- ' The HP 33 cannot take square roots of x<0 Q0013 square root Q0014 STO E ' E = abs(sqrt(D)) Q0015 -B/(2xA) ' enter this as an equation Q0016 STO F Q0017 E/(2xA) ' enter this an an equation Q0018 STO G Q0019 RCL G Q0020 0 Q0021 FS? 1 Q0022 x<>y Q0023 RCL F Q0024 0 Q0025 CMPLX+ Q0026 x<>y Q0027 STOP ' press the R/S key: display first root Q0028 RCL G Q0029 +/- Q0030 0 Q0031 FS? 1 Q0032 x<>y Q0033 RCL F Q0034 0 Q0035 CMPLX+ Q0036 x<>y Q0037 STOP Q0038 CF 1 ' clean up command Q0039 RTN Can you think of a way of dramatically shortening it, in line with your 15-liner for the HP-42s, so that it still gives real and complex roots? The memory in the HP-32Sii is so limited and shortening this program might allow me to squeeze one more program in there... Thanks, Jeff K |
|||
05-27-2014, 06:57 PM
Post: #5
|
|||
|
|||
RE: Short Quadratic Solver (HP-42S)
(05-27-2014 05:30 PM)Jeff_Kearns Wrote: Can you think of a way of dramatically shortening it, (...) so that it still gives real and complex roots? Copied most of it from here: Short quadratic solver (HP-15C) Code: Q01 LBL Q Doesn't give you the complex solution but that should be easy to add. There are probably shorter solutions possible by using a register. Cheers Thomas |
|||
05-28-2014, 04:18 AM
Post: #6
|
|||
|
|||
RE: Short Quadratic Solver (HP-42S)
(05-27-2014 06:57 PM)Thomas Klemm Wrote:(05-27-2014 05:30 PM)Jeff_Kearns Wrote: Can you think of a way of dramatically shortening it, (...) so that it still gives real and complex roots? Nice use of CMPLX- for saving a step or two! This might do while we don't think of something better: Code:
If the roots are complex, the flag 1 announciator will be lit and the real and complex parts will be in registers Y and X, respectively. Cheers, Gerson[/code] |
|||
05-28-2014, 10:17 PM
(This post was last modified: 05-28-2014 11:12 PM by Jeff_Kearns.)
Post: #7
|
|||
|
|||
RE: Short Quadratic Solver (HP-42S)
(05-28-2014 04:18 AM)Gerson W. Barbosa Wrote: If the roots are complex, the flag 1 annunciator will be lit and the real and complex parts will be in registers Y and X, respectively. Gerson - The code (without INPUT prompts) works OK for real roots but not for complex roots (IMHO)... I prefer to see the results (if complex) displayed as x + iy, as per the 39 step routine based on Eddie Shore's program to which I added a couple of x<>y statements to display 'correctly'. There are two issues with this particular routine that I can't quite figure out: 1) even when I insert a x<>y before the final RTN, I still get the imaginary part in x, i.e. ix + y. With or without a x<>y before the final RTN, the displayed result is the same. 2) Only one complex root is solved correctly. As an example, if you try solving 5x² + 2x + 1 = 0, you get the following: Flag 1 is set with the imaginary part 0.4 in x, and the real part -0.2 in y. Upon R/S the second root (x = -0.2 - 0.4i) is not shown. Granted, all complex roots are of the form x = D ± Ei, so one can deduce the second root and it doesn't really matter I guess BUT... Can you suggest a better solution? Regards, Jeff K |
|||
05-29-2014, 04:23 AM
(This post was last modified: 05-29-2014 03:53 PM by Gerson W. Barbosa.)
Post: #8
|
|||
|
|||
RE: Short Quadratic Solver (HP-42S)
(05-28-2014 10:17 PM)Jeff_Kearns Wrote:(05-28-2014 04:18 AM)Gerson W. Barbosa Wrote: If the roots are complex, the flag 1 annunciator will be lit and the real and complex parts will be in registers Y and X, respectively. Jeff, I hope this is better, although not so short anymore: Code:
Usage examples on the HP-32SII 1 ENTER 5 +/- ENTER 6 XEQ Q --> 2 x<>y --> 3 (Two real roots, because the flag 1 annunciator is off) 1 ENTER 1 +/- ENTER 1 XEQ Q --> 0.5 x<>y 8.66025404E-1 R↓ 0.5 x<>y -0.86602540378 (Two complex roots, 0.5 ± (√3)÷2 i, because the flag 1 annunciator is on) Alternatively, you can use a slightly modified version of Eddie Shore's program to save a few steps: Code:
Regards, Gerson. P.S.: Regarding issue #1, that happens because the first RTN instruction (step Q21), not the last, is executed when flag 1 is set. |
|||
05-29-2014, 05:10 AM
Post: #9
|
|||
|
|||
RE: Short Quadratic Solver (HP-42S) | |||
05-29-2014, 03:48 PM
Post: #10
|
|||
|
|||
RE: Short Quadratic Solver (HP-42S) | |||
05-30-2014, 03:56 AM
Post: #11
|
|||
|
|||
RE: Short Quadratic Solver (HP-42S) | |||
05-31-2014, 03:31 AM
Post: #12
|
|||
|
|||
RE: Short Quadratic Solver (HP-42S)
(05-30-2014 03:56 AM)Jeff_Kearns Wrote:(05-29-2014 03:48 PM)Gerson W. Barbosa Wrote: Thank you very much!And I thank you both! --- Jeff K You're most welcome! BTW, the HP-42S code (post #1) has been shortened to 14 steps and 25 bytes, including LBL and END. Perhaps a new world record for the HP-42S :-) (Not being the most accurate, at least that might be the shortest one ever - well, until someone manages to save yet another step or byte) Regards, Gerson. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 4 Guest(s)