Computer Science Canada Local Minimum and Local Maximum of A Cubic Function v.3

Author:  randint [ Fri Dec 23, 2011 7:58 pm ]
Post subject:  Local Minimum and Local Maximum of A Cubic Function v.3

This is the completely new, "fixed" version of the program to find local minimum and maximum of a cubic function. It can accurately calculate, using the rules of calculus, the local minimum and maximum (if they exist).
 Java: import java.io.*; import java.text.*; public class localmaxmin {     public static void main (String args[])     {         try         {             BufferedReader br = new BufferedReader (new InputStreamReader (System.in));             DecimalFormat df = new DecimalFormat ("#.###");             double c[] = new double [4];             double dxdy[] = new double [3];             double dxdy2[] = new double [2];             int count = 3;             char _ = 'A';             System.out.println ("This program calculates the local minimum and maximum of a cubic function.");             System.out.println ("A, B, C and D are coefficients from the form f(x)=ax^3+bx^2+cx+d");             System.out.println ("The minimum or maximum might not exist depending on the equation.");             System.out.println ("When the second derivative test returns 0, errors will occur.");             System.out.println ();             for (int i = 0 ; i < 4 ; i++)             {                 System.out.print ("Enter " + _ + " : ");                 c [i] = Double.parseDouble (br.readLine ());                 _++;             }             for (int j = 0 ; j < 3 ; j++)             {                 dxdy [j] = c [j] * count;                 count--;             }             double x[] = new double [2];             double vertex[] = new double [2];             double r1[] = new double [3];             double r2[] = new double [3];             double r3[] = new double [2];             double discriminant = Math.pow (dxdy [1], 2) - 4 * dxdy [0] * dxdy [2];             x [0] = (-dxdy [1] + Math.sqrt (discriminant)) / (2 * dxdy [0]);             x [1] = (-dxdy [1] - Math.sqrt (discriminant)) / (2 * dxdy [0]);             count = 3;             vertex [0] = c [3];             vertex [1] = vertex [0];             for (int k = 0 ; k < 3 ; k++)             {                 r1 [k] = c [k] * Math.pow (x [0], count);                 vertex [0] += r1 [k];                 r2 [k] = c [k] * Math.pow (x [1], count);                 vertex [1] += r2 [k];                 count--;             }             dxdy2 [0] = 2 * dxdy [0];             dxdy2 [1] = dxdy [1];             double st[] = new double [2];             st [0] = dxdy2 [0] * x [0] + dxdy2 [1];             st [1] = dxdy2 [0] * x [1] + dxdy2 [1];             if (discriminant >= 0)             {                 System.out.println ();                 System.out.println ("First Derivative Test:");                 System.out.println ("Returned Value 1: " + df.format (x [0]) + ", " + df.format (vertex [0]));                 System.out.println ("Returned Value 2: " + df.format (x [1]) + ", " + df.format (vertex [1]));                 System.out.println ();                 System.out.println ("Second Derivative Test:");                 System.out.println ("Returned Value 1: " + df.format (x [0]) + ", " + df.format (st [0]));                 System.out.println ("Returned Value 2: " + df.format (x [1]) + ", " + df.format (st [1]));                 System.out.println ();                 for (int n = 0 ; n < 2 ; n++)                 {                     if (st [n] < 0)                     {                         System.out.println (df.format (x [n]) + ", " + df.format (vertex [n]) + " is a local maximum.");                     }                     else if (st [n] > 0)                     {                         System.out.println (df.format (x [n]) + ", " + df.format (vertex [n]) + " is a local minimum.");                     }                     else if (st [n] == 0)                     {                         System.out.println (df.format (x [n]) + ", " + df.format (vertex [n]) + " is a point of inflection");                     }                 }             }             if (discriminant < 0)             {                 System.out.println ("There is no local minimum or maximum for this function.");             }         }         catch (Exception e)         {             System.exit (0);         }     } }

Method used to find the local minimum/maximum of any polynomial function:
1. Find the derivative
2. Find the roots (x-intercepts) of this derivative
3. Substitute the roots into the original function, these are local minima and maxima
4. Find the second derivative
5. Substitute the roots into the second derivative, if the answer is positive, it is a minimum, if it's negative, it's a maximum, if it is 0, it is a point of inflection (curve turns from up to down or vice versa.

 :