Post Reply 
(11C) CUBIC EQUATION
06-09-2018, 11:49 AM (This post was last modified: 06-11-2018 08:32 AM by Gamo.)
Post: #1
(11C) CUBIC EQUATION
This program finds the roots of cubic equation of the form

f(x) = X^3 + aX^2 + bX + c

where a, b and c are real.

It does so by extracting the first root, performing synthetic division,
and solving the resulting quadratic equation.

(This program was from HP-19C/HP-29C Applications Book, P.6 and here is the modified version for HP-11C)

Program: CUBIC EQUATION
Code:

LBL A      
STO 1
Rv
STO 2
Rv
STO 3
EEX
CHS
4
STO 0
CLx
STO 4
RTN
-----------------------------------------------------------
LBL B     
RCL 3    
RCL 3
ABS
÷
STO 6
LSTx
RCL 1
ABS
+
RCL 0
-----------------------------------------------------------------------
LBL 0
10
x
X≤Y
GTO 0
STO 7
-------------------------------------------------------------------------
LBL 2
10
STO÷7
RCL 6
CHS 
STO 6
------------------------------------------------------------------------
LBL 1
RCL 7
RCL 6
x
RCL 4
+
STO 5
RCL 4
X=Y
GTO 3
RCL 5
RCL 1
+
RCL 5
x
RCL 2
+
RCL 5
x
RCL 3
+
STO 8
ABS 
RCL 0
X˃Y
GTO 3
RCL 5
STO 4
RCL 8
RCL 6
x
X˂0
GTO 1
GTO 2
-----------------------------------------------------------------------
LBL 3
RCL 5
R/S
RCL 1
+
CHS
STO 1
2
÷
ENTER
X^2
RCL 1
CHS
RCL 5
x
RCL 2
+
STO 3
-
R/S
√X
X<>Y
ABS
+
RCL 1
ENTER
ABS
÷
x
R/S
RCL 3
X<>Y
÷
RTN
--------------------------------------------------------------------------
LBL C   //  Complex Roots
CHS
√X
R/S

Usage:
c [ENTER] b [ENTER] a

[A] ---> 0
[B] ---> 1st Root

[R/S] ---> Discriminant // If D≥0 continue [R/S] [R/S] If D˂0 continue to [C]

[R/S] ---> 2nd Root
[R/S] ---> 3nd Root

[C] ---> Complex Roots // [X<>Y] (u in Y) and (v in X)


Put to USER MODE --> f USER

Example: X^3 - 6X^2 + 11X - 6 = 0

6 [CHS] [ENTER] 11 [ENTER] 6 [CHS]

[A] 0
[B] 3 // 1st root
[R/S] 0.25 // D is greater than or equal zero go ahead with R/S
[R/S] 2 // 2nd root
[R/S] 1 // 3th root

Answer of 3 roots are 3, 2 and 1
----------------------------------------------------------------------------------------
Example: X^3 - 4X^2 + 8X - 8 = 0

8 [CHS] [ENTER] 8 [ENTER] 4 [CHS]

[A] 0
[B] 2 // 1st root
[R/S] -3 // D is less than zero then use LBL C for complex roots
[C] 1.73 [X<>Y] 1 // 1.73 (Imaginary Part) and 1 (Real Part)

Real answer is 2
Complex answer is 1 ±1.73i
-----------------------------------------------------------------------------------------

Gamo
Find all posts by this user
Quote this message in a reply
06-09-2018, 12:34 PM
Post: #2
RE: (11C) CUBIC EQUATION
Hello Dieter

Thanks for the look up I have make update to the instruction procedure.
I hope this update make it a little more clear. Any suggestion is very welcome.

Remark about this program:
This program routine take about 1 minute or more to compute for the roots when used with HP-11C
With this program on HP-15C LE or 15C App this run very fast instant answer.

Gamo
Find all posts by this user
Quote this message in a reply
06-09-2018, 12:54 PM
Post: #3
RE: (11C) CUBIC EQUATION
(06-09-2018 12:34 PM)Gamo Wrote:  Thanks for the look up I have make update to the instruction procedure.
I hope this update make it a little more clear. Any suggestion is very welcome.

