 Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki Blog Search Turing Chat Room Members
Local Minimum and Local Maximum of A Cubic Function v.3        Author Message
randint Posted: 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 ;             double dxdy[] = new double ;             double dxdy2[] = new double ;             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 ;             double vertex[] = new double ;             double r1[] = new double ;             double r2[] = new double ;             double r3[] = new double ;             double discriminant = Math.pow (dxdy , 2) - 4 * dxdy  * dxdy ;             x  = (-dxdy  + Math.sqrt (discriminant)) / (2 * dxdy );             x  = (-dxdy  - Math.sqrt (discriminant)) / (2 * dxdy );             count = 3;             vertex  = c ;             vertex  = vertex ;             for (int k = 0 ; k < 3 ; k++)             {                 r1 [k] = c [k] * Math.pow (x , count);                 vertex  += r1 [k];                 r2 [k] = c [k] * Math.pow (x , count);                 vertex  += r2 [k];                 count--;             }             dxdy2  = 2 * dxdy ;             dxdy2  = dxdy ;             double st[] = new double ;             st  = dxdy2  * x  + dxdy2 ;             st  = dxdy2  * x  + dxdy2 ;             if (discriminant >= 0)             {                 System.out.println ();                 System.out.println ("First Derivative Test:");                 System.out.println ("Returned Value 1: " + df.format (x ) + ", " + df.format (vertex ));                 System.out.println ("Returned Value 2: " + df.format (x ) + ", " + df.format (vertex ));                 System.out.println ();                 System.out.println ("Second Derivative Test:");                 System.out.println ("Returned Value 1: " + df.format (x ) + ", " + df.format (st ));                 System.out.println ("Returned Value 2: " + df.format (x ) + ", " + df.format (st ));                 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.

localmaxmin.java
Description:
 Code Version of this program Download
Filename:  localmaxmin.java
Filesize:  3.16 KB
Downloaded:  249 Time(s) Sponsor
Sponsor   Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First         Page 1 of 1  [ 1 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: