Cubic function

From testwiki
Revision as of 12:27, 24 October 2024 by imported>ThaniosAkro (Review of complex math)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

It is not the purpose of this page to repeat good information available elsewhere. However, it seems to the author that other descriptions of the cubic function are more complicated than they need to be. This page attempts to demystify elementary but essential information concerning the cubic function.

Objective

Template:RoundBoxTop

  • Present cubic function and cubic equation.
  • Introduce the concept of roots of equal absolute value.
  • Show how to predict and calculate equal roots, techniques that will be useful when applied to higher order functions.
  • Simplify the depressed cubic.
  • Simplify Vieta's substitution.
  • Review complex numbers as they apply to a complex cube root.
  • Present cubic formula simplified.
  • Show that the cubic equation is effectively solved when at least one real root is known.
  • Use Newton's Method to calculate one real root.
  • Show that the cubic equation can be solved with high-school math.

Template:RoundBoxBottom

Lesson

Template:RoundBoxTop

Introduction

The cubic function is the sum of powers of x from 0 through 3:

y=f(x)=ax3+bx2+cx1+dx0

usually written as:

y=f(x)=ax3+bx2+cx+d.

If d==0 the function becomes x(ax2+bx+c).

Within this page we'll say that:

  • both coefficients a,d must be non-zero,
  • coefficient a must be positive (simply for our convenience),
  • all coefficients must be real numbers, accepting that the function may contain complex roots.

The cubic equation is the cubic function equated to zero:

ax3+bx2+cx+d=0.

Roots of the function are values of x that satisfy the cubic equation.

Because all coefficients must be real numbers, the cubic function must have 3 real roots or exactly 1 real root.

Other combinations of real and complex roots are possible, but they produce complex coefficients.

Characteristics of cubic functions

Coefficient c missing

Template:RoundBoxTop

File:0222cubic2.png

If coefficient c is missing, the cubic function becomes y=ax3+bx2+d, and

y=3ax2+2bx=x(3ax+2b).

For a stationary point y=x(3ax+2b)=0.

When coefficient c is missing, there is always a stationary point at x=0. Template:RoundBoxBottom

x = f(y)

Template:RoundBoxTop

File:0222cubic1.png

The cubic function may be expressed as x=ay3+by2+cy+d.

Unless otherwise noted, references to "cubic function" on this page refer to function of form y=ax3+bx2+cx+d. Template:RoundBoxBottom

Coefficient a negative

Template:RoundBoxTop

File:0410cubic01.png
Graph of cubic function with coefficient a negative.
There is no absolute maximum or absolute minimum.

Coefficient a may be negative as shown in diagram.


As abs(x) increases, the value of f(x) is dominated by the term ax3.

When x has a very large negative value, f(x) is always positive.

When x has a very large positive value, f(x) is always negative.


Unless stated otherwise, any reference to "cubic function" on this page will assume coefficient a positive. Template:RoundBoxBottom

Sum of roots 0

Template:RoundBoxTop

File:0506cubic01.png
Graph of cubic function with coefficient b missing.
Sum of roots is 0.
Yaxis compressed for clarity.

When sum of roots is 0, coefficient b=0.


In the diagram, roots of f(x) are 8,2.4,5.6.

Sum of roots =0.

Therefore coefficient b=0. Template:RoundBoxBottom

Ratio of stationary points to roots

Template:RoundBoxTop

File:0618cubic01.png
Graph of cubic function with 2 stationary points.
Ratio of r to x2=33
Ratio of q to x1=33

Stationary points relative to roots:


Consider cubic function: f(x)=ax3cx.

Roots of function are: 0, x1,x2 = ±ca.

Derivative of function: g(x)=3ax2c.

Stationary points of function are roots of g(x): q,r = 0±024(3a)(c)6a or ±3ac3a.

Ratio of r to x2 = 3ac3aac  = 33.

Ratio of q to x1 = 3ac3aac  = 33. Template:RoundBoxBottom

s = 2r

Template:RoundBoxTop

File:0618cubic02.png
Graph of cubic function with 2 stationary points.
(q,f(q)),(r,f(r)) are stationary points.
f(s)=f(q).
Therefore, s=2r.

Equation of red curve in diagram: y=f(x)=ax3cx, where a,b,c=a,0,c

Aim of this section is to calculate s so that f(s)=f(q).

Associated quadratic when x=q:

A=a

B=Aq+b

C=Bq+c

q is a root of this function. Divide Ax2+Bx+C by xq.

Quotient is Ax+Aq+B.

Remainder is Aq2+Bq+C which equals 0.

Ax+Aq+B=0. Therefore:

s=AqBA=AqAqA=2q.

r=q. Therefore s=2(r)=2r. Template:RoundBoxBottom

Function as product of 3 linear functions

The function may be expressed as:

y=(xp)(xq)(xr) where p,q,r are roots of the function, in which case

y=x3(p+q+r)x2+(pq+qr+rp)xpqr where: a=1; b=(p+q+r); c=pq+qr+rp; d=pqr

Solving the cubic equation means that, given a,b,c,d, at least one of p,q,r must be calculated.

Given b,c,d, I found that r can be calculated as:

r8+2br7+(bb+3c)r6+(4bc+2d)r5+(bbc+2bd+3cc)r4+(2bcc+4cd)r3+(2bcd+c3+dd)r2+2ccdr+cdd=0

This approach was not helpful.

Function as product of linear function and quadratic

When p is a root of the function, the function may be expressed as:

(xp)(Ax2+Bx+C) where

A=a; B=Ap+b; C=Bp+c.

When one real root p is known, the other two roots may be calculated as roots of the quadratic function Ax2+Bx+C. Template:RoundBoxTop f(x)=(xp)(ax2+Bx+C) =ax3+bx2+cx(ap3+bp2+cp).

When p is a root of the function, ap3+bp2+cp+d=0 or ap3+bp2+cp=d.

Therefore the expansion of (xp)(ax2+Bx+C) =ax3+bx2+cx(d) =ax3+bx2+cx+d.

Generally, if point (p,q) is any point on the curve and it is desired to calculate the other values of x that produce y=q, then:

ap3+bp2+cp+d=q or ap3+bp2+cp=qd and

f(x)=ax3+bx2+cx(ap3+bp2+cp) =ax3+bx2+cx(qd) =ax3+bx2+cx+dq.

When f(x)=0, ax3+bx2+cx+dq=0 and ax3+bx2+cx+d=q. Template:RoundBoxTop Let ax2+Bx+C=0 (1)

where B=ap+b;C=Bp+c.

If point (p,q) is any point on the curve of y=ax3+bx2+cx+d, then the solution of (1) provides the other values of x that produce y=q. Template:RoundBoxTop

File:0223cubic1.png
Graph of cubic function and associated quadratic function.
All values of x that produce f(x)=2.

An example:

Let f(x)=3x36x23x+228

It is known that point (1,2) satisfies y=f(x).

Let associated quadratic function =g(x)=3x23x68.

Roots of g(x), 1,2, show that when x=1 or x=2,f(x)=2. Template:RoundBoxBottom

General case

Template:RoundBoxTop For function allequal(), see isclose().

The following python code implements the functionality of this section:

# python code.

TwoRootsOfCubicDebug = 0