I see you were faster in reading my post than I was in deleting it. #-)
I found it may have been a little harsh, and then I found more errors that would have required even more questions and comments. So I decided to cancel my post.

But while we're at it: are you sure the program is correct? For instance R4 is not initialized. ?!?

(06-09-2018 12:34 PM)Gamo Wrote:  Remark about this program:
This program routine take about 1 minute or more to compute for the roots when used with HP-11C
With this program on HP-15C LE or 15C App this run very fast instant answer.

Maybe I can post a program that calculates the solutions directly. This should be much faster. The method is known since the 16th century (Tartaglia, Cardano). I have a HP67 program that does it. It may not fit in the 98 steps of a 19C/29C, but a translation for the 11C/15C should be easy.

Dieter
Find all posts by this user
Quote this message in a reply
06-09-2018, 01:00 PM
Post: #4
RE: (11C) CUBIC EQUATION
(06-09-2018 12:54 PM)Dieter Wrote:  I see you were faster in reading my post than I was in deleting it....

LOL, well that explains my confusion reading the thread. I was wondering if Gamo was somehow automatically channeling your feedback without you even having to post it. Smile

--Bob Prosperi
Find all posts by this user
Quote this message in a reply
06-09-2018, 01:18 PM
Post: #5
RE: (11C) CUBIC EQUATION
(06-09-2018 01:00 PM)rprosperi Wrote:  
(06-09-2018 12:54 PM)Dieter Wrote:  I see you were faster in reading my post than I was in deleting it....

LOL, well that explains my confusion reading the thread. I was wondering if Gamo was somehow automatically channeling your feedback without you even having to post it. Smile

Yeah, LOL!

Greetings,
    Massimo

-+×÷ ↔ left is right and right is wrong
Visit this user's website Find all posts by this user
Quote this message in a reply
06-09-2018, 06:05 PM (This post was last modified: 06-09-2018 07:14 PM by Dieter.)
Post: #6
RE: (11C) CUBIC EQUATION
(06-09-2018 12:54 PM)Dieter Wrote:  Maybe I can post a program that calculates the solutions directly. This should be much faster.

OK, here it is. This should run on various classic RPN HPs, including the 11C, 15C, 67/97, 41 and maybe others.

Code:
001  LBL C
002  STO 3
003  R↓
004  STO 2
005  R↓
006  STO 1
007  R↓
008  STO/1
009  STO/2
010  STO/3
011  RCL 1
012  ENTER
013  x^2
014  4
015  .
016  5
017  /
018  RCL 2
019  -
020  RCL 1
021  3
022  /
023  STO 1
024  *
025  STO+3
026  R↓
027  RCL 1
028  *
029  STO-2
030  RCL 2
031  3
032  /
033  ENTER
034  X^2
035  *
036  STO 0
037  RCL 3
038  2
039  /
040  x^2
041  +
042  x<0?
043  GTO 3
044  SQRT
045  ENTER
046  ENTER
047  RCL 3
048  2
049  /
050  -
051  X<>Y
052  LastX
053  +
054  CHS
055  GSB 0
056  STO 2
057  STO 3
058  X<>Y
059  GSB 0
060  STO+2
061  STO-3
062  LBL 2
063  RCL 2
064  RCL 1
065  -
066  R/S
067  RCL 2
068  2
069  /
070  RCL 1
071  +
072  CHS
073  RCL 3
074  .
075  7
076  5
077  SQRT
078  *
079  ABS
080  PAUSE
081  X<>Y
082  R/S
083  GTO 2
084  LBL 0
085  x=0?
086  RTN
087  ENTER
088  ABS
089  /
090  LastX
091  3
092  1/x
093  Y^X
094  *
095  RTN
096  LBL 3
097  RCL 3
098  CHS
099  2
100  /
101  RCL 0
102  CHS
103  SQRT
104  /
105  COS-1
106  3
107  STO/2
108  /
109  STO 3
110  RCL 2
111  CHS
112  SQRT
113  2
114  *
115  STO 2
116  LBL 1
117  RCL 2
118  RCL 3
119  COS
120  *
121  RCL 1
122  -
123  R/S
124  ENTER
125  .
126  5
127  CHS
128  COS-1
129  STO-3
130  GTO 1

