The Museum of HP Calculators

HP Forum Archive 20

[ Return to Index | Top of Index ]

Which quadratic formula is use?
Message #1 Posted by Michel Beaulieu on 29 Sept 2011, 8:47 a.m.

I'm amaze by the fact that this quadratic formula is so short :

001- LBL E 002- ENTER 003- R^ 004- / 005- R^ 006- LSTx 007- / 008- 2 009- CHS 010- / 011- ENTER 012- ENTER 013- x^2 014- R^ 015- - 016- SQRT 017- - 018- x<>y 019- LSTx 020- + 021- RTN

I found this on a museum's archive 17 :

http://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/archv017.cgi?read=114345

My shortest program have 30 step. I realize that the formula use for this program is not the same old one we learn at high school.

I'm still learning the RPN stack and programming on the 15C. What is the "modified" formula use?

Thanks

      
Re: Which quadratic formula is use?
Message #2 Posted by Dave Britten on 29 Sept 2011, 9:08 a.m.,
in response to message #1 by Michel Beaulieu

It's probably the same formula in the end, it's just using a ton of RPN tricks to cram the computation into as short a program as possible.

      
Re: Which quadratic formula is use?
Message #3 Posted by Gerson W. Barbosa on 29 Sept 2011, 9:47 a.m.,
in response to message #1 by Michel Beaulieu

Here it is, courtesy of Allen:

On the HP-42S, 15 steps suffice:

00 { 28-Byte Prgm }                        
01 LBL "Q"                     
02 RCL/ ST Z                                         
03 X<> ST Z 
04 /
05 -2                        
06 /                  
07 STO ST Z                 
08 x^2          
09 X<>Y                   
10 -             
11 SQRT      
12 RCL+ ST Y                     
13 X<>Y               
14 RCL- ST L 
15 END

Edited: 29 Sept 2011, 9:54 a.m.

      
Re: Which quadratic formula is use?
Message #4 Posted by Jeff O. on 29 Sept 2011, 10:12 a.m.,
in response to message #1 by Michel Beaulieu

Definitely the standard quadratic formula with optimum use of stack to provide both answers in x and y, as shown in the following listing which shows the stack contents after each function:

Step	Function	     Stack Contents
--------------------------------------------------------	
			T:	--	
			Z:	A	
			Y:	B	
1	 LBL E		X:	C	
--------------------------------------------------------
			T:	A	
			Z:	B	
			Y:	C	
2	 ENTER		X:	C	
--------------------------------------------------------
			T:	B	
			Z:	C	
			Y:	C	
3	 R^		X:	A	
--------------------------------------------------------
			T:	B	
			Z:	B	
			Y:	C	
4	 /		X:	C/A	
--------------------------------------------------------
			T:	B	
			Z:	C	
			Y:	C/A	
5	 R^		X:	B	
--------------------------------------------------------
			T:	C	
			Z:	C/A	
			Y:	B	
6	 LSTx		X:	A	
--------------------------------------------------------
			T:	C	
			Z:	C	
			Y:	C/A	
7	 /		X:	B/A	
--------------------------------------------------------
			T:	C	
			Z:	C/A	
			Y:	B/A	
8	 2		X:	2	
--------------------------------------------------------
			T:	C	
			Z:	C/A	
			Y:	B/A	
9	 CHS		X:	-2	
--------------------------------------------------------
			T:	C	
			Z:	C	
			Y:	C/A	
10	 /		X:	-B/2A	
--------------------------------------------------------
			T:	C	
			Z:	C/A	
			Y:	-B/2A	
11	 ENTER		X:	-B/2A	
--------------------------------------------------------
			T:	C/A	
			Z:	-B/2A	
			Y:	-B/2A	
12	 ENTER		X:	-B/2A	
--------------------------------------------------------
			T:	C/A	
			Z:	-B/2A	
			Y:	-B/2A	
13	 x^2		X:	B2/4A2	
--------------------------------------------------------
			T:	-B/2A	
			Z:	-B/2A	
			Y:	B2/4A2	
14	 R^		X:	C/A	
--------------------------------------------------------
			T:	-B/2A	
			Z:	-B/2A	
			Y:	-B/2A	
15	 -		X:	B2/4A2-C/A	
--------------------------------------------------------
			T:	-B/2A	
			Z:	-B/2A	
			Y:	-B/2A	
16	 SQRT		X:	SQRT(B2/4A2-C/A)	
--------------------------------------------------------	
		        T:	-B/2A	
			Z:	-B/2A	
			Y:	-B/2A	
17	 -		X:	-B/2A-SQRT(B2/4A2-C/A)
--------------------------------------------------------	
			T:	-B/2A	
			Z:	-B/2A	
			Y:	-B/2A-SQRT(B2/4A2-C/A)	
18	 x<>y		X:	-B/2A	
--------------------------------------------------------
			T:	-B/2A	
			Z:	-B/2A-SQRT(B2/4A2-C/A)	
			Y:	-B/2A	
19	 LSTx		X:	SQRT(B2/4A2-C/A)	
--------------------------------------------------------
			T:	-B/2A	
			Z:	-B/2A	
			Y:	-B/2A-SQRT(B2/4A2-C/A)	
20	 +		X:	-B/2A+SQRT(B2/4A2-C/A)	
--------------------------------------------------------
			T:	-B/2A	
			Z:	-B/2A	
			Y:	-B/2A-SQRT(B2/4A2-C/A)	