def TwoRootsOfCubic (abcd, x1) :
    '''
x2,x3 = TwoRootsOfCubic (abcd, x1)
f(x2) = f(x3) = f(x1)
If x1 is a root, then f(x2) = f(x3) = f(x1) = 0, and x2,x3 are roots.
x1 may be complex.
'''
    a,b,c,d = abcd
    B = a*x1 + b
    C = B*x1 + c
    disc = B*B - 4*a*C
    almostZero = 1e-15
    if abs(disc) <  almostZero :
        x2 = x3 = -B/(2*a)
    else :
        if isinstance (disc, complex) or (disc > 0) :
            root = disc ** .5
        else :
            root = ((-disc) ** .5)*1j
        x2 = (-B - root)/(2*a)
        x3 = (-B + root)/(2*a)

    if not TwoRootsOfCubicDebug : return x2,x3

    sum1,sum2,sum3 = [ (a*x*x*x + b*x*x + c*x + d) for x in (x1,x2,x3) ]
    print ('TwoRootsOfCubic ():')
    print ('    y = (',a,')xx + (',B, ')x + (', C, ')')
    print ('    for x1 =',x1,', sum1 =',sum1)
    print ('    for x2 =',x2,', sum2 =',sum2)
    print ('    for x3 =',x3,', sum3 =',sum3)
# sum1,sum2,sum3 should all be equal.
# In practice there may be small rounding errors.
# The following check allows for small errors, but flags
# errors that are not "small".
    allEqualDebug = 1
    allEqual((sum1,sum2,sum3))

    return x2,x3

The example above:

# python code.
print ( TwoRootsOfCubic ((3,-6,-3,22), 1) )
print ( TwoRootsOfCubic ((3,-6,-3,22), -1) )
print ( TwoRootsOfCubic ((3,-6,-3,22), 2) )
(-1.0, 2.0)
(1.0, 2.0)
(-1.0, 1.0)

When 1 root is known:

# python code.
print ( TwoRootsOfCubic ((1,-2,-5,6), 1) )
print ( TwoRootsOfCubic ((1,-3,-9,-5), 5) )
(-2.0, 3.0)
(-1.0, -1.0)

The method works with complex values:

# python code.
TwoRootsOfCubicDebug = 1
print ( TwoRootsOfCubic ((1,0,0,27), -3) )
print ( TwoRootsOfCubic ((1,9,31,39), -3+2j) )
TwoRootsOfCubic ():
    y = ( 1 )xx + ( -3 )x + ( 9 )
    for x1 = -3 , sum1 = 0
    for x2 = (1.5-2.598076211353316j) , sum2 = 0j
    for x3 = (1.5+2.598076211353316j) , sum3 = 0j
((1.5-2.598076211353316j), (1.5+2.598076211353316j))

TwoRootsOfCubic ():
    y = ( 1 )xx + ( (6+2j) )x + ( (9+6j) )
    for x1 = (-3+2j) , sum1 = 0j
    for x2 = (-3-2j) , sum2 = 0j
    for x3 = (-3+0j) , sum3 = 0j
((-3-2j), (-3+0j))

Notice complex coefficients: B,C=6+2j,9+6j.


Reporting an error:

print(TwoRootsOfCubic ((3,-6,-3,22), -4+3j))
TwoRootsOfCubic ():
    y = ( 3 )xx + ( (-18+9j) )x + ( (42-90j) )
    for x1 = (-4+3j) , sum1 = (124+486j)
    for x2 = (0.264468373145825-5.338376386119454j) , sum2 = (124.00000000000007+485.9999999999998j)
    for x3 = (5.735531626854176+2.338376386119455j) , sum3 = (124.00000000000017+486.0000000000003j)
allEqual()6: 2 values not close. (124.00000000000007+485.9999999999998j) , (124.00000000000017+486.0000000000003j)
    abs(a-b)   = 5.211723197616109e-13
    comparison = 5.015695365550026e-13
((0.264468373145825-5.338376386119454j), (5.735531626854176+2.338376386119455j))

Notice complex coefficients: B,C=18+9j,4290j. Template:RoundBoxBottom Template:RoundBoxBottom Template:RoundBoxBottom

Function defined by 4 points

Template:RoundBoxTop

File:0218cubic1.png
Figure 1. Cubic function defined by 4 points

Any 4 points on the curve may be used to define the function.

Because the cubic function contains 4 coefficients, 4 simultaneous equations are needed to define the function.

See Figure 1. The cubic function may be defined by any 4 unique points on the curve.

For example, let us choose the four points:

(4,2),(3,3),(2,3),(8,8)

Rearrange the standard cubic function to prepare for the calculation of a,b,c,d:

x3a+x2b+xc+1dy=0.

For function solveMbyN see "Solving simultaneous equations" .

Template:RoundBoxTop

# python code

points = ((-4,-2), (-3,3), (2,3), (8,8))

L11 = []

for point in points :
    x,y = point
    L11 += [[x*x*x, x*x, x, 1, -y]]

print (L11)

Template:RoundBoxTop

[[-27,  9, -3, 1, -3],     #
 [-64, 16, -4, 1,  2],     # matrix supplied to function solveMbyN() below.
 [  8,  4,  2, 1, -3],     # 4 rows by 5 columns.
 [512, 64,  8, 1, -8]]     #

Template:RoundBoxBottom

# python code

output = solveMbyN(L11)
print (output)

Template:RoundBoxTop

# 4 coefficients a, b, c, d:
(0.07575757575757577, -0.4545454545454544, -0.9848484848484848, 6.181818181818182)

Template:RoundBoxBottom The 4 coefficients above are in fact the values 566,3066,6566,40866. Template:RoundBoxBottom Cubic function defined by the 4 points (4,2),(3,3),(2,3),(8,8) is y=5x330x265x+40866. Template:RoundBoxBottom

Function defined by 3 points and 1 slope

Template:RoundBoxTop The cubic function may be defined by any 3 unique points on the curve and the slope at any 1 of these points.

For example, let us choose the three points:

(4,2),(2,3),(8,8)

It is known that the slope at point (8,8) is 6+1966.

Rearrange the standard cubic function to prepare for the calculation of a,b,c,d:

x3a+x2b+xc+1dy=0.

Equation of slope: s=3ax2+2bx+c

Rearrange the equation of slope to prepare for the calculation of a,b,c,d:

3x2a+2xb+1c+0ds=0.

For function solveMbyN see "Solving simultaneous equations" .

Template:RoundBoxTop

# python code

points = ((-4,-2), (2,3), (8,8))

L11 = []

for point in points :
    x,y = point
    L11 += [[x*x*x, x*x, x, 1, -y]]

(x,y) = (8,8)
L11 += [[3*x*x, 2*x, 1, 0, -(6 + 19/66)]]

print (L11)

Template:RoundBoxTop

[[-64, 16, -4, 1,  2],     
 [  8,  4,  2, 1, -3],                 # matrix supplied to function solveMbyN() below.
 [512, 64,  8, 1, -8],                 # 4 rows by 5 columns.
 [192, 16,  1, 0, -6.287878787878788]]

Template:RoundBoxBottom

# python code

output = solveMbyN(L11)
print (output)

Template:RoundBoxTop

# 4 coefficients a, b, c, d:
(0.07575757575757572, -0.4545454545454543, -0.9848484848484839, 6.181818181818179)

Template:RoundBoxBottom The 4 coefficients above are in fact the values 566,3066,6566,40866 (same as above.) Template:RoundBoxBottom Template:RoundBoxTop

File:0223cubics2.png
Graphs of 2 different cubic functions that satisfy the same 4 criteria.

If these 4 criteria (3 points and 1 slope) are used to define the cubic in which y is the independent variable, result is:

x=0.020819277108433898y3+0.1873734939759051y2 +1.1583614457831322y2.599325301204827.

Both curves satisfy the points (4,3),(2,3),(8,8) and have the same slope at point (8,8).

(In fact f(y)=1.728y3+15.552y2+96.144y215.74483.) Template:RoundBoxBottom Template:RoundBoxBottom Template:RoundBoxBottom

The simplest cubic function

Template:RoundBoxTop

File:20190823simplestCubic.png
Figure 1.