The program determines the roots for ax³+bx²+cx+d.
It uses the data registers 0...3. "COS-1" is the inverse cosine (cos–1).
If you want to see the discriminant you may insert a PAUSE between line 041 and 042.
Even on slow machines like the 11C or 67/97 the program should run within a few seconds.

Usage:
a [ENTER] b [ENTER] c [ENTER] d

[C] => first real root

- if three real roots exist:
[R/S] => second root
[R/S] => third root

- if the other two roots are complex (conjugate):
[R/S] => "imaginary part", real part
Both parts can be reviewed with the [X<>Y] key.

Examples:

2x³ + 3x² – 6x – 5

2 [ENTER] 3 [ENTER] 6 [CHS] [ENTER] 5 [CHS]
  [C]    1,5297
[R/S] –0,7022
[R/S] –2,3275

x³ + x² + x – 1

1 [ENTER] 1 [ENTER] 1 [ENTER] 1 [CHS]
  [C]    0,5437
[R/S] "1,1151"  –0,7718

So the real root is at 0,5437 and the two others are –0,7718 ±1,1151 i.

Dieter
Find all posts by this user
Quote this message in a reply
06-09-2018, 07:50 PM (This post was last modified: 06-09-2018 08:25 PM by Dieter.)
Post: #7
RE: (11C) CUBIC EQUATION
Gamo, I see you updated your initial post, but there are still errors:

(06-09-2018 11:49 AM)Gamo Wrote:  LBL A // Input c Enter b Enter c then press A

Enter c, then b, then c once more ?!?

(06-09-2018 11:49 AM)Gamo Wrote:  RCL 3 // If D ˂ 0 then press LBL C for the answer of complex roots

If you press [LBL] [C] on an 11C (or another calculator) nothing will happen. ;-)
You have to press [GSB] [C] or [C].

(06-09-2018 11:49 AM)Gamo Wrote:  Example: X^3 - 6X^2 + 11X - 6 = 0

6 CHS ENTER
11 ENTER
6 CHS ENTER A display 0

The last ENTER is wrong, it has to be removed.
But OK, the input order here seems to be a ENTER b ENTER c.

(06-09-2018 11:49 AM)Gamo Wrote:  B Display 3 R/S 0.25 // D is greater than or equal zero go ahead with R/S

The returned 3 is the first root. But what is "0,25"?

(06-09-2018 11:49 AM)Gamo Wrote:  Example: X^3 - 4X^2 + 8X - 8 = 0

8 CHS ENTER
8 ENTER
4 CHS A Display 0

?!? Now the input order is c ENTER b ENTER a ?!?

So there are three variations in your post:

c ENTER b ENTER c
a ENTER b ENTER c
c ENTER b ENTER a

Gamooo.... ;-)

Dieter
Find all posts by this user
Quote this message in a reply
06-09-2018, 10:34 PM
Post: #8
RE: (11C) CUBIC EQUATION
(06-09-2018 07:50 PM)Dieter Wrote:  
(06-09-2018 11:49 AM)Gamo Wrote:  B Display 3 R/S 0.25 // D is greater than or equal zero go ahead with R/S

The returned 3 is the first root. But what is "0,25"?

