HP35S - Cubic (and incidentally quadratic) equations solver
|
04-07-2023, 09:31 AM
(This post was last modified: 05-20-2023 10:17 AM by Roberto Volpi.)
Post: #1
|
|||
|
|||
HP35S - Cubic (and incidentally quadratic) equations solver
Hi all,
inspired by similar programs from classic HP models, here a short program to solve cubic equations in the form of Ax^3+Bx^2+Cx+D=0 The algorithm search the 1st root x1, always real, divides the polynomial by (x-x1) with synthetic division (Ruffini rule) to obtain a quadratic equation, and solve it to have 2nd and 3rd roots x2 and x3. In case we have a quadratic equation in the form Ax^2+Bx+C=0 to solve, it processes directly to obtain the roots, real or complex. LINE INSTRUCTION C001 LBL C C002 CLVARS C003 STO D C004 R down C005 STO C C006 R down C007 STO B C008 R down C009 STO/B C010 STO/C C011 STO/D C012 RCL D C013 SGN C014 STO H C015 LAST X C016 ABS C017 RCL B C018 ABS C019 + C020 10 C021 STO P C022 R down C023 1E-6 C024 STO O C025 RCLxP C026 X<=Y? C027 GTO C025 C028 STO K C029 RCL P C030 STO/K C031 RCL H C032 +/- C033 STO H C034 RCL K C035 RCLxH C036 RCL+F C037 STO G C038 RCL F C039 X=Y? C040 GTO C059 C041 RCL G C042 RCL+B C043 RCLxG C044 RCL+C C045 RCLxG C046 RCL+D C047 STO L C048 ABS C049 RCL O C050 X>Y? C051 GTO C059 C052 RCL G C053 STO F C054 RCL L C055 RCLxH C056 X<0? C057 GTO C034 C058 GTO C029 C059 RCL G C060 R/S C061 1 C062 X<>Y C063 RCL+B C064 ENTER C065 RCLxG C066 RCL+C C067 GTO E001 E001 LBL E E002 -2 i 0 E003 x E004 LAST X E005 R up E006 x E007 / E008 X<>Y E009 LAST X E010 / E011 ENTER E012 ENTER E013 x E014 LAST X E015 X<>Y E016 R up E017 - E018 0.5 E019 Y^X E020 - E021 X<>Y E022 LAST X E023 + E024 RTN Instructions to solve cubic equation: input A press ENTER input B press ENTER input C press ENTER input D press XEQ C ENTER the calculator shows first root x1, always real, in stack X, and the accuracy in stack Y press R/S the calculator shows 2nd and 3rd roots, x2 and x3, in stack Y and X. The first real root x1 can be retrieved by pressing RCL G Instructions to solve quadratic equations input A press ENTER input B press ENTER input C press XEQ E ENTER the calculator shows both roots in stacks X and Y Enjoy! Put a calculator into your life! |
|||
04-07-2023, 07:55 PM
Post: #2
|
|||
|
|||
RE: HP35S - Cubic (and incidentally quadratic) equations solver
Nice program Roberto, thanks for sharing. Just to see if I understood the instructions and didn't make typing mistakes, ¿could you give me two numerical examples, one for cubic and the other for quadratic?. Pedro
|
|||
04-08-2023, 04:39 AM
Post: #3
|
|||
|
|||
RE: HP35S - Cubic (and incidentally quadratic) equations solver
(04-07-2023 07:55 PM)PedroLeiva Wrote: Nice program Roberto, thanks for sharing. Just to see if I understood the instructions and didn't make typing mistakes, ¿could you give me two numerical examples, one for cubic and the other for quadratic?. Pedro Thanks Pedro Let's take as example the following quadratic EQN: x^2 -x -12 Here how to calculate both roots: Press 1 Press ENTER Press -1 Press ENTER Press -12 Press XEQ E ENTER The calculator will give following result: Stack Y: -3 i 0 (1st root) Stack X: 4 i 0 (2nd root) ----- Now a quick example of cubic EQN: x^3 -13x -12 Here how to calculate all 3 roots: Press 1 Press ENTER Press 0 Press ENTER Press -13 Press -12 Press XEQ C ENTER The calculator will give as result: Stack Y: 0.000001 (theoretical approximation) Stack X: 4 (1st root, always real) Press R/S The calculator will give as result: Stack Y: -3 i 0 (2nd root) Stack X: -1 i 0 (3rd root) in case you forget to take note of 1st root, just press RCL G. Put a calculator into your life! |
|||
04-08-2023, 11:46 AM
Post: #4
|
|||
|
|||
RE: HP35S - Cubic (and incidentally quadratic) equations solver
Excellent Roberto, the program works very well. Thanks a lot. Pedro
|
|||
04-10-2023, 02:21 PM
Post: #5
|
|||
|
|||
RE: HP35S - Cubic (and incidentally quadratic) equations solver
As usual we divide all coefficients of the original polynomial \(ax^3 + bx^2 + cx + d\) by \(a\), so we start with the following polynomial:
\( f(x) = x^3 + px^2 + qx + r \) Using Horner's method we can reuse the intermediate values \(x_0 + p \) and \(x_0(x_0 + p) + q\) while calculating the derivative: \( \begin{matrix} & 1 & p & q & r \\ x_0 & 1 & x_0 + p & x_0(x_0 + p) + q & x_0(x_0(x_0 + p) + q) + r = f(x_0) \\ x_0 & 1 & 2 x_0 + p & x_0(3 x_0 + 2 p) + q = f'(x_0) & \\ \end{matrix} \) This allows calculating the derivative and synthetic division in one step. Registers R00: \(p = \frac{b}{a}\) R01: \(q = \frac{c}{a}\) R02: \(r = \frac{d}{a}\) R03: \(x_0 + p\) R04: \(x_0(x_0 + p) + q\) HP-42S Code: 00 { 69-Byte Prgm } HP-15C Code: 001 { 42 21 13 } f LBL C Example \( x^3 - x^2 - x - 1 = 0 \) 1 ENTER -1 ENTER ENTER XEQ "CBRT" X: 1.83928675521 R/S Y: -0.41964 - i0.60629 X: -0.41964 + i0.60629 Caveat The value \(- \frac{r}{q}\) is used as initial value for Newton's iteration. This is the next guess when you start with \(0\). However, this might not always be the best value. I was a bit sloppy with the termination criterion. It may never be reached, but I've never experienced it. |
|||
04-20-2023, 07:18 AM
Post: #6
|
|||
|
|||
RE: HP35S - Cubic (and incidentally quadratic) equations solver
(04-10-2023 02:21 PM)Thomas Klemm Wrote: As usual we divide all coefficients of the original polynomial \(ax^3 + bx^2 + cx + d\) by \(a\), so we start with the following polynomial: Just tried the version for HP15C, but after obtaining the 1st root (20 seconds in an iPhone emulator, which is much much faster than a real HP15C), R/S just gives ERROR 0. Can you confirm specific instructions also for HP15C? Heartfelt thanks! Put a calculator into your life! |
|||
04-20-2023, 11:36 AM
Post: #7
|
|||
|
|||
RE: HP35S - Cubic (and incidentally quadratic) equations solver
(04-20-2023 07:18 AM)Roberto Volpi Wrote: R/S just gives ERROR 0. From HP-15C Owner's Handbook p. 121: Quote:Complex mode is activated Either set flag 8 before running the program or set it only if \(X < 0\) before calculating the square root in the following line: Code: 045 { 11 } √x̅ |
|||
05-12-2023, 07:49 AM
(This post was last modified: 05-12-2023 07:50 AM by Roberto Volpi.)
Post: #8
|
|||
|
|||
RE: HP35S - Cubic (and incidentally quadratic) equations solver
(04-20-2023 11:36 AM)Thomas Klemm Wrote:(04-20-2023 07:18 AM)Roberto Volpi Wrote: R/S just gives ERROR 0. Tried again with complex mode activated, and it is ok, except when a cubic polynomial with multiple roots is entered. Ex. x^3+3x^2+3x+1, instead of giving -1 gives ERROR 0 The same for x^3-3x^3+3x-1. Put a calculator into your life! |
|||
05-13-2023, 10:33 AM
Post: #9
|
|||
|
|||
RE: HP35S - Cubic (and incidentally quadratic) equations solver
(05-12-2023 07:49 AM)Roberto Volpi Wrote: … it is ok, except when a cubic polynomial with multiple roots is entered. Thank you for pointing that out. In these cases the derivative at this root is zero leading to the \(y \div 0\) error in line 32. You can wiggle the coefficients a bit to get roots that are close to the exact roots. Example 1.000000001 ENTER 3 ENTER ENTER 1 f C -0.999031396 R/S -1.000484301 f (i) 0.000839643 x<>y -1.000484301 f (i) 0.000839643 As you can see they might turn out to be complex though. How do you handle this special case in your program? |
|||
05-13-2023, 12:40 PM
Post: #10
|
|||
|
|||
RE: HP35S - Cubic (and incidentally quadratic) equations solver | |||
05-13-2023, 06:00 PM
(This post was last modified: 05-13-2023 06:06 PM by Gerson W. Barbosa.)
Post: #11
|
|||
|
|||
RE: HP35S - Cubic (and incidentally quadratic) equations solver
(04-10-2023 02:21 PM)Thomas Klemm Wrote: Caveat I have, on Free42 with the first example I tried: 1 ENTER 2 ENTER 3 ENTER 4 XEQ “CBRT” (No problem on the real 42S, at least with this example). The insertion of RND between steps 30 and 31 might be a workaround, but accuracy becomes limited to only twelve significant digits on Free42. Also, this will cause your example to run forever with FIX 1. Gerson. |
|||
05-18-2023, 06:26 PM
(This post was last modified: 05-18-2023 06:26 PM by Roberto Volpi.)
Post: #12
|
|||
|
|||
RE: HP35S - Cubic (and incidentally quadratic) equations solver
How do you handle this special case in your program? [/quote] No special care is needed. Just input the cubic equation, and in case it has repeated roots they will be shown on the stacks. EX. X^3+3X^2+3X+1 1st root always real: -1 2nd and 3rd root: stack y: -1 stack x: -1 Just it. Put a calculator into your life! |
|||
05-20-2023, 09:45 AM
Post: #13
|
|||
|
|||
RE: HP35S - Cubic (and incidentally quadratic) equations solver | |||
05-20-2023, 10:22 AM
Post: #14
|
|||
|
|||
RE: HP35S - Cubic (and incidentally quadratic) equations solver
(05-20-2023 09:45 AM)Thomas Klemm Wrote:(04-07-2023 09:31 AM)Roberto Volpi Wrote: C026 X>=Y? Thanks Thomas, Just chequed the instruction C026 and you were right! It was a bug. Correction has been made in the original text. Thanks once more for your valuable contribution. Put a calculator into your life! |
|||
05-20-2023, 12:05 PM
Post: #15
|
|||
|
|||
RE: HP35S - Cubic (and incidentally quadratic) equations solver
It took me a while to understand how your program works.
The following Python program helped me to do so: Code: from math import copysign Then a digit by digit algorithm is used to approach the solution. Whenever a change of sign is found the next decimal place to the right is considered. Here's an example for the equation \(x^3 - x^2 - x - 1 = 0\): cbeq(1, -1, -1, -1) Code: C025: X= 0.000010000000 Y= 2.000000000000 True G= 1.839286900000 But this made me think, what happens if the solution gets closer and closer to 1e-6. cbeq(1e3, -1e2, -1e1, -1) Code: C025: X= 0.000010000000 Y= 0.101000000000 True G= 0.183920000000 cbeq(1e6, -1e4, -1e2, -1) Code: C025: X= 0.000010000000 Y= 0.010001000000 True G= 0.019000000000 cbeq(1e9, -1e6, -1e3, -1) Code: C025: X= 0.000010000000 Y= 0.001000001000 True G= 0.001000000000 Unfortunately it looks like fewer and fewer digits are calculated. |
|||
05-20-2023, 12:34 PM
(This post was last modified: 05-20-2023 12:46 PM by Thomas Klemm.)
Post: #16
|
|||
|
|||
RE: HP35S - Cubic (and incidentally quadratic) equations solver
We can use Horner's method to calculate the Taylor polynomial at \(x=1\) of the initial cubic polynomial:
\( ax^3 + bx^2 + cx + d = a (x - 1)^3 + (3 a + b) (x - 1)^2 + (3 a + 2 b + c) (x - 1) + (a + b + c + d) \) This leads to the following outline: \( \begin{matrix} a & b & c & d \\ a & a + b & a + b + c & a + b + c + d \\ a & 2a + b & 3a + 2b + c & \\ a & 3a + b & & \\ a & & & \\ \end{matrix} \) The following Python program uses the shift function to do this: Code: def shift(x, p): Once a change of sign is found the zoom function is used with the previous values. From then on the next decimal place to the right is considered. This allows calculating the root using only addition and multiplication by ten which is a simple shift in BCD. Examples: solve([1, -1, -1, -1], 75) 1839286755214161132551852564653286600424178746097592246778758639404203222080 \(x \approx 1.83928675521416113255185256465328660042417874609759224677875863940420322208\) solve([1, -2, 3, -4], 75) 1650629191439388218880800967426197435895495231706172591999594185884157996140 \(x \approx 1.65062919143938821888080096742619743589549523170617259199959418588415799614\) solve([1, -3, 3, -1], 10) 10000000000 \(x=1\) Please consider this just a sketch of an algorithm. But those familiar with computing square roots in HP calculators may recognize the similarities. |
|||
05-21-2023, 10:49 AM
Post: #17
|
|||
|
|||
RE: HP35S - Cubic (and incidentally quadratic) equations solver
(05-20-2023 12:05 PM)Thomas Klemm Wrote: It took me a while to understand how your program works. Long story short, the algorithm to calculate the first, always real root, is an open numerical method. We start with a guess, and decimals tlll 10E-6 are calculated. I have also tried to use native solver of HP35S. The program was shorter, but sometimes the accuracy was poor, sometime execution time was too long, or both. I have used the constant value 1E-6 in order to balance speed and accuracy. I consider extremely important to obtain the first root in a decent time, but those who prefer a different value may change instruction C023 as they like. Put a calculator into your life! |
|||
05-22-2023, 07:53 AM
Post: #18
|
|||
|
|||
RE: HP35S - Cubic (and incidentally quadratic) equations solver
With little extra effort, we can also use Halley's method:
\( \begin{align} x_{n+1}=x_{n}-{\frac {f(x_{n})}{f'(x_{n})-{\frac {f(x_{n})}{f'(x_{n})}}{\frac {f''(x_{n})}{2}}}} \end{align} \) The advantage is that the rate of convergence to the root is cubic. Code: 00 { 80-Byte Prgm } Example But unfortunately the previous example \(x^3-x^2-x-1=0\) doesn't work anymore: 1 ENTER -1 ENTER ENTER XEQ "CBEQ" Divide by 0 x: 0 In such a case, you can just use a different initial value and start the inner loop with: 2 XEQ 00 1.83928675521 Feel free to include a RND command before this line: Code: 35 X≠Y? This avoids the endless loop that Gerson experienced. Also it allows to limit the execution time when full accuracy is not needed. These are the intermediate results of the previous example when rounded with FIX 12: 2 1.84090909091 1.83928675734 1.83928675521 1.83928675521 |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 3 Guest(s)