logo back up home forward   further reading more topics »

Maths - Cubic Functions

A cubic function has an x3 term, its general form is:

a x3 + b x2 + c x + d = 0

The three solutions are:

root1 = -a/3 + ((-2a3 + 9ab - 27c + sqrt((2a3 -9ab +27c)2 + 4(-a2 + 3b)3))/54)1/3 + ((-2a3 + 9ab - 27c - √((2a3 -9ab +27c)2 + 4(-a2 + 3b)3))/54)1/3

root2 = -a/3 - (1 + i sqrt(3))*0.5*((-2a3 + 9ab - 27c + sqrt((2a3 -9ab +27c)2 + 4(-a2 + 3b)3))/54)1/3 + (-1 + i sqrt(3))*0.5*((-2a3 + 9ab - 27c - sqrt((2a3 -9ab +27c)2 + 4(-a2 + 3b)3))/54)1/3

root3 = -a/3 + ((-2a3 + 9ab - 27c + sqrt((2a3 -9ab +27c)2 + 4(-a2 + 3b)3))/54)1/3 + ((-2a3 + 9ab - 27c - √((2a3 -9ab +27c)2 + 4(-a2 + 3b)3))/54)1/3

Methods of solution

  1. by factors
  2. by completing the cobe
  3. by formulae
  4. by graph

Sum and difference of two cubes

(a + b)(a2 - ab + b2)

= a (a2 - ab + b2) + b (a2 - ab + b2)

= a3 - a2b + ab2 + ba2 - ab2 + b3

= a3 + b3

simarly

(a - b)(a2 + ab + b2) = a3 - b3

Using Symmetry

Instead of using the equation:

a x3 + b x2 + c x + d = 0

= 0

we can use the equation:

(x - x1)*(a' x2 + b' x + c') = 0

where x is our variable and x1, a', b' and c' are constants.

which has solutions at:

x = x1 and quadratic formula

In other words, can we use this to reduce the cubic to a quadratic in this way? First we have to multiply out the terms to see if its a general cubic:

(x - x1)*(a' x2 + b' x + c') = (a' x3 + b' x2 + c' x ) - x1*a' x2 - x1* b' x - x1* c')

= a' x3 + (b' - x1*a')x2 + (c' - x1* b') x - x1* c')

Therefore:

So, puting this in terms of the original equation,

so:

c'2 - c*c' + d*(b - d*a) = 0

c' = (c± √(c2 - 4*d*(b - d*a)))/2

substituting in above gives:

Code

Here is a Java function to return the cubic roots

/**
This is incomplete and does not handle roots of -ve, let me know if you can help

code to solve cubic equation ax^3 + bx^2 + cx + d =0
result we be returned in resultReal[0],resultImaginary[0]
to resultReal[2] and resultImaginary[2]
expects arrays to be setup before being called, like this:
double[] resultReal= new double[3];
double[] resultImaginary = new double[3];
*/
public void cubic(double a, double b, double c, double d,
     double[] resultReal , double[] resultImaginary) {
  if (Math.abs(a) < 0.000001) { // if a=0 equation is quadratic
    // handle seperately to avoid divide by zero
	quadratic(b,c,d,resultReal,resultImaginary);
	// the quadratic function is defined on this page:
    // http://www.euclideanspace.com/maths/algebra/equations/polynomial/quadratic/
	return;
  }
  // recuring terms
  double t1=(2*a*a*a -9*a*b +27*c);
  double t2=(-a*a + 3*b);
  double t3= t1*t1 + 4*t2*t2*t2;
  if (t3<0) handle complex roots;
  double t4=((-t1 + sqrt(t3))/54);
  double t41=((-t1 - sqrt(t3))/54);
  double t5=(-a*a + 3b);
  double t6=sqrt(t1*t1 + 4*t5*t5*t5);
  double t7=((-t1 - t6)/54);
  resultReal[0] = -a/3 + Math.pow(t4,1/3) + Math.pow(t7,1/3);
  resultReal[1] = -a/3 - 0.5*Math.pow(t4,1/3)+  -0.5*Math.pow(t4,1/3);
  resultReal[2] = -a/3 + Math.pow(t4,1/3) + Math.pow(t41,1/3)
  resultImaginary[0] = 0;
  resultImaginary[1] = sqrt(t3)*0.5*Math.pow(t4,1/3)+ sqrt(t3)*0.5*Math.pow(t4,1/3);
  resultImaginary[2] = 0;
  return;
} 

metadata block
see also:

 

Correspondence about this page

Book Shop - Further reading.

Where I can, I have put links to Amazon for books that are relevant to the subject, click on the appropriate country flag to get more details of the book or to buy it from them.

cover Mathematics for 3D game Programming - Includes introduction to Vectors, Matrices, Transforms and Trigonometry. (But no euler angles or quaternions). Also includes ray tracing and some linear & rotational physics also collision detection (but not collision response).

Other Math Books

Commercial Software Shop

Where I can, I have put links to Amazon for commercial software, not directly related to the software project, but related to the subject being discussed, click on the appropriate country flag to get more details of the software or to buy it from them.

cover Mathmatica

Can you help?

Please send me any improvements to here. I would appreciate ideas to make the pages more useful including error correction, ideas for new pages, improvements to wording. It helps if you quote the full URL of the page.

 

Terminology and Notation

Specific to this page here:

 

program

I am working on a project which uses these principles, if you would like to help me with this you are welcome to join in, here:

http://sourceforge.net/projects/mjbworld/

This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2008 Martin John Baker - All rights reserved - privacy policy.