21	 RTN		X:	-B/2A+SQRT(B2/4A2-C/A)	
--------------------------------------------------------

Of course SQRT(B2/4A2-C/A) is equal to SQRT(B2-4AC)/2A

listing edited to hopefully make it easier to read

Edited: 29 Sept 2011, 10:05 p.m. after one or more responses were posted

            
Re: Which quadratic formula is use?
Message #5 Posted by Michel Beaulieu on 29 Sept 2011, 1:20 p.m.,
in response to message #4 by Jeff O.

Very impressive use of the stack! I will study it tonight after work...

                  
Re: Which quadratic formula is use?
Message #6 Posted by Jeff O. on 29 Sept 2011, 1:57 p.m.,
in response to message #5 by Michel Beaulieu

Agreed. I have edited the listing to hopefully make it easier to read. In case it is not clear, the stack contents on each line are as they will be after the function on that line is executed.

                        
Re: Which quadratic formula is use?
Message #7 Posted by C.Ret on 29 Sept 2011, 4:42 p.m.,
in response to message #6 by Jeff O.

Thank you.

Usefull code. Here a more compact version of your code I have input into a personal note format:

Step  Function       t:                       z:                        y:                       x:             Lastx:
----- --------   ------   ----------------------   -----------------------   ----------------------             ------
01    LBL E           ~                        a                         b                        c
02    ENTER           a                        b                         c                        c
03    R^              b                        c                         c                        a
04    /               b                        b                         c                      c/a                  a
05    R^              b                        c                       c/a                        b
06    LSTx            c                      c/a                         b                        a
07    /               c                        c                       c/a                      b/a                  a
08    2               c                      c/a                       b/a                        2
09    CHS             c                      c/a                       b/a                       -2                 -2
10    /               c                        c                       c/a                    -b/2a                 -2
11    ENTER           c                      c/a                     -b/2a                    -b/2a
12    ENTER         c/a                    -b/2a                     -b/2a                    -b/2a
13    x^2           c/a                    -b/2a                     -b/2a                   b²/4a²              -b/2a
14    R^          -b/2a                    -b/2a                    b²/4a²                      c/a
15    -           -b/2a                    -b/2a                     -b/2a               b²/4a²-c/a                c/a
16    SQRT        -b/2a                    -b/2a                     -b/2a         SQRT(b²/4a²-c/a)         b²/4a²-c/a
17    -           -b/2a                    -b/2a                     -b/2a   -b/2a-SQRT(b²/4a²-c/a)   SQRT(b²/4a²-c/a)
18    x<>y        -b/2a                    -b/2a    -b/2a-SQRT(b²/4a²-c/a)                    -b/2a
19    LSTx        -b/2a   -b/2a-SQRT(b²/4a²-c/a)                     -b/2a         SQRT(b²/4a²-c/a)
20    +           -b/2a                    -b/2a    -b/2a-SQRT(b²/4a²-c/a)   -b/2a+SQRT(b²/4a²-c/a)   SQRT(b²/4a²-c/a)
21    RTN                                                               x1                       x2
----- --------   ------   ----------------------   -----------------------   ----------------------             ------
where x1 and x2 are the real roots of equation a.x2+b.x+c=0

Exemple :
Type 2 [ENTER] 10 [CHS][ENTER] 12 [ GSB ][ E ] to solve 2.x2 - 10.x + 12=0 and get x1 = 2 and x2 = 3
The calculator first display x2, you have to press [x<->y]key to get x1.

But you will be in trouble solving 2.x2 - 8.x + 26=0 !

Note that on HP-15c, this code needs very few modifications to also produce real or complex solution(s) to any a.x2+ b.x + c = 0 equation with real or complex coefficients a, b and c.
Step  Func
----- -------- 
01    LBL E    
02    ENTER    
03    R^       
04    /        
05    R^       
06    LSTx     
07    /        
08    2        
09    CHS      
10    /        
11    ENTER    
12    ENTER    
13    x^2      
14    R^       
15    -        
16    TEST 2      @  ( x<=0 ? )
17    SF 8        @  (Complex mode ON)
18    SQRT     
19    -        
20    x<>y     
21    LSTx     
22    +        
23    RTN      
----- -------- 

To solve quadratic equation 2.x2 - 8.x + 26 = 0, y have to type :
[ g ][ CF ][ 8 ] to set complex mode OFF in order to enter real coefficients :
[ 2 ][ENTER^][ 8 ][CHS][ENTER^][2][6] to type in respectively a=2, b=-8 and c=26.
[GSB][ E ] to run code.

Note that the ‘C’ enunciator indicates complex solutions z1 = 2-3i and z2 = 2+3i. Imaginary parts have to be displayed using [ f ][Re<->I] switchs or by holding [ f ][(i)] key.

To enter complex coefficients, simply enter a, b and c as complex using [ f][ I ] or [ f ][Re<->Im] method.

Amazing HP-15c!

Edited: 29 Sept 2011, 4:54 p.m.

                              
Re: Which quadratic formula is use?
Message #8 Posted by Jeff O. on 29 Sept 2011, 10:17 p.m.,
in response to message #7 by C.Ret

Thanks.
I considered laying the stack contents out horizontally. But I seem to visualize the stack in a vertical layout and find it easier to follow and/or develop how the contents move around if I present it that way. I'll have to do a layout for the complex version tomorrow to see how it works.


[ Return to Index | Top of Index ]

Go back to the main exhibit hall