(42S) Short Quadratic Solver - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (/forum-10.html) +--- Forum: General Software Library (/forum-13.html) +--- Thread: (42S) Short Quadratic Solver (/thread-1156.html) |
(42S) Short Quadratic Solver - Gerson W. Barbosa - 04-24-2014 12:32 AM 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. RE: Short Quadratic Solver (HP-42S) - Jeff_Kearns - 05-25-2014 03:30 PM 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 RE: Short Quadratic Solver (HP-42S) - Gerson W. Barbosa - 05-25-2014 09:55 PM (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. RE: Short Quadratic Solver (HP-42S) --- and 32Sii?? - Jeff_Kearns - 05-27-2014 05:30 PM (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 RE: Short Quadratic Solver (HP-42S) - Thomas Klemm - 05-27-2014 06:57 PM (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 RE: Short Quadratic Solver (HP-42S) - Gerson W. Barbosa - 05-28-2014 04:18 AM (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] RE: Short Quadratic Solver (HP-42S) - Jeff_Kearns - 05-28-2014 10:17 PM (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 RE: Short Quadratic Solver (HP-42S) - Gerson W. Barbosa - 05-29-2014 04:23 AM (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. RE: Short Quadratic Solver (HP-42S) - Thomas Klemm - 05-29-2014 05:10 AM (05-29-2014 04:23 AM)Gerson W. Barbosa Wrote: You can remove this step as D is never used. Cheers Thomas RE: Short Quadratic Solver (HP-42S) - Gerson W. Barbosa - 05-29-2014 03:48 PM (05-29-2014 05:10 AM)Thomas Klemm Wrote:(05-29-2014 04:23 AM)Gerson W. Barbosa Wrote: Duely removed above. Thank you very much! Gerson. RE: Short Quadratic Solver (HP-42S) - Jeff_Kearns - 05-30-2014 03:56 AM (05-29-2014 03:48 PM)Gerson W. Barbosa Wrote: Thank you very much!And I thank you both! --- Jeff K RE: Short Quadratic Solver (HP-42S) - Gerson W. Barbosa - 05-31-2014 03:31 AM (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. |