Search code examples
pythonmathscientific-computing

Finding local min/max of a cubic function


I'm looking to program a Python function that takes in 6 variables, a, b, c, d, e, f, where a, b is the interval to compute on (e.g. [1, 3], all real numbers), and c, d, e, f are the coefficients of the cubic polynomial, i.e. f(x) = cx^3 + dx^2 + ex + f, and returns the local min/max on the interval [a, b].

I have a rough idea (although the computing time would be bad) of how to program this, where I create a new list of steps 0.01 or something similarly small from a to b, evaluate f at each value, then simply return the min/max of the list. This would take very long for a, b values that are very far apart.

What is the best way to go about making this? Are there any outside libraries for scientific/mathematical computing? Thank you.


Solution

  • For cubic function you can find positions of potential minumum/maximums without optimization but using differentiation:

    • get the first and the second derivatives
    • find zeros of the first derivative (solve quadratic equation)
    • check the second derivative in found points - sign tells whether that point is min, max or saddle point

    I think that differentiation should be in sympy package

    Also check whether problem statement assumes accounting for boundary values (as @Lakshay Garg notices in comments)