HP Forums

Full Version: (12C Platinum) Cubic Equation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
ALG mode program solution of a Cubic Equation by Newton's Method.

f(x) = aX^3 + bX^2 + cX + d = 0

Successive approximations to a root are found by

Xi+1 = 2aXi^3 + bXi^2 -d / 3aXi^2 + 2bXi + c

Guess X0

------------------------------------------------------

Remark:

This program is use to solve for "REAL ROOT"

-------------------------------------------------------
Procedure:

f PRGM // Each new program or GTO 000

a [R/S] b [R/S] c [R/S] d [R/S] X0 [R/S]

Display shown each successive approximation until root is found.

If more than one Real Solutions enter another guess and [R/S]

Maximum of 3 Real Root.

-------------------------------------------------------
Example:

x^3 - 4x^2 + 6x - 24 = 0

f [PRGM] or [GTO] 000
1 [R/S]
4 [CHS] [R/S]
6 [R/S]
24 [CHS] [R/S]
20 [R/S] // My starting guess
Display successive approximation search and stop when root is found.

Answer Display 4

X=4

---------------------------------------------
-2x^3 + 3x^2 + 4x - 5 = 0

f [PRGM] or [GTO] 000
2 [CHS] [R/S]
3 [R/S]
4 [R/S]
5 [CHS] [R/S]

10 [R/S] ...............display 1.8508
0 [R/S] .................display 1
5 [CHS] [R/S] ..........display -1.3508
-----------------------------------------------
Program: ALG Mode
Code:

STO 0 R/S
STO 1 R/S
STO 2 R/S
STO 3 R/S
STO 4 x 2 x RCL 0 + RCL 1 x RCL 4 X^2 - RCL 3 ÷
(RCL 4 x 3 x RCL 0 + (RCL 1 x 2) x RCL 4 + RCL 2) = 
STO 5 - RCL 4 =
X=0
GTO 049
RCL 5
PSE
GTO 009
RCL 5
GTO 008

Gamo
Hi Gamo

It might be better if iteration formula is *not* simplified:

X(i+1)= Xi - (aXi^3 + bXi^2 + cXi + d) / (3aXi^2 + 2bXi + c)

Simplified form may introduce subtraction cancellation error on the *last* iteration.

BTW, Professor Kahan had a systemetic way to get a good guess X0:
https://apps.dtic.mil/dtic/tr/fulltext/u2/a206859.pdf, page 5

Using your examples:

f(x) = x^3 - 4x^2 + 6x - 24
f(4/3) = -2.7475³, f'(4/3) = 2/3 ≥ 0
guess = 4/3 - (-1)(2.7475) = 4.0809
x = 4.0809 → 4.0023 → 4.0000

f(x) = -2x^3 + 3x^2 + 4x - 5
f(1/2)/-2 = 1.0772³, f'(1/2)/-2 = -1.6583² < 0
guess = 1/2 - 1.324718 (1) max(1.0772, 1.6583) = -1.6968
x = -1.6968 → -1.4145 → -1.3536 → -1.3508

Update:
if we need the other roots, Kahan's algorithm suggested this:
Deflate cubic to quadratic: a X² + e X + f = 0
IF |x³| > |d/a| THEN (f=-d/x, e=(f-c)/x) ELSE (e=ax+b, f=ex+c)
Another way to get good guess is "look" at shape of curve

Example 1: x^3 - 4x^2 + 6x - 24 = 0
x((x-2)² + 2) = 24 ~ 2.88³

Any guess > 2 work. Say, guess = 3
X = 3 -> 4.6667 -> 4.1220 -> 4.0051 -> 4.0000

Example 2: -2x^3 + 3x^2 + 4x - 5 = 0
x^3 - 1.5x^2 - 2x + 2.5 = 0
x((x-0.75)² - 2.5625) = -2.5 ~ -1.36³

If RHS is 0, we have roots -0.8508, 0, 2.3508, with LHS interval signs = - + - +
Any guess < -0.8508 work. Say, guess = -1.5
X = -1.5 -> -1.3649 -> -1.3509 -> -1.3508

Since LHS interval (0, 2.3508) also is negative, eqn might have 3 real roots.
Indeed it does, 3 roots = -1.3508, 1.0000, 1.8508
Or then use the built-in polynomial solver with this program:
Code:
01-       1    1
02-      34    x<>y
03-      24    ∆%
04-45,43 31    RCL PSE
05-      34    x<>y
06-      33    R↓
07-      25    %
08-      40    +

Examples:

\(x^3 - 4x^2 + 6x - 24 = 0\)

Enter coefficients:

1 CF0
-4 CFj
6 CFj
-24 CFj

Enter guess:

20 R/S
4.0000

\(-2x^3 + 3x^2 + 4x - 5 = 0\)

Enter coefficients:

-2 CF0
3 CFj
4 CFj
-5 CFj

Enter guess:

5 R/S
1.8508

Enter guess:

0.5 R/S
1.0000

The advantage is that the same program can be used for higher order polynomials.

Cheers
Thomas

PS: Cf. HP-12C’s Serendipitous Solver
Thanks Thomas Klemm

The build in Polynomial Solver work very good.

On Original 12C work well but
On 12C Platinum need to adjust guesses differently.
Your second example with two real roots.

First guess 5 R/S display 1
Second guess 0.5 R/S display 1

Remark: second guess needed to be 3 to get answer
For 1.8508

Can the guess be negative integer?
I try -1 and Error 5 shown.

Gamo
(02-05-2019 04:24 AM)Gamo Wrote: [ -> ]Can the guess be negative integer?

It appears that you can't solve for negative roots.
But you can just do a transformation: \(x \rightarrow -x\)
This transforms the equation to:

\(2x^3 + 3x^2 - 4x - 5 = 0\)

Enter coefficients:

2 CF0
3 CFj
-4 CFj
-5 CFj

Enter guess:

1 R/S
1.3508


And then transform the result back again to get the solution of the original equation:

-1.3508


Cheers
Thomas
(02-05-2019 04:24 AM)Gamo Wrote: [ -> ]Remark: second guess needed to be 3 to get answer
For 1.8508

That's interesting. Overshooting this solution happens only with initial guess 10.
So it seems that the algorithm is implemented differently.
(02-04-2019 08:13 PM)Thomas Klemm Wrote: [ -> ]\(x^3 - 4x^2 + 6x - 24 = 0\)

Enter coefficients:

1 CF0
-4 CFj
6 CFj
-24 CFj

Enter guess:

20 R/S
4.0000

Above entered coefficients order are good for root finding, and to evaluate polynomial. Smile

Example, with above entered polynomial, calculate f(X = 12.34)

1 Enter 12.34 [Δ%] [i] ; showed rate of 1134%, X = 1 + i

NPV 0 PMT FV CHS ; showed 1320.018504 <-- f(X)

Confirm with Horners rule:

12.34 Enter Enter Enter RCL 0
* RCL 1 +
* RCL 2 +
* RCL 3 + ; showed 1320.018504

Doing the same with reversed ordered entry is not as accurate:

24 CHS CF0
6 CFj
4 CHS CFj
1 CFj

1 Enter 12.34 [1/x] [Δ%] [i] ; showed rate of -91.89627229%, X = 1/(1 + i)

NPV ; showed 1320.018507, +3 ULP error
Reference URL's