The simplest cubic function has coefficients b=c=0.

The simplest cubic function has coefficients b=c=0, for example:

y=f(x)=2x3+54.

To solve the equation:

2x3=54

x3=542=27

x=273=3

The function also contains two complex roots that may be found as solutions of the associated quadratic:

ax2+(ap+b)x+ap2+bp+c=2x2+(6)x+(18)=x23x+9=0

x=3±9362=3±272 =3±2712=3±(33)12


Curve f(x) is useful for finding the cube root of a real number.


Solve: x=N3.

x3=N

x3N=0.

This is equivalent to finding a root of function y=g(x)=x3N.


If you use Newton's method to solve g(x), it may be advantageous to put N in form n(103p) where 1n<1000.

Then N3=n3(10p). Remember to preserve correct sign of result. Template:RoundBoxBottom

Roots of equal absolute value

Template:RoundBoxTop

The cubic function ax3+bx2+cx+d  (1)

Let one value of x be p+q and another be pq.

Substitute these values into the original function in x (1) and expand.

+appp+3appq+3apqq+aqqq+bpp+2bpq+bqq+cp+cq+d  (2a)

+appp3appq+3apqqaqqq+bpp2bpq+bqq+cpcq+d  (3a)

(2a)+(3a),+2appp+6apqq+2bpp+2bqq+2cp+2d  (4)

(2a)(3a),+6appq+2aqqq+4bpq+2cq  (5)

Reduce (4) and (5) and substitute Q for qq:

+3Qap+Qb+appp+bpp+cp+d  (4a)

+Qa+3app+2bp+c  (5a)

Combine (4a) and (5a) to eliminate Q and produce a function in p:

(8aa)p3+(8ab)p2+(2ac2bb)p+(+adbc)  (6)

From (6), c0=adbc.

If c0==0, p=0 is a solution and function (5a) becomes:

Qa+c=0  (5d)

Q=ca

q=Q and two roots of (1) are 0±q.

An example

File:20190823cubicEqualAbsRoots.png
Figure 2.

The roots of equal absolute value are 3,3.

See Figure 2.


y=x3+2x29x18

c0=adbc=1(18)2(9)=0.

The function has roots of equal absolute value.

Q=ca=91=9; q=Q=9=±3

The roots of equal absolute value are 3,3. Template:RoundBoxTop This method works with complex roots of equal absolute value.


Consider function: y=f(x)=2x311x2+98x539

a,b,c,d=2,11,98,539

c0=adbc=2(539)(11)98=1078+1078=0.

f(x) has roots of equal absolute value.

Q=ca=982=49

q=Q=49=±7i

Roots of equal absolute value are: 7i,7i. Template:RoundBoxBottom Template:RoundBoxBottom

Equal Roots

Template:RoundBoxTop

Combine 4(a) and 5(a) from above to eliminate p and produce a function in Q:


(64aaaaa)Q4+ (160aaaac32aaabb)Q3+ (132aaacc56aabbc+4abbbb)Q2+ (27aaadd18aabcd+40aaccc+4abbbd25abbcc+4bbbbc)Q+ (27aacdd18abccd+4acccc+4bbbcdbbccc)  (8)


From (8) above: C0=27aacdd18abccd+4ac4+4b3cdbbc3 =c(27aadd18abcd+4ac3+4b3dbbcc)


If C0==0, then Q=0 is a solution , x=p±0=p and (4a),(5a) become:

ax3+bx2+cx+d  (4e)

3ax2+2bx+c  (5e)

If C0==0 because c=0, there is a stationary point where x=0.

Template:RoundBoxTop Note:

  • 27aadd18abcd+4accc+4bbbdbbcc is a factor of discriminant of cubic formula below. If C0==0 because the discriminant is 0, function (1) contains at least 2 roots equal to x±0 when both functions (4e),(5e) are 0.
  • (4e),(5e) are functions of the curve and the slope of the curve. In other words, equal roots occur where the curve and the slope of the curve are both zero.

Template:RoundBoxBottom

(4e) and (5e) can be combined to produce:

(ac)x2+(3ad+bc)x+(2bd+cc)  (4f)

(3a)x2+(2b)x+(c)  (5f)


(4f) and (5f) can be combined to produce:

(9ad+bc)x+(6bd+2cc)  (4g)

(+6abd2acc)x+(3acd+4bbdbcc)  (5g)


If the original function (1) contains 3 unique roots, then (4g),(5g) are numerically different.


If the original function (1) contains exactly 2 equal roots, then (4g),(5g) are numerically identical, and the 2 roots have the value x in (4g).


If the original function (1) contains 3 equal roots, then (4g),(5g) are both null, (4f),(5f) are numerically identical and x=b3a.


From equations (4g) and (5g):

(+6abd2acc)(6bd+2cc)+(3acd+4bbdbcc)(9ad+bc) =+27aacdd18abccd+4acccc+4bbbcdbbccc =C0

Examples

No equal roots

Template:RoundBoxTop

File:20190826cubic3uniqueRealRoots.png
Figure 3a.

Cubic function with 3 unique, real roots at (2,0),(1,0),(3,0).

Consider function y=x32x25x+6=(x+2)(x1)(x3)

from 4(g), x=(9ad+bc)(6bd+2cc)=44122

from 5(g), x=(+6abd2acc)(3acd+4bbdbcc)=122236

4(g),5(g) are numerically different.

Template:RoundBoxBottom

Exactly 2 equal roots

Template:RoundBoxTop

File:20190821cubicEqualRoots.png
Figure 3b.

Cubic function with 2 equal, real roots at (1,0).

Consider function y=f(x)=x33x29x5=(x+1)(x+1)(x5)

from 4(g), x=7272=1

from 5(g), x=7272=1

There are 2 equal roots at x=1. Template:RoundBoxTop See Function_as_product_of_linear_function_and_quadratic above.

To calculate all roots:

# python code.
a,b,c,d = 1,-3,-9,-5

# Associated quadratic:
p = -1
A = a
B = A*p + b
C = B*p + c

# Associated linear function:
a1 = A
b1 = a1*p + B

print ('x3 =', -b1/a1)
x3 = 5.0

Roots of cubic function f(x)=x33x29x5 are 1,1,5. Template:RoundBoxBottom Template:RoundBoxBottom

3 equal roots

Template:RoundBoxTop

File:20190826cubic3equalRoots1.png
Figure 3c.

Cubic function with 3 equal, real roots at (3,0).

Consider function y=x3+9x2+27x+27=(x+3)(x+3)(x+3)

from 4(g),x=00

from 4(f), 27x2+162x+243=x2+6x+9=0

from 5(f), 3x2+18x+27=x2+6x+9=0

4(f),5(f) are numerically identical, the discriminant of each is 0 and x=62=3

Template:RoundBoxBottom

Template:RoundBoxBottom

Depressed cubic

Template:RoundBoxTop

The depressed cubic may be used to solve the cubic equation.

In the cubic function: y=f(x)=ax3+bx2+cx+d let x=b+t3a, substitute for x and expand:

y=t3+(9ac3bb)t+(27aad9abc+2b3)27a2

When the function is equated to 0, the depressed equation is:

t3+At+B=0 where

A=(9ac3bb) and

B=(27aad9abc+2b3)

In the depressed equation the coefficient of t3 is 1 and the coefficient of t2 is 0. Template:RoundBoxTop

File:0226cubic1.png

The depressed function is a specific case of the general function in which coefficient b is missing.

Let y=ax3+cx+d.

Then point of inflection has coordinates (0,d), and y=3ax2+c.

When x==0, y=c.

If coefficient b is missing, the cubic function becomes y=ax3+cx+d, and

  • point of inflection has coordinates (0,d) and
  • slope at point of inflection =c.

