(11C) CUBIC 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: (11C) CUBIC EQUATION (/thread-10891.html) Pages: 1 2 |
(11C) CUBIC EQUATION - Gamo - 06-09-2018 11:49 AM 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:
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 RE: (11C) CUBIC EQUATION - Gamo - 06-09-2018 12:34 PM 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 RE: (11C) CUBIC EQUATION - Dieter - 06-09-2018 12:54 PM (06-09-2018 12:34 PM)Gamo Wrote: Thanks for the look up I have make update to the instruction procedure. 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: 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 RE: (11C) CUBIC EQUATION - rprosperi - 06-09-2018 01:00 PM (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. RE: (11C) CUBIC EQUATION - Massimo Gnerucci - 06-09-2018 01:18 PM (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.... Yeah, LOL! RE: (11C) CUBIC EQUATION - Dieter - 06-09-2018 06:05 PM (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 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 RE: (11C) CUBIC EQUATION - Dieter - 06-09-2018 07:50 PM 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 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 ?!? 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 RE: (11C) CUBIC EQUATION - Valentin Albillo - 06-09-2018 10:34 PM (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 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. . RE: (11C) CUBIC EQUATION - Gamo - 06-10-2018 02:42 AM Thank You Dieter and Valentin Updated the instruction and usage. Thanks again Gamo RE: (11C) CUBIC EQUATION - Dieter - 06-10-2018 07:15 AM (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: ... 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 RE: (11C) CUBIC EQUATION - Gamo - 06-10-2018 02:36 PM 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 https://youtu.be/c42KvUDk7as Gamo RE: (11C) CUBIC EQUATION - Dieter - 06-10-2018 03:34 PM (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 (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 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 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. 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 RE: (11C) CUBIC EQUATION - Valentin Albillo - 06-10-2018 08:50 PM (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. . RE: (11C) CUBIC EQUATION - Dieter - 06-10-2018 09:17 PM (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 RE: (11C) CUBIC EQUATION - Dieter - 06-10-2018 09:35 PM (06-10-2018 02:36 PM)Gamo Wrote: Somehow the program I listed seem to work. 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 RE: (11C) CUBIC EQUATION - Gamo - 06-11-2018 02:16 AM 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:
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 RE: (11C) CUBIC EQUATION - Dieter - 06-11-2018 07:04 AM (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. 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 Here your program differs from the listing: Code: RCL 7 (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 RE: (11C) CUBIC EQUATION - Gamo - 06-11-2018 08:19 AM 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. RE: (11C) CUBIC EQUATION - Dieter - 06-11-2018 08:19 PM (06-11-2018 08:19 AM)Gamo Wrote: After I took off the three line and test it somehow program keep running none stop. 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 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.
Code: 001 LBL A // input: a ENTER b ENTER c ENTER d 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 RE: (11C) CUBIC EQUATION - Gamo - 06-12-2018 07:02 AM 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 |