(15C) Quadratic Equation - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (/forum-10.html) +--- Forum: General Software Library (/forum-13.html) +--- Thread: (15C) Quadratic Equation (/thread-10977.html) (15C) Quadratic Equation - Gamo - 06-29-2018 12:20 PM This program calculate Quadratic Equation of aX^2 + bX + c = 0 Answer can be both Real and Complex integer. Procedure: a [ENTER] b [ENTER] c f [A] --> 1st Answer [X<>Y] --> 2nd Answer If answer is Complex the display will show small letter (c) on screen with the Integer part shown. To see the complex part press and hold f (i) this will give you positive and negative complex answer. To disable (c) --> g [CF] 8 ------------------------------------------------------------------------------------- Example 1: 2X^2 + 2X - 50 = 0 2 [ENTER] 2 [ENTER] 50 [CHS] f [A] Answer: -5.5249 [X<>Y] 4.5249 --------------------------------------------------------------------------------- Example 2: 5X^2 - 10X - 800 = 0 5 [ENTER] 10 [CHS] [ENTER] 800 [CHS] f [A] Answer: 13.6886 [X<>Y] -11.6886 -------------------------------------------------------------------------------- Example 3: -3x^2 - 2x - 1 = 0 3 [CHS] [ENTER] 2 [CHS] [ENTER] 1 [CHS] f [A] Answer is Complex: -0.3333 small (c) shown on display press and hold f [ (i) ] -0.4714 Final answer is -0.3333 ± 0.4714 ------------------------------------------------------------------------------ Program: Quadratic Equation Code:  LBL A CF8 X<>Y 2 CHS ÷ CF0 X<0 (TEST 2) SF0 STO I X^2 R↓ x LSTx X<>Y R↑ - CHS X≥0 (TEST 3) GTO 1 CHS √x R↑ ÷ ENTER CHS RCL I LSTx ÷ X<>Y f I ENTER R↑ f I RTN ---------------------------------------------------------------------- LBL 1 √x F0 CHS RCL+ I X=0 RTN ÷ LSTx R↑ ÷ RTN Gamo RE: (15C) Quadratic Equation - C.Ret - 07-30-2021 03:46 PM Please let me share here an evolution of the previous code. This new version of the solver find any roots of the Quadratic Equation of $$a.x^2 + b.x + c = 0$$ where the three coefficients $$a$$ ,$$b$$ and $$c$$ are any real or complex numbers. Consequently, the two roots are real or complex number as well and not necessary conjugates. The usage of this new version is expressly the same as the version posted by Gamo. Code: Code: 001-       2       2                  012-      36      ENTER 002-      16      CHS                 013-   43 11    g x² 003-      20       ×                  014-   43 33    g R↑ 004-   43 36    g LSTx                015-      30       - 005-   43 33    g R↑                  016-43,30, 2    g x<0?   (Test 2)   006-      20       ×                  017-43, 4, 8    g SF 8 007-      10       ÷                  018-      11      √x 008-      34      x↔y                 019-      30       - 009-   43 36    g LSTx                020-      34      x↔y 010-      10       ÷                  021-   43 36    g LSTx 011-      36      ENTER               022-      40       + In: Z: a Y: b X: c Out: Y: x2 X: z1 Register: none Flags: Flag 8 is set when any root x1 or x2 is complex. Usage: Enter the three coefficients in the order $$a$$, $$b$$ and $$c$$ where $$a$$ can't be zero. Run the code by pressing the [R/S] key. You may have to add a user label at the beginning of this short code if it's sharing the program memory space with any other code. The roots of the corresponding quadratic polynomial are computed and stored in stack at X: and Y:. No memory register is used or affected by the run of this code. If one of the two roots is complex, the 'c' annunciator is lit on at the bottom of the display. it's a good practice to clear Flag_8 (complex mode indicator) before entering real coefficients, so that the appearing of the 'c' annunciator indicate that there is no real root. But this is in no way mandatory. Entering a complex number automaticaly set the flag 8 to activate the complex stack and computations. To see the complex part press and hold f[(i)]. If the complex 'c' annunciators is off and no imaginary part exist then Error 3 occurs. To see the other root press [X↔Y] key. this will give you positive and negative complex answer. Example 1 integer coefficients : $$108x^2+1188x-140616 = 0$$ Optionally, since no coefficient is a complex number, you may clear Complex Mode : g[CF][ 8 ] Enter a: [ 1 ][ 0 ][ 8 ][ENTER] Enter b: [ 1 ][ 1 ][ 8 ][ 8 ][ENTER] Enter c: [ 1 ][ 4 ][ 0 ][ 6 ][ 1 ][ 6 ][CHS] Press [R/S] to run code and display the first root: " 31.00000 " Press [X↔Y] to display the second root: "-42.00000 " The roots of the quadratic equation $$108x^2+1188x-140616 = 0$$ are the two integers $$31$$ and $$-41$$. Example 2 number coefficients : $$5x^2-51.2x+131.072 = 0$$ Optionally, since no coefficient is a complex number, you may clear Complex Mode : g[CF][ 8 ] Enter a: [ 5 ][ENTER] Enter b: [ 5 ][ 1 ][ . ][ 2 ][CHS][ENTER] Enter c: [ 1 ][ 3 ][ 1 ][ . ][ 0 ][ 7 ][ 2 ] Press [R/S] to run code and display the first root: " 5.12000 " Press [X↔Y] to display the second root: " 5.12000 " (double root here !) The quadratic equation $$5x^2-51.2x+131.072 = 0$$ admit a double root $$x_1=x_2=5.12$$ Example 3 integer coefficients and complex conjugate roots : $$x^2-4x+13 = 0$$ Optionally, since no coefficient is a complex number, you may clear Complex Mode : g[CF][ 8 ] Enter a: [ 1 ][ENTER] (entering a non zero a coefficient is mandatory) Enter b: [ 4 ][CHS][ENTER] Enter c: [ 1 ][ 3 ] Press [R/S] to run code and notice that the 'c' annunciator is displayed indicating complex roots: Real part of first root is on display " 2.00000 " Press and hold f[(i)] key to see imaginary part of first root: " 3.00000 " Press [X↔Y] to display the second root real part : " 2.00000 " Press and hold f[(i)] to display the second root’s imaginary part: "-3.00000 " The two roots of quadratic equation $$z^2-4z+13 = 0$$ are the conjugates $$z_1=2+3i$$ and $$z_2=2-3i=\bar{z_1}$$ Example 4 full complex example : $$(2+3i).z^2+(-6-87i).z+(-483+1193i) = 0$$ Enter a: [ 2 ][ENTER] [ 3 ] f[ I ] (note that the 'c' annunciators lit on) Enter b: [ 6 ][CHS][ENTER] [ 8 ][ 7 ][CHS] f[ I ] Enter c: [ 4 ][ 8 ][ 7 ][CHS][ENTER] [ 1 ][ 1 ][ 9 ][ 3 ] f[ I ] Press [R/S] to run code. Real part of first root is " 17.00000 " Press and hold f[(i)] key to see imaginary part of first root: "-7.00000 " Press [X↔Y] to display the second root real part : " 4.00000 " Press and hold f[(i)] to display the second root’s imaginary part: " 19.00000 " The two roots of quadratic equation $$(2+3i).z^2+(-6-87i).z+(-483+1193i) = 0$$ are the complex $$z_1=17-7i$$ and $$z_2=4+19i$$ Example 5 mixte example : $$(3-i).z^2+(-49+33i).z+120-90i = 0$$ Enter a: [ 3 ][ENTER] [ 1 ][CHS] f[ I ] (note that the 'c' annunciators lit on) Enter b: [ 4 ][ 9 ][CHS][ENTER] [ 3 ][ 3 ] f[ I ] Enter c: [ 1 ][ 2 ][ 0 ][ENTER] [ 9 ][ 0 ][CHS] f[ I ] Press [R/S] to run code. Real part of first root is " 15.00000 " Press and hold f[(i)] key to see imaginary part of first root: "-5.00000 " Press [X↔Y] to display the second root real part : " 3.00000 " Press and hold f[(i)] to display the second root’s imaginary part: " 0.00000 " The quadratic equation $$(3-i).z^2+(-49+33i).z+120-90i = 0$$ have one complex root $$z_1=15-5i$$ and one real root $$x_2=3$$