Template:RoundBoxBottom Be prepared for the possibility that one or both of A,B may be zero.

6 examples

Template:RoundBoxTop Six simple depressed cubic functions illustrate all the possible shapes of all cubic functions:

Template:RoundBoxBottom

When A = 0

Template:RoundBoxTop

File:20190825cubicSlope0atPoI1.png
Figure 4a.

Cubic function with slope 0 at point of inflection (0.5,0.5).

This condition occurs when the cubic function in x has exactly one stationary point or when slope at point of inflection is zero.

f(x)=4x36x2+3x1

f(t)=t3216

t=2163=6

x=b+t3a=6+63(4)=1

The other roots may be derived from the associated quadratic:

y=4x2+(4(1)+(6))x+4(1)(1)+(6)(1)+3=4x22x+1

x=2±4168 =2±1218 =2±(23)18 =1±314

Template:RoundBoxBottom

When B = 0

Template:RoundBoxTop

File:20190825cubicPoIonXaxis.png
Figure 4b.

Cubic function with point of inflection (3,0) on X axis.

This condition occurs when the cubic function in x is of format (x+g)(x+g+h)(x+gh) or when point of inflection is on the X axis.

f(x)=x3+9x2+31x+39

f(t)=t3+36t=t(t2+36)

x=b3a=93(1)=3

t2=36; t=36=36(1)=±61

x=b+t3a=9±613(1)=3±21

Template:RoundBoxBottom

When A = B = 0

Template:RoundBoxTop

File:20190826cubic3equalRoots1.png
Figure 4c (same as 3c above).

Cubic function with:
* point of inflection (3,0) on X axis,
* slope 0 at point of inflection.

This condition occurs when:

  • slope at point of inflection is 0, and
  • point of inflection is on X axis.


Consider function y=x3+9x2+27x+27=(x+3)(x+3)(x+3)

f(t)=t3+(9ac3bb)t+(27aad9abc+2b3) =t3+(9127399)t+(27112791927+2999) =t3+(0)t+(0)

x=b+t3a=b3a=93=3

Template:RoundBoxBottom

Template:RoundBoxBottom

Vieta's substitution

Template:RoundBoxTop See Vieta's Substitution.

Let the depressed cubic be written as: t33Ct+B where C=b23ac and A=3C

Let t=w+Cw=w2+Cw

Substitute for t in the depressed function:

f(w)=w6+Bw3+C3

f(W)=W2+BW+C3 where W=w3 and w=W3.

From the quadratic formula: W=B±B24C32

The discriminant =B24C3. Substitute for B,C and expand:

This discriminant = 27a2(27aadd18abcd+4ac3+4b3dbbcc)

The factor (27aadd18abcd+4ac3+4b3dbbcc) is a factor of C0 under "Equal Roots" above.

Discriminant (B² - 4C³) == 0

Template:RoundBoxTop

File:20190821cubicEqualRoots.png
Figure 5a.

Cubic function with 2 equal, real roots at (1,0).

If discriminant (B24C3)==0, the function contains at least 2 equal, real roots.

Consider function y=x33x29x5=(x+1)(x+1)(x5)

W=B2=216

w=2163=6

t=w+Cw=6+366=12

x=b+t3a=3+123=5

Associated quadratic =x2+(15+3)x+(155+35+9) =x2+2x+1=(x+1)(x+1)

The 2 equal roots are: (1,0),(1,0).


Template:RoundBoxBottom

Discriminant (B² - 4C³) positive

Template:RoundBoxTop

File:20190827cubicDiscrPos.png
Figure 5b.

Cubic function with discriminant (B24C3) positive
and 1 real root at (5,0).

If discriminant (B24C3) is positive, the function contains exactly 1 real root.

Consider function y=x33x25x25

discriminant =B24C3=691200

r=691200=831.3843876330611

W=B+r2=847.6921938165306

w=W3=9.464101615137753

t=w+Cw=12

or:

W=Br2=16.307806183469438

w=W3=2.5358983848622447

t=w+Cw=12


x=b+t3a=5

The associated quadratic is: x2+(15+3)x+(155+35+5)=x2+2x+5

and the two complex roots are: 2±4202 =2±412 =1±21

Template:RoundBoxBottom

Discriminant (B² - 4C³) negative

Template:RoundBoxTop

If discriminant (B24C3) is negative, the function contains 3 real roots and W becomes the complex number B2±4C3B221.


Let Wmod be the modulus of W.

Let Wreal be the real part of W.

Let Wimag be the imaginary part of W.

Then Wreal=B2

Wimag=4C3B22

Wmod2=Wreal2+Wimag2=B24+4C3B24=C3.

Wmod=C3

Let Wϕ be the phase of W.

Then cosWϕ=WrealWmod and Wϕ=arccos(cosWϕ).

w=W3. Therefore:

wmod=Wmod3=C

wϕ=Wϕ3

wreal=wmod*cos(wϕ)

t=2*wreal

An example

Template:RoundBoxTop

File:20190826cubic3uniqueRealRoots.png
Figure 5c.

Cubic function with 3 unique, real roots at (2,0),(1,0),(3,0).

y=f(x)=x32x25x+6 in which a,b,c,d=1,2,5,6.

B=56

C=19

Wreal=B2=28

Wmod=C3=82.8190799272728

cosWϕ=WrealWmod=0.338086344651354

Wϕ=1.91567908829702 radians.

wϕ=Wϕ3=0.638559696099005 radians.

coswϕ=0.802955068546966

wmod=C=4.35889894354067

wreal=wmod*coswϕ=3.5

t=2*wreal=7

x=b+t3a=3 Template:RoundBoxBottom

Using Cosine (A/3)

Template:RoundBoxTop For function cosAfrom_cos3A see "Cosine(A/3)" .

Template:RoundBoxTop

# python code
from decimal import *
getcontext().prec = 40

a,b,c,d = [ Decimal(v) for v in (1,-2,-5,6) ]

A = 9*a*c - 3*b*b
B = 27*a*a*d - 9*a*b*c + 2*b*b*b
C = A/-3

Wreal = -B/2
wmod = C.sqrt()
Wmod = wmod * wmod * wmod
cosWφ = Wreal/Wmod

data = cosAfrom_cos3A(cosWφ)

for coswφ in data :
    wreal = wmod * coswφ
    t = 2*wreal
    x = (-b+t)/(3*a)
    print ('x =', float(x))

Template:RoundBoxBottom Results are: Template:RoundBoxTop

x = 3.0
x = -2.0
x = 1.0

Template:RoundBoxBottom Template:RoundBoxBottom Template:RoundBoxBottom

Template:RoundBoxBottom

Review of complex math

Template:RoundBoxTop

File:20180311Complex.png
Figure 6a: Components of complex number Z.

Origin at point (0,0).
Zreal parallel to X axis.
Zimag parallel to Y axis.
r=abs(Z)=Zmod = modulus of Z=Zreal2+Zimag2
Angle ϕ is the phase of Z=arctanZimagZreal Z=r(cosϕ+1sinϕ)=Zreal+1Zimag


File:20190830Wandw.png
Figure 6b: Complex numbers W and w.

Origin at point (0,0).
Wimag=4C3B22 (off image to left.)
w3=(wmod(coswϕ+isinwϕ))3
=wmod3(cos(3wϕ)+isin(3wϕ))
=Wmod(cosWϕ+isinWϕ)=W

A complex number contains a real part and an imaginary part, eg: 2+21.

In theoretical math the value 1 is usually written as i. In the field of electrical engineering and computer language Python it is usually written as j.


The value 2+21 is a complex number expressed in rectangular format.

The value w=2(cos45+1sin45) is a complex number expressed in polar format where 2 is the modulus of w or wmod and 45 is the phase of w or wϕ.

