(11C) CUBIC EQUATION
|
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. 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 |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)