Post Reply 
(42S) Short Quadratic Solver
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.

Cheers,

Gerson

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

Jeff,

I hope this is better, although not so short anymore:

Code:

Q01 LBL Q
Q02 CF 1
Q03 ENTER
Q04 R↑
Q05 ÷
Q06 R↑
Q07 LASTx
Q08 ÷
Q09 ⁻2
Q10 ÷
Q11 ENTER
Q12 ENTER
Q13 x²
Q14 R↑
Q15 -
Q16 x<0?
Q17 SF 1
Q18 ABS
Q19 SQRT
Q20 ENTER
Q21 +/-
Q22 CMPLX+
Q23 FS? 1
Q24 CMPLX-
Q25 FS? 1
Q26 R↑
Q27 RTN

CK=3BA1 LEN 048.5 (HP-32SII)

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:

Q0001 LBL Q 
Q0002 CF 1
Q0003 INPUT A
Q0004 INPUT B
Q0005 INPUT C
Q0006 SQ(B)-4×A×C 
Q0007 x<0?
Q0008 SF 1 
Q0009 ABS
Q0010 √x
Q0011 STO E 
Q0012 -B÷2÷A
Q0013 STO F
Q0014 E÷2÷A
Q0015 STO G
Q0016 0
Q0017 FS? 1
Q0018 x<>y
Q0019 RCL F
Q0020 0
Q0021 CMPLX+
Q0022 x<>y
Q0023 STOP 
Q0024 RCL G
Q0025 +/-
Q0026 0
Q0027 FS? 1
Q0028 x<>y
Q0029 RCL F
Q0030 0
Q0031 CMPLX+
Q0032 x<>y
Q0033 RTN

CK=545E
LN=169    (hp 33s)

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.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Short Quadratic Solver (HP-42S) - Gerson W. Barbosa - 05-29-2014 04:23 AM



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