2(cos45+1sin45) =2(22+122) =2+12

Multiplication of complex numbers

p(cosA+isinA)q(cosB+isinB) =pq(cosAcosB+cosAisinB+isinAcosB+i2sinAsinB) =pq(cosAcosBsinAsinB+i(sinAcosB+cosAsinB)) =pq(cos(A+B)+isin(A+B))

To multiply complex numbers, multiply the moduli and add the phases.

Complex number cubed

(p(cosA+isinA))3 =p(cosA+isinA)p(cosA+isinA)p(cosA+isinA) =p2(cos2A+isin2A)p(cosA+isinA) =p3(cos2AcosA+cos2AisinA+isin2AcosA+isin2AisinA) =p3(cos2AcosA+i2sin2AsinA+i(cos2AsinA+sin2AcosA)) =p3((cos2AcosAsin2AsinA)+i(sin2AcosA+cos2AsinA)) =p3(cos3A+isin3A)


Generally (p(cosA+isinA))n =pn(cos(nA)+isin(nA)).

For the cube of a complex number in polar format, n=3.

Cube root of complex number W

Let W=Wmod(cosWϕ+1sinWϕ) and w=wmod(coswϕ+1sinwϕ)

If w=W3 then:

wmod=Wmod3 and

wϕ=Wϕ3.

Complex number w + C/w

Let w=k+mi where k=wreal,m=wimag.

w+Cw=k(C+k2+m2)+im(k2+m2C)k2+m2

If k2+m2==C:

w+Cw=k(C+C)+im(CC)C=2CkC=2k

In the case of 3 real roots, t=2wreal

Template:RoundBoxBottom

Cubic formula

Template:RoundBoxTop The substitutions made above can be used to produce a formula for the solution of the cubic equation.

Given cubic equation: y=f(x)=ax3+bx2+cx+d=0, calculate the 3 values of x.

x=b+t3a where:

Template:RoundBoxTop Coefficients of depressed cubic:

A=9ac3b2

B=27a2d9abc+2b3 Template:RoundBoxBottom

Template:RoundBoxTop One root of cubic function:

C=A3=b23ac

Δ=B24C3

δ=Δ

W=B+δ2

w=W3

t=w+Cw Template:RoundBoxBottom Template:RoundBoxTop Formula incorporating all eight statements above is:

x=b+(27a2d9abc+2b3)+(27a2d9abc+2b3)24(b23ac)323+(b23ac)(27a2d9abc+2b3)+(27a2d9abc+2b3)24(b23ac)3233a Template:RoundBoxBottom Template:RoundBoxTop Template:RoundBoxTop Cube roots of unity are: 1, cos60±isin60. See "Cube roots of 1."


Therefore W3 has 3 values:

w1=w

w2=w1+i32

w3=w1i32 Template:RoundBoxBottom

Template:RoundBoxTop It is not necessary to use both values of ±δ.

Choose either W=B+δ2 or W=Bδ2. Template:RoundBoxBottom Template:RoundBoxTop If f(x) contains 3 equal roots, A=B=0 and line t = w + C/w fails with divisor w=0.

Before using this formula, check for equal roots as in "3 equal roots" above. Template:RoundBoxBottom Template:RoundBoxBottom

Using cubic formula

Template:RoundBoxTop

2 equal roots

Template:RoundBoxTop

File:0329cubic01.png
Graph of cubic function with 2 equal roots at x=5.
Y axis compressed for clarity.

Calculate roots of f(x)=x37x25x+75.

# python code
a,b,c,d = 1, -7, -5, 75
A = 9*a*c - 3*b*b
B = 27*a*a*d - 9*a*b*c + 2*b*b*b
C = -A/3
Δ = B*B - 4*C*C*C
print ('Δ =', Δ)
W = -B/2
# The following 2 lines ensure that cube root of negative
# real number is real number.
if W < 0 : w_ = -((-W)**(1/3))
else : w_ = W**(1/3)
r3 = 3**(0.5)
values_of_w = (w_, 
               w_*(-1 + 1j*r3)/2, 
               w_*(-1 - 1j*r3)/2)
for w in values_of_w :
    print ()
    print ('w =', w)
    t = w + C/w
    print ('t =', t)
    x = (-b + t)/(3*a)
    print ('x =', x)

Template:RoundBoxTop

Δ = 0.0

w = -8
t = -16
x = -3

w = (4-6.928203230275509j)
t = 8
x = 5

w = (4+6.928203230275508j)
t = 8
x = 5

Notice that:

  • Δ is zero.
  • W=B2.
  • t=2wreal.

Template:RoundBoxBottom Template:RoundBoxBottom

1 real root

Template:RoundBoxTop

File:0329cubic02.png
Graph of cubic function with 1 real root at x=3.
Y axis compressed for clarity.

Calculate roots of f(x)=x37x2x+87.

# python code
a,b,c,d = 1, -7, -1, 87
A = 9*a*c - 3*b*b
B = 27*a*a*d - 9*a*b*c + 2*b*b*b
C = -A/3
Δ = B*B - 4*C*C*C
print ('Δ =', Δ)
δ = (Δ)**.5
W = (-B+δ)/2
if W < 0 : w_ = -((-W)**(1/3))
else : w_ = W**(1/3)
r3 = 3**(0.5)
values_of_w = (w_, 
               w_*(-1 + 1j*r3)/2, 
               w_*(-1 - 1j*r3)/2)
for w in values_of_w :
    print ()
    print ('w =', w)
    t = w + C/w
    print ('t =', t)
    x = (-b + t)/(3*a)
    print ('x =', x)

Template:RoundBoxTop

Δ = 1997568.0

w = -4.535898384862245
t = -16
x = -3

w = (2.2679491924311215-3.9282032302755088j)
t = (8.0+6.0j)
x = (5.0+2.0j)

w = (2.267949192431123+3.9282032302755083j)
t = (8.0-6.0j)
x = (5.0-2.0j)

Notice that:

  • Δ is positive.
  • W=B+δ2.
  • t does not equal 2wreal.

Template:RoundBoxBottom Template:RoundBoxBottom

3 real roots

Template:RoundBoxTop

File:0329cubic03.png
Graph of cubic function with 3 real roots, 3,4.4,11.
Y axis compressed for clarity.

Calculate roots of f(x)=5x362x2+11x+726.

# python code
a,b,c,d = 5, -62, 11, 726
A = 9*a*c - 3*b*b
B = 27*a*a*d - 9*a*b*c + 2*b*b*b
C = -A/3
Δ = B*B - 4*C*C*C
print ('Δ =', Δ)
δ = (Δ)**.5
W = (-B-δ)/2
w_ = W**(1/3)
r3 = 3**(0.5)
values_of_w = (w_, 
               w_*(-1 + 1j*r3)/2, 
               w_*(-1 - 1j*r3)/2)
for w in values_of_w :
    print ()
    print ('w =', w)
    t = w + C/w
    print ('t =', t)
    x = (-b + t)/(3*a)
    print ('x =', x)

Template:RoundBoxTop

Δ = -197238264300.0

w = (51.5-32.042939940024226j)
t = 103
x = 11

w = (2.0+60.62177826491069j)
t = 4.0
x = 4.4

w = (-53.5-28.578838324886462j)
t = -107
x = -3

Notice that:

  • Δ is negative
  • W=Bδ2.
  • t equals 2wreal.

Template:RoundBoxBottom Template:RoundBoxBottom Template:RoundBoxBottom Template:RoundBoxBottom

cos (A/3)

Template:RoundBoxTop

The method above for calculating coswϕ depends upon calculating the value of angle wϕ.

However, coswϕ may be calculated from cosWϕ because wϕ=Wϕ3.