Most probably some sort of discriminant, either of the original cubic, some reduced cubic (if that's how he's computing the root, I didn't read the code) or of the quadratic after deflation. If it is >=0 the remaining two roots are real, else they are complex.

I'd bet it's the discriminant of the deflated quadratic, i.e., the equivalent of b^2-4*a*c in the classical formula for the roots of a quadratic equation (not the a,b,c he uses in his instructions).

V.
.

  
All My Articles & other Materials here:  Valentin Albillo's HP Collection
 
Visit this user's website Find all posts by this user
Quote this message in a reply
06-10-2018, 02:42 AM
Post: #9
RE: (11C) CUBIC EQUATION
Thank You Dieter and Valentin

Updated the instruction and usage.

Thanks again

Gamo
Find all posts by this user
Quote this message in a reply
06-10-2018, 07:15 AM (This post was last modified: 06-10-2018 09:54 AM by Dieter.)
Post: #10
RE: (11C) CUBIC EQUATION
(06-09-2018 10:34 PM)Valentin Albillo Wrote:  Most probably some sort of discriminant, either of the original cubic, some reduced cubic (if that's how he's computing the root, I didn't read the code) or of the quadratic after deflation. If it is >=0 the remaining two roots are real, else they are complex.

So it can't be the discriminant of the original cubic equation (here D<0 means three real roots). But it looks like it's indeed some discriminant of a reduced quadratic.

(06-10-2018 02:42 AM)Gamo Wrote:  Updated the instruction and usage.

We're not finished yet. ;-)

The instructions say
"[C] ---> Complex Roots // [X<>Y] (u in Y) and (v in X) "

What is u, what is v? I guess these are the real and imaginary parts of the solution. But which is which? Gamo, nobody can know what you mean by "u" or "v". It is not even clear if after the [R/S] the real or imaginary part is displayed !

So it should better read:

[C] ---> Complex Roots
The imaginary part of the two complex conjugate roots is displayed.
Press [X<>Y] to see the real part.

Or vice versa – don't know which is which. ;-)

But now for the most important part: I still think the program is not correct !
After LBL 1 the content of R4 is added in the calculation. But R4 has no defined value at this point. There is nothing that has been stored there before. Gamo, please check the program again. Take a look at the original listing in the 29C Applications Book. The program does not work for me. The results of your examples only appear if I store –7 in R4 (first example) or –8 (second example). There must be some code missing. Maybe you can show the original 29C program?

Try it yourself: clear the program in your 11C and re-enter it using the listing you posted here. Then try the examples.

By the way, you may adjust the program so that it automatically continues with C if the discriminant is < 0. I understand the code correctly it could be done this way:

Code:
...
LBL 3
RCL 5
R/S      <=== this R/S may be removed
x<0?
GTO C
...
LBL C
CHS
SQRT
PSE
X<>Y
RTN

The added modification at LBL C would show the imaginary part (PAUSE) and then the real part of the complex solution.

Now both the three real solutions as well as the real + complex ones are displayed without the user having to press additional keys. So the R/S that follows LBL 1 (which shows the discriminant) can/should be removed. Simply press [B] to get the first root, then once or twice [R/S] for the others. The user directly realizes whether it's two real or one conjugate complex solution because the latter is displayed with a PSE instruction.

Or simply try the direct solution program I suggested. This runs in a few seconds. ;-)

Dieter
Find all posts by this user
Quote this message in a reply
06-10-2018, 02:36 PM (This post was last modified: 06-10-2018 03:21 PM by Gamo.)
Post: #11
RE: (11C) CUBIC EQUATION
Hello Dieter
Somehow the program I listed seem to work.
Under X>Y there is one STO 4 follow by X<0 then branch to LBL 1

For now I provide the clips to show that this program work.
Two cubic equations shown in the description.

YouTube link Smile
https://youtu.be/c42KvUDk7as

Gamo
Find all posts by this user
Quote this message in a reply
06-10-2018, 03:34 PM (This post was last modified: 06-10-2018 06:51 PM by Dieter.)
Post: #12
RE: (11C) CUBIC EQUATION
(06-10-2018 02:36 PM)Gamo Wrote:  Somehow the program I listed seem to work.

Did you really re-enter it completely from the listing in your first post?
Look at this:

Code:
LBL 1
RCL 7
RCL 6
x
RCL 4    <=== here R4 is added although nothing has been stored there before!
+
STO 5
RCL 4   <=== here R4 is recalled once again although nothing has been stored there before
X=Y
GTO 3

(06-10-2018 02:36 PM)Gamo Wrote:  Under X<0 there is one STO 4 which follow by another conditional then branch to LBL 1

There is only one X<0? test in the whole program. (BTW I could hardly find it because instead of the "<" there is some other strange symbol).

Look at the point with the X<0? test that you mentioned. This is the only place where X<0? appears in the program:

Code:
RCL 5
STO 4
RCL 8
RCL 6
x
X<0?    <=== now with a correct "<" sign
GTO 1
GTO 2
LBL 3
...

Here you said:
(06-10-2018 02:36 PM)Gamo Wrote:  Under X<0 there is one STO 4 which follow by another conditional then branch to LBL 1

As you can see there is no STO 4 below the X<0? test. And there also is no other conditional test following these steps.

EDIT: I see you have changed your post.
Now it says: "Under X>Y there is one STO 4 follow by X<0 then branch to LBL 1"

There is only one x>y? test, so you seem to refer to this part of the code:

Code:
ABS 
RCL 0
X>Y
GTO 3
RCL 5
STO 4
RCL 8
RCL 6
x
X<0
GTO 1

Yes. there is a STO 4. But this is AFTER R4 has been recalled two times before – without anything having been stored there previously ! R4 is already recalled before this, four and seven steps after LBL 1. But at this point nothing has been stored there before.

(06-10-2018 02:36 PM)Gamo Wrote:  For now I provide the clips to show that this program work.
Two cubic equations shown in the description.

It may well be that the program in your calculator is working. But that does not say anyting about the listing in the first post.

Once again: Please, Gamo, check the listing carefully. It does not work the way you posted it.

By the way: what does the program return for x³ – x² – x ?
According to the listing this should throw an Error. This could be avoided with a slight change.

Dieter
Find all posts by this user
Quote this message in a reply
06-10-2018, 08:50 PM
Post: #13
RE: (11C) CUBIC EQUATION
(06-10-2018 07:15 AM)Dieter Wrote:  So it can't be the discriminant of the original cubic equation (here D<0 means three real roots). But it looks like it's indeed some discriminant of a reduced quadratic.

Great insight on your part but that's exactly what I said in the very next paragraph of my previous post, which you choose not to quote for whatever reasons. I'll save you the trouble, here you are:

Valentin Albillo Wrote:I'd bet it's the discriminant of the deflated quadratic, i.e., the equivalent of b^2-4*a*c in the classical formula for the roots of a quadratic equation (not the a,b,c he uses in his instructions).

Also, I don't appreciate your attitude towards Gamo. It's great that you're helping him refine his program and correct the perceived programming errors to try and get a working program, very nice of you, but in my opinion you're doing it in a condescending, patronizing way which is completely out of place. Surely you can help him without that much smugness on your part.

There.
V.
.

  
All My Articles & other Materials here:  Valentin Albillo's HP Collection
 
Visit this user's website Find all posts by this user
Quote this message in a reply
06-10-2018, 09:17 PM
Post: #14
RE: (11C) CUBIC EQUATION
(06-10-2018 08:50 PM)Valentin Albillo Wrote:  Great insight on your part but that's exactly what I said in the very next paragraph of my previous post, which you choose not to quote for whatever reasons. I'll save you the trouble, here you are:

Yes, sure. I simply confirmed what you said. Shouldn't I do so?

(06-10-2018 08:50 PM)Valentin Albillo Wrote:  Also, I don't appreciate your attitude towards Gamo. It's great that you're helping him refine his program and correct the perceived programming errors to try and get a working program, very nice of you, but in my opinion you're doing it in a condescending, patronizing way which is completely out of place. Surely you can help him without that much smugness on your part.

I admit that at times I am a bit impatient on such topics. @Gamo: please let me know if my corrections and suggestions are really "completely out of place". Of course this is not my intention.

Regarding the subject (i.e. the program) see my next post.

Dieter
Find all posts by this user
Quote this message in a reply
06-10-2018, 09:35 PM (This post was last modified: 06-11-2018 07:55 AM by Dieter.)
Post: #15
RE: (11C) CUBIC EQUATION
(06-10-2018 02:36 PM)Gamo Wrote:  Somehow the program I listed seem to work.
Under X>Y there is one STO 4 follow by X<0 then branch to LBL 1

Gamo, I now have looked a bit closer at all this. And it looks a bit clearer now.

1. I had an error in the program I entered into the 15C emulator. One of the test command was wrong. Sorry, my bad. So I corrected this.

2. After the correction the program still required a substantial time even on the emulator. On a real 15C this would mean several minutes before a result is returned. If at all. It turned out that this depends on the initial value of R4. Which confirms my idea that it cannot be right that R4 is not initialized in the program: Depending on the initial R4 value the program runs fine, or may take a very long time.

R4 seems to hold some kind of initial guess for the first root. Now if R4 happens to hold a value like 10^9 and the root is near 1... I just stopped such an iteration which seemed to run endlessly.

So R4 should be initialized. There are two options now:

3a: The program as listed has an error and there actually is some code that initializes R4, but it is missing in the listing. That's why I asked you to carefully compare the code with the original listing.

3b: The original 29C program indeed has no initializsation code for R4. That's bad programming, but of course it's not your fault. In this case one could start with R4=0 (instead of what happens to be stored in R4 on program start). This can be done with a simple STO 4 after the final CLX at the end of the LBL A routine.

I modified the program this way and for the cases I tested it seems to run fine. At least there were no cases where the program required an exceptionally long execution time.

Dieter
Find all posts by this user
Quote this message in a reply
06-11-2018, 02:16 AM (This post was last modified: 06-11-2018 07:50 AM by Gamo.)
Post: #16
RE: (11C) CUBIC EQUATION
Dieter
Thank You for reviewing this program I noticed that this routine take a long time to run and must be something wrong in the original coding.
I try this on HP-15C App on Android and it run extremely fast with correct answer using the original code.

Here is the original program: CUBIC EQUATION

It does so by extracting the first root, performing synthetic division,
and solving the resulting quadratic equation (ref: HP-19C/HP-29C Application Book, p.6)

USER INSTRUCTIONS:
1. Initialize f [REG]
2. 10^-4 [STO] 0
3. STORE Coefficients: a [STO 1] b [STO 2] c [STO 3]
4. RUN: [GSB] 1 // 1st root // [R/S] D
5a. If D greater than or equal to zero // [R/S] 2nd root [R/S] 3nd root
5b. If D less than zero // [GSB] 2 (v) [X<>Y] (u)

Program: CUBIC EQUATION
Code:

LBL 1
RCL 3
RCL 3
ABS
/
STO 6
LSTx
RCL 1
ABS
+
RCL 0
LBL 0
10
x
X<=Y
GTO 0
STO 7
LBL 9
10
STO / 7
RCL 6
CHS
STO 6
LBL 8
RCL 7
RCL 6
x
RCL 4
X=Y
GTO 7
RCL 5
RCL 1
+
RCL 5
x
RLC 2
+
RCL 5
x
RCL 3
+
STO 8
ABS
RCL 0
X>Y
GTO 7
RCL 5
STO 4
RCL 8
RCL 6
x
X<0
GTO 8
GTO 9
LBL 7
RCL 5
R/S
RCL 1
+
CHS
STO 1
2
/
ENTER
X^2
RCL 1
CHS
RCL 5
x
RCL 2
+
STO 3
-
R/S
SQRT
X<>Y
ABS
+
RCL 1
ENTER
ABS
/
x
R/S
RCL 3
X<>Y
/
RTN
LBL 2
CHS
SQRT
R/S

Remark:
Under user instruction the first step said Initialize f [REG] is this to make sure that each RUN register R4 is clear and that start R4 with zero ?

Gamo Smile
Find all posts by this user
Quote this message in a reply
06-11-2018, 07:04 AM (This post was last modified: 06-11-2018 07:06 AM by Dieter.)
Post: #17
RE: (11C) CUBIC EQUATION
(06-11-2018 02:16 AM)Gamo Wrote:  Thank You for reviewing this program I noticed that this routine take a long time to run and must be something wrong in the original coding.
...
USER INSTRUCTIONS:
1. Initialize f [REG]

There we are:
This initializes R4 (and all other registers) with zero.
So the suggested CLX STO 4 in my previous post seems to be a good idea.

(06-11-2018 02:16 AM)Gamo Wrote:  2. 10^4 [STO] 0

10^4 ?
Your program stores 10^–4 instead. Which seems to make more sense.

(06-11-2018 02:16 AM)Gamo Wrote:  Program: CUBIC EQUATION
Code:
...
LBL 8
RCL 7
RCL 6
x
RCL 4
X=Y
GTO 7
...

Here your program differs from the listing:

Code:
RCL 7
RCL 6
x
RCL 4   // these three lines
+       // are not in the
STO 5   // 29C listing
RCL 4
X=Y
GTO 3

(06-11-2018 02:16 AM)Gamo Wrote:  Under user instruction the first step said Initialize f [REG] is this to make sure that each RUN register R4 is clear and that start R4 with zero ?

Yes, exactly.
That's why you should add a STO 4 after the CLX in the LBL A part.
I did so and it seems to work.

So the issue now seems to be resolved.

Dieter
Find all posts by this user
Quote this message in a reply
06-11-2018, 08:19 AM (This post was last modified: 06-11-2018 10:33 AM by Gamo.)
Post: #18
RE: (11C) CUBIC EQUATION
Dieter
Thank You for clarify this specific program.
I have make change to this program, took off the three line that didn't match the original program, added negative 4 (10^-4) and added the CLx STO 4 to LBL A
You program also work very good I like it when answer with complex result show both answer with PAUSE function and program run much faster.

Valentin
Thank You for more detail about discriminant portion of the instruction.
And thanks of your sincere.

Gamo

Remark:
After I took off the three line and test it somehow program keep running none stop.
So I put that three line back and it work.
Seem like after adding the [CLx] [STO] 4 to [LBL] A somewhat the program run a bit faster than before.
Find all posts by this user
Quote this message in a reply
06-11-2018, 08:19 PM (This post was last modified: 06-11-2018 08:23 PM by Dieter.)
Post: #19
RE: (11C) CUBIC EQUATION
(06-11-2018 08:19 AM)Gamo Wrote:  After I took off the three line and test it somehow program keep running none stop.
So I put that three line back and it work.

Sure. These three lines are required, if you remove them the program will not work. The lines are missing in the 29C listing, that's an error.

(06-11-2018 08:19 AM)Gamo Wrote:  Seem like after adding the [CLx] [STO] 4 to [LBL] A somewhat the program run a bit faster than before.

This can be expected. But it still is not running very fast. Here is why:

I tried to understand how the program works. The general method is obvious: Determine one (real) root of the cubic equation, then calculate the other two from this. The first root is calculated by iteration. Here the program uses a simple but not very effective method: the value is determined digit by digit!

Here is an example: For the first root of x³–x²–x+0,5 at –0,8546376797 the program generates the following sequence:

Code:
-0,1
-0,2
-0,3
-0,4
-0,5
-0,6
-0,7
-0,8
-0,9
-0,89
-0,88
-0,87
-0,86
-0,85
-0,851
-0,852
-0,853
-0,854
-0,855
-0,8549
-0,8548
-0,8547
-0,8546
-0,85461
-0,85462
-0,85463
-0,85464
-0,854639
-0,854638
-0,854637
-0,8546371
-0,8546372
-0,8546373
-0,8546374
-0,8546375
-0,8546376
-0,8546377
-0,85463769
-0,85463768

The iteration exits if |f(x)| drops below 1E-4 (or 1E-8 in my version below) or if the last two approximations match (because a change beyond the 10th digit would occur). For the x=–0,8546... example this requires about 40 iterations for f(x)<1E–8.

On the other hand, if the root has only a few significant digits, for instance if it's an integer like 1 or 3 in the first example, the iteration finds the first root quite fast. But if the value needs 9 or 10 digits to get f(x) below 1E–8 the average iteration count is 40 loops or more. That's why it takes so long. And that's probably also why the original error threshold was as high as 1E-4. This gives a mid-precision approximation to the first root in about half the time required for 1E–8. But since the other two roots are calculated from the first one they are also not exact, but just more or less accurate approximations. In other words: at least in the original version the program mostly does not give exact or near exact results, but solutions that may only have 5 correct digits.

Here is a modified version of the program.
  • The program now handles the general case ax³+bx²+cx+d
  • Unlike the original version, the constant term now may also be zero.
  • Input is a [ENTER] b [ENTER] c [ENTER] d
  • The f(x) threshold for the first root has been reduced to 1E-8.
    This way the returned roots are more accurate.
  • The program now returns the solutions directly.
    There is no need to press different keys depending on the sign of D.
  • Because of this the discriminant D is no longer displayed.
  • The 2nd/3rd complex roots are displayed as "imaginary part" (PAUSE), real part.
  • The 2nd/3rd real roots are displayed with R/S.
The listing:

Code:
001  LBL A     // input: a ENTER b ENTER c ENTER d
002  STO 3
003  R↓
004  STO 2
005  R↓
006  STO 1
007  R↓
008  STO÷1
009  STO÷2
010  STO÷3
011  EEX
012  CHS
013  8         // lower error threshold than the original 1E-4
014  STO 0
015  CLx
016  STO 4
017  RCL 3
018  STO 6
019  ABS
020  x≠0?      // TEST 0 on 15C
021  STO÷6
022  RCL 1
023  ABS
024  +
025  RCL 0
026  LBL 0
027  1
028  0
029  x
030  x≤y?
031  GTO 0
032  STO 7
033  LBL 2
034  1
035  0
036  STO÷7
037  RCL 6
038  CHS
039  STO 6
040  LBL 1
041  RCL 7
042  RCL 6
043  x
044  RCL 4
045  +
046  STO 5
047  RCL 4
048  x=y?      // TEST 5 on 15C
049  GTO 3
050  RCL 5
051  RCL 1
052  +
053  RCL 5
054  x
055  RCL 2
056  +
057  RCL 5
058  x
059  RCL 3
060  +
061  STO 8
062  ABS
063  RCL 0
064  x>y?      // TEST 7 on 15C
065  GTO 3
066  RCL 5
067  STO 4
068  RCL 8
069  RCL 6
070  x
071  x<0?      // TEST 2 on 15C
072  GTO 1
073  GTO 2
074  LBL 3
075  RCL 5
076  R/S       // first real root
077  RCL 5
078  RCL 1
079  +
080  CHS
081  STO 1
082  2
083  ÷
084  ENTER
085  X^2
086  RCL 1
087  CHS
088  RCL 5
089  x
090  RCL 2
091  +
092  STO 3
093  -         // = discriminant D
094  x<0?      // TEST 2 on 15C
095  GTO 4     // for D<0 go to complex roots
096  √X
097  X<>Y
098  ABS
099  +
100  RCL 1
101  ENTER
102  ABS
103  ÷
104  x
105  STO 5
106  R/S       // second real root
107  RCL 3
108  RCL 5
109  ÷
110  RTN       // third real root
111  LBL 4
112  CHS       // 2nd & 3rd complex roots
113  √X
114  PSE       // show imaginary part
115  X<>Y
116  RTN       // show real part

Usage:

Enter the four coefficients of the equation ax³ + bx² + cx + d:
a [ENTER] b [ENTER] c [ENTER] d

Press [A]
This returns the first root, calculated by iteration.

Press [R/S]
- If the other roots are complex: "imaginary part" (PAUSE) and real part are returned.
- Otherwise the second real root is returned, [R/S] then displays the third one.

Examples:

x³ – x² – x + 1/2
1 [ENTER] 1 [CHS] [ENTER] 1 [CHS] [ENTER] 0,5
  [A]  => –0,854637680
[R/S] =>   1,451605963
[R/S] =>   0,403031717

x³ + x² – x
1 [ENTER] 1 [ENTER] 1 [CHS] [ENTER] 0
  [A]  =>   0,000000000
[R/S] => –1,618033989
[R/S] =>   0,618033989

2x³ + 4x² + 4x + 2
2 [ENTER] 4 [ENTER] 4 [ENTER] 2
  [A]  => –1,000000000
[R/S] => "0,866025404"  –0,500000000
So that's –0,5 ±0,866 i.

Dieter
Find all posts by this user
Quote this message in a reply
06-12-2018, 07:02 AM (This post was last modified: 06-12-2018 07:05 AM by Gamo.)
Post: #20
RE: (11C) CUBIC EQUATION
The original program that Dieter make the update turn out to be a lot better than the original version. The computation speed on most problem run faster and some problem run slow but still give answer.

For the advance version that used the trigonometry functions of COS and COS^-1 is much better overall and this is the highly recommend one to solve for Cubic Equation on HP-11C or 15C.

Thank You

Gamo
Find all posts by this user
Quote this message in a reply
Post Reply 




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