Generally, when cosA is known, there are 3 possible values of the third angle because cos3(A3±120)=cos(A±360)=cos(A).

This suggests that there is a cubic relationship between cosA3 and cosA.

Expansion of cos (3A)

Template:RoundBoxTop

File:20190718cos3A.png
Figure 7a.

Graph of cos3A.

The well known identity for cos3A is:

cos3A=4cos3A3cosA.

The derivation of this identity may help understanding and interpreting the curve of cos3A.

Let cos3A=acos3A+bcos2A+ccosA+d.

cos90=0 and cos270=0

Therefore the point (0,0) is on the curve and d=0.

A 3A cos A cos 3A
0 0 1 1
180 180*3 -l -1
60 180 0.5 -1

Three simultaneous equations may be created from the above table:

1=1a+1b+1c

1=1a+1b1c

Therefore b=0.

1=1a+1c

1=a8+c2

a=4 and c=3.

When cos3A is known, 4cos3A3cosAcos3A=0.

Template:RoundBoxBottom

Newton's Method

Template:RoundBoxTop

File:20190901Newton'sMethod.png
Figure 7b.

Newton's Method used to calculate cosA when cos3A=0.1.

Newton's method is a simple and fast root finding method that can be applied effectively to the calculation of cosA when cos3A is known because:

  • the function is continuous in the area under search.
  • the derivative of the function is continuous in the area under search.
  • the method avoids proximity to stationary points.
  • a suitable starting point is easily chosen.

See Figure 7b.

Perl code used to calculate cosA when cos3A=0.1 is:

$cos3A = 0.1;

$x = 1; # starting point.
$y = 4*$x*$x*$x - 3*$x - $cos3A;

while(abs($y) > 0.00000000000001){
    $s = 12*$x*$x - 3; # slope of curve at x.
    $delta_x = $y/$s;
    $x -= $delta_x;
    $y = 4*$x*$x*$x - 3*$x - $cos3A;

    print "                                                                                   
x=$x                                                                                              
y=$y                                                                                              
";
}

print "                                                                                           
cos(A) = $x                                                                                       
";
x=0.9
y=0.116

x=0.882738095238095
y=0.00319753789412588

x=0.882234602936352
y=2.68482638085543e-06

x=0.882234179465815
y=1.89812054962601e-12

x=0.882234179465516
y=-3.60822483003176e-16

cos(A) = 0.882234179465516

When cos(3A) is positive

Template:RoundBoxTop

File:20190901cos3A+ve.png
Figure 7c.

Newton's Method used to calculate cosA when cos3A=0.4.

When cos(3A)=0.4, output of the above code is:

x=0.933333333333333
y=0.0521481481481486

x=0.926336712383224
y=0.000546900278781126

x=0.926261765753783
y=6.24370847246425e-08

x=0.926261757195518
y=7.7715611723761e-16

cos(A) = 0.926261757195518

If all 3 values of cosA are required, the other 2 values can be calculated as roots of the associated quadratic function with coefficients (a1,b1,c1)=(4,4cosA,4cos2A3)

x1 = -0.136742508909433
x2 = -0.789519248286085

Proof:

# python code
values = (0.926261757195518, -0.136742508909433, -0.789519248286085)

for cosA in values :
    cos3A_ = 4*cosA*cosA*cosA -	3*cosA
    difference = abs (cos3A_ - 0.4)
    print ('cosA =',cosA,'   difference =',difference)

Results:

cosA =  0.926261757195518    difference = 7.771561172376096e-16
cosA = -0.136742508909433    difference = 1.7208456881689926e-15
cosA = -0.789519248286085    difference = 2.1094237467877974e-15

Template:RoundBoxBottom

When cos(3A) is negative

Template:RoundBoxTop

File:20190901cos3A-ve.png
Figure 7d.

Newton's Method used to calculate cosA when cos3A=0.2.

When cos3A is negative, the starting value of x=1.

When cos3A=0.2, output of the above code is:

x=-0.911111111111111
y=-0.0920054869684496

x=-0.89789474513884
y=-0.00190051666894692

x=-0.897610005610658
y=-8.73486682706481e-07

x=-0.89760987462259
y=-1.8446355554147e-13

x=-0.897609874622562
y=-1.66533453693773e-16

cos(A) = -0.897609874622562

Template:RoundBoxBottom

An example

Template:RoundBoxTop

File:20190826cubic3uniqueRealRoots.png
Figure 7e.

Cubic function with 3 unique, real roots at (2,0),(1,0),(3,0).

y=f(x)=x32x25x+6 in which a,b,c,d=1,2,5,6.

B=56

C=19

Wreal=B2=28

Wmod=C3=82.8190799272728

cosWϕ=WrealWmod=0.338086344651354

Use the code beside Figure 7b above with initial conditions:

$cosWphi = -0.338086344651354;

$cos3A = $cosWφ;

$x = -1; # starting point.

coswϕ=0.917662935482248

wmod=C=4.35889894354067

wreal=wmod*coswϕ=4

t=2*wreal=8

x=b+t3a=2

Template:RoundBoxBottom

Template:RoundBoxBottom

Template:RoundBoxBottom

Point of Inflection

Template:RoundBoxTop

The Point of Inflection is the point at which the slope of the curve is minimum.

After taking the first and second derivatives value x at point of inflection is:

xpoi=b3a.

The slope at point of inflection is:

spoi=3acb23a.

Value y at point of inflection is:

ypoi=2b39abc+27a2d27a2.

From basic principles

Template:RoundBoxTop

File:0218cubic1.png
Figure 1. 4 points relative to point of inflection.

Relative to point of inflection (2,3):
Points (3,3),(7,3) have coordinates (5,0),(5,0).
Points (4,2),(8,8) have coordinates (6,5),(6,5).
When 2 points are equidistant in terms of X, they are also equidistant in terms of Y.

xpoi may be calculated from basic principles.

Let us define the point of inflection as the point about which the curve is symmetric.

Let xpoi=X and let p be non-zero.

Then Y=aX3+bX2+cX+d where (X,Y) is point of inflection.

Let Xp=X+p and Xn=Xp.

Then Yp=a(Xp)3+b(Xp)2+c(Xp)+d

=XXXa+3XXap+XXb+3Xapp+2Xbp+Xc+appp+bpp+cp+d.

Let dp be Yp relative to Y.

Then dp=YpY=3XXap+3Xapp+2Xbp+appp+bpp+cp.

Similarly dn=YYn=3XXap3Xapp+2Xbp+apppbpp+cp.

dp=dn therefore:

3XXap+3Xapp+2Xbp+appp+bpp+cp =3XXap3Xapp+2Xbp+apppbpp+cp

+3Xapp+bpp=3Xappbpp

+3Xa+b=3Xab

+3Xa+b+3Xa+b=0

+3Xa+b=0

X=b3a Template:RoundBoxBottom

Depressed cubic

Recall from "Depressed cubic" above:

A=9ac3b2

B=27a2d9abc+2b3

Therefore:

spoi=A9a

ypoi=B27a2

Template:RoundBoxTop Coefficients A,B of the depressed cubic show immediately:

  • If slope at point of inflection is positive, zero or negative, and
  • If point of inflection is above, on or below X axis.

Template:RoundBoxBottom

If 1 of A,B is zero, the cubic equation may be solved as under "Depressed cubic" above.

Template:RoundBoxBottom

Newton's Method

Template:RoundBoxTop

If both A,B of the depressed function are non-zero, Newton's method may be applied to the original cubic function, and the Point of Inflection offers a convenient starting point. Template:RoundBoxTop When implemented as described below, Newton's Method always avoids that part of the curve where there might be equal roots. Template:RoundBoxBottom

slope at PoI positive

Template:RoundBoxTop

File:20190909spoi+ve.png
Figure 8a.

y=x3+9x2+52x+15225
Cubic function with positive slope at Point of Inflection (3,2).

A=0.36

B=0.0864

xpoi=3

spoi=1

ypoi=2

Template:RoundBoxBottom

slope at PoI negative

Template:RoundBoxTop

PoI above X axis

Template:RoundBoxTop

File:20190909PoIaboveXaxis.png
Figure 8b.

y=x3+9x2+2x2810
Cubic function with negative slope at Point of Inflection (3,2)
and PoI above X axis.

A=2.25

B=0.54

xpoi=3

spoi=2.5

ypoi=2

When y==ypoi, the other 2 intercepts may be calculated as roots of the associated quadratic with coefficients:

(a1, b1, c1) =(a, axpoi+b, axpoi2+bxpoi+c) =(0.1,0.6,1.6).

(x1a,x1b)=(8,2).

($a,$b,$c,$d) = (0.1,0.9,0.2,-2.8);
($x,$y) = ($x1a,$ypoi);

while(abs($y) > 1e-14){
    $s = 3*$a*$x*$x + 2*$b*$x + $c;
    $delta_x = $y/$s;
    $x -= $delta_x ;
    $y = $a*$x*$x*$x + $b*$x*$x + $c*$x + $d;
    print "
x=$x,y=$y
";
}

print "
x=$x
";
x=-8.4,y=-0.246400000000004

x=-8.36056338028169,y=-0.00251336673084257

x=-8.36015274586958,y=-2.7116352896428e-07

x=-8.36015270155726,y=-8.88178419700125e-16

x=-8.36015270155726

Template:RoundBoxTop

File:0227cubics3.png
Graphs of 3 different cubic functions all solved with the same technique.

The figure shows all possible considerations if point of inflection is above X axis.

The same technique using Newton's Method works well with all conditions. Template:RoundBoxBottom Template:RoundBoxBottom

PoI below X axis

Template:RoundBoxTop

File:20190909PoIbelowXaxis.png
Figure 8c.

y=x36x213x810
Cubic function with negative slope at Point of Inflection (2,5)
and PoI below X axis.

A=2.25

B=1.35

xpoi=2

spoi=2.5

ypoi=5

When y==ypoi, the other 2 intercepts may be calculated as roots of the associated quadratic with coefficients:

(a1, b1, c1) =(0.1,0.4,2.1).

(x1a,x1b)=(3,7). Template:RoundBoxTop

File:0227 3cubics.png
Graphs of 3 different cubic functions all solved with the same technique.

The figure shows all possible considerations if point of inflection is below X axis.

The same technique using Newton's Method works well with all conditions. Template:RoundBoxBottom Template:RoundBoxBottom Template:RoundBoxBottom

Using Newton's method

Template:RoundBoxTop The method used to solve the cubic equation (as presented here) depends on the value of the discriminant (B² - 4C³). If this value is non-negative, the value of 1 root is easily calculated and the other 2 roots are solutions of the associated quadratic described under "linear and quadratic" above. If this value is negative, the use of this value leads to some interesting theory of complex numbers and the solution depends on the calculation of cosθ3.


The method above uses Newton's method to calculate cosθ from cos(3θ). Newton's method is very fast and more than adequate to do the job. However, the purist might say that the solution of a cubic equation must not depend on the solution of a cubic equation. The solution offered under "Cosine(A/3)" above satisfies the purist.


Also, if you're going to use Newton's method to calculate cosθ3, why not use Newton's method to calculate one real root of the original cubic?


One of the objectives above is to show that the cubic equation can be solved with high school math. Newton's method can be implemented with a good knowledge of high school calculus and the starting point may depend on the solution of a quadratic equation, also understood with a good knowledge of high school algebra.


The example presented below shows how to solve the cubic equation with high school math. Template:RoundBoxTop

For function TwoRootsOfCubic() see General_case above.

Calculate roots of cubic function: y=f(x)=2x333x26164x65760.

Template:RoundBoxTop

# python code

a,b,c,d = abcd = 2, -33, -6164, -65760

# Coefficients of depressed cubic:
A = 9*a*c - 3*b*b
B = 27*a*a*d - 9*a*b*c + 2*b*b*b

# The point of inflection:
ypoi = B/(27*a*a)
xpoi = -b/(3*a)
spoi = A/(9*a)
print ('xpoi =',xpoi)
print ('ypoi =',ypoi)
print ('spoi =',spoi)
xpoi = 5.5
ypoi = -100327.5
spoi = -6345.5

spoi is negative. xpoi will not be used as starting point. Template:RoundBoxBottom

File:0327 2curves01.png
Figure 1: Diagram illustrating relationship between f(x)=2x333x26164x65760 and g(x)=2x222x6285.
Roots of g(x),(x11,x12) are 2 possible starting points.
Because ypoi is below X axis, x12 is chosen.

The associated quadratic when p=xpoi:

Template:RoundBoxTop Find 2 possible starting points (x11, x12) to left and right of xpoi.

TwoRootsOfCubicDebug = 1

x11,x12 = TwoRootsOfCubic((a,b,c,d), xpoi)
print ('x11,x12 =',x11,x12)
TwoRootsOfCubic ():
    y = ( 2 )xx + ( -22.0 )x + ( -6285.0 )
    for x1 = 5.5 , sum1 = -100327.5
    for x2 = -50.82716928800878 , sum2 = -100327.5
    for x3 = 61.82716928800878 , sum3 = -100327.50000000006
    
x11,x12 = -50.82716928800878 61.82716928800878

Template:RoundBoxBottom

Execute newton's method:

Template:RoundBoxTop

if ypoi > 0 : start = x11
else : start = x12

# The differential function
_a = 3*a
_b = 2*b
_c = c

x = start
y = ypoi
while abs(y) > 1e-24 :
    print ('x,y =',x,y)
    s = _a*x*x + _b*x + _c
    deltax = y/s
    x -= deltax
    y = a*x*x*x + b*x*x + c*x + d
x1 = x
print ('x1 =',x1)
x,y = 61.82716928800878 -100327.5
x,y = 69.7325746934142 22109.249048075755
x,y = 68.53160134747581 552.4056893695961
x,y = 68.50002158732813 0.3770984176080674
x,y = 68.50000000001009 1.762527972459793e-07

x1 = 68.5

The starting point (x= start) is close to x1, which was found quickly. Template:RoundBoxTop If only one root is required (as in calculation of roots of quartic function), x1 may be used, in which case calculations below are not necessary. The big advantage of using x1 is that x1 is guaranteed to be a real number. Template:RoundBoxBottom Template:RoundBoxBottom

File:0509 2curves01.png
Figure 2: Diagram illustrating relationship between f(x)=2x333x26164x65760 and h(x)=2x2+104x+960.
Roots of h(x),(x2,x3), are 2 roots of f(x).

The associated quadratic when p=x1:

Template:RoundBoxTop

x2,x3 = TwoRootsOfCubic(abcd, x1)

print ('3 roots:', x1,x2,x3)
TwoRootsOfCubic ():
    y = ( 2 )xx + ( 104.0 )x + ( 960.0 )
    for x1 = 68.5 , sum1 = 0.0
    for x2 = -40.0 , sum2 = 0.0
    for x3 = -12.0 , sum3 = 0.0
    
3 roots: 68.5 -40.0 -12.0

Template:RoundBoxBottom Template:RoundBoxBottom Template:RoundBoxBottom

In practice

Template:RoundBoxTop Much interesting theory concerning complex numbers and Vieta's substitution has been presented above. The formula for x, a root of the cubic equation, is already appearing to be too complicated. Using the formula usually involves calculation of square root and cube root, possibly a complex cube root. How are these values calculated? Possibly by using Newton's method.

Every cubic function is guaranteed to contain at least one real root. The function below, oneRootOfCubic (), is an attempt at almost extreme simplicity. Considerations such as equal roots or complex cube root are ignored. After a few simple decisions, Newton's method is used to derive one real root of the given cubic. Template:RoundBoxTop

# Python code
newtonDebug = 0

def newton (abcd, startx) :
    '''
x = newton (abcd, startx)
Values a,b,c,d,startx may be Decimal or non-Decimal, but not a mixture of both.
x is float or None.
Newton's method for finding 1 root of cubic function.
2 global variables are needed: newtonDebug
                               almostZero (same as relative tolerance)
'''

    if newtonDebug : print ('newton() 1: a,b,c,d =',abcd)
    a,b,c,d = abcd
    x = startx ; xx = x*x
    y = a*xx*x + b*xx +  c*x + d
    if newtonDebug : print ('newton() 2: x,y =', x,y)

# The differential function
# slope = 3*a*x*x + 2*b*x + c
    _a = 3*a
    _b = 2*b
    _c = c

    count = 0 ; L1 = []
    while 1 :
        count += 1
        if count >= 51 :
            print ('newton() 3: count expired.')
            return None
        slope = _a*xx + _b*x + _c
        delta_x = y/slope
        x -= delta_x
        xx = x*x
        t3,t2,t1 = a*xx*x, b*xx, c*x
        y = t3 + t2 + t1 + d
# Yes. This calculation of y is slightly faster than:
#        y = a*x*x*x + b*x*x + c*x + d
        if newtonDebug : print ('newton() 4: x,y =', x,y)
        if abs(y) <= almostZero : break
        max = sorted([ abs(v) for v in (t3,t2,t1,d) ])[-1]
        if abs(y)/max <= almostZero : break
        if x in L1[-1::-1] :
            if newtonDebug : print ('newton() 5: Endless loop detected.')
            return None
        L1 += [x]

    if newtonDebug : print ('newton() 6: count =', count)

    return x

Template:RoundBoxBottom Template:RoundBoxTop

# Python code
import decimal
D = decimal.Decimal

oneRootOfCubicDebug = 0

def oneRootOfCubic (abcd) :
    '''
x1 = oneRootOfCubic (abcd)
Each member of a,b,c,d must be int or float or Decimal.
If any member is Decimal, this function ensures that all are Decimal.
x1 may be None.
'''
    useDecimal = False
    for v in abcd :
        if isinstance (v,D) :
            useDecimal = True; continue
        if type(v) not in (int,float) :
            print ('oneRootOfCubic() 1: Each member of input (abcd) must be int, float or Decimal.')
            return None

    if useDecimal : a,b,c,d = [ D(str(v)) for v in abcd ]
    else : a,b,c,d = abcd

    if a == 0 :
        print ('oneRootOfCubic() 2: a must be non-zero.')
        return None

    if d == 0 : return 0

    if a != 1 :
        divider = a
        a,b,c,d = [ v/divider for v in (a,b,c,d) ] # a is now +1.

    if b == c == 0 :
        # This is effectively the calculation of cube root.
        if oneRootOfCubicDebug : print ('oneRootOfCubic() 3: Cube root of', -d)
        root3 = simpleCubeRoot(-d)
        if useDecimal : return D(str(root3))
        return float(root3)

    xpoi = -b/3 # Point of inflection.

    # Coefficient B of depressed cubic. B = 27*a*a*d - 9*a*b*c + 2*b*b*b
    B = 27*d - 9*b*c + 2*b*b*b
    if B == 0 :
        # Point of inflection is on X axis.
        if oneRootOfCubicDebug : print ('oneRootOfCubic() 4: Found B=0.')
        return xpoi

    A = 3*c - b*b # Coefficient A of depressed cubic. A = 9*a*c - 3*b*b
    if A == 0 :
        # Slope at Point of inflection is 0.
        if oneRootOfCubicDebug : print ('oneRootOfCubic() 5: Found A=0.')
        t = simpleCubeRoot (-B)
        if not useDecimal : t = float(t)
        x = (-b + t)/3
        return x

    if A > 0 :
        # Slope at Point of inflection is positive.
        return newton((a,b,c,d),xpoi)

    # Slope at Point of inflection is negative.
    x1,x2 = TwoRootsOfCubic ( [ float(v) for v in (a,b,c,d) ], float(xpoi) )
    if oneRootOfCubicDebug : print ('oneRootOfCubic() 6: x1,x2 =',x1,x2)
    if useDecimal : x1,x2 = [ D(str(v)) for v in (x1,x2) ]

    if B > 0 :
        # Point of inflection is above X axis.
        return newton((a,b,c,d),x1)

    # Point of inflection is below X axis.
    return newton((a,b,c,d),x2)

For function TwoRootsOfCubic () see General case above.

For function simpleCubeRoot (N), see Implementation. Template:RoundBoxBottom Template:RoundBoxBottom Template:RoundBoxBottom

Translation of axes

Cubic function relative to (u,v)

Template:RoundBoxTop

File:0608 2curves01.png
Graphs of 2 cubic functions with same shape.
Red curve relative to point (6,2) is same as blue curve relative to (0,0).

The familiar equation of the cubic function: y=f(x)=ax3+bx2+cx+d. This is the equation of f(x) relative to origin (0,0). However, f(x) need not be constrained as always relative to origin. It is always possible, and sometimes desirable, to express f(x) relative to any other point (u,v) in the two dimensional plane. The process of producing a new function g(x) that is f(x) relative to (u,v) is called "Translation of axes."

On this page the point of reference of any cubic function is the point of inflection.


Point of inflection of f(x) (red curve) is (0,0). Relative to point (6,2) red curve is located at (6,2). Relative to (0,0) blue curve is located at (6,2) and equation of blue curve is y2=(x6)34(x6) or y=x318x2+104x190.


Equation of f(x) relative to (6,2) is: y=x318x2+104x190.


  • y+v=f(x+u).


y=Ax3+Bx2+Cx+D where:

A=a

B=3au+b

C=3au2+2bu+c

D=au3+bu2+cu+dv Template:RoundBoxBottom

Move cubic function to (u,v)

Template:RoundBoxTop

File:0609 2curves00.png
Graphs of 2 cubic functions with same shape.
Red curve moved to (12,7) has equation y=x336x2+428x1673.
Blue curve moved to (3,4) has equation y=x39x2+23x11.

When a cubic function is moved to point (u,v), the point of inflection is moved from present position (xpoi,ypoi) to (u,v), and the amount of movement is:

Δx=uxpoi

Δy=vypoi.


Equation of function after being moved is:

yΔy=f(xΔx), or y=Ax3+Bx2+Cx+D where:

xpoi=b3a

ypoi=axpoi3+bxpoi2+cxpoi+d

A=a

B=3aΔx+b

C=3aΔx22bΔx+c

D=aΔx3+bΔx2cΔx+d+Δy. Template:RoundBoxBottom

Describing a cubic function

Template:RoundBoxTop

File:0610cubic01.png
Graph of complicated cubic function simplified.

Given a random cubic function, particularly those with large coefficients, it's usually difficult to visualize the function. One way to simplify our perception of a random cubic function is to consider the function at origin (0,0). This can be done by:

  • moving function to (0,0), or
  • calculating equation of function relative to point of inflection (xpoi,ypoi).

Consider function y=f(x)=x3214035x2+15270327066x363153827650064.

Point of inflection is (71345,96456).

Equation of f(x) relative to point of inflection is:

y=g(x)=x39x. See red curve in diagram.

f(x) may be described as g(x) moved to point (71345,96456). Template:RoundBoxBottom