Find the monotonic interval of a function with python

Is there some convenient way for python to find the monotonic interval of a function, say, f(x)= e^x(x^3+2)?

Regards,
HY

Howdy Hongyi,

as far as I know, a function is monotonically increasing in a range, where it’s derivation (exists and) is positive.

Whereby a function is monotonically decreasing in a range, where it’s derivation (exists and) is negative.

This means, that a function is monotonic in a range, where it’s derivation (exists and) does NOT have a zero point (“root”), right?

There are several means for finding roots symbolically as well as numerically, e.g.:

from sympy.solvers import solve
from sympy import Symbol
from sympy import diff
from sympy import exp

x = Symbol("x", real=True)
solve(diff(exp(x) * (x**3+2)), x)

Cheers, Dominik

Agreed, this is the best that can be advised. Remember, however, that this is an empirical method. While it is true that a differentiable function whose derivative is always positive (non-zero) or always negative (non-zero) is monotonic, the converse does not hold. Think f(x) = x**3.

For simple cases, sympy also has inequality solvers which you can use on the derivative:

https://docs.sympy.org/latest/modules/solvers/inequalities.html

1 Like

PS: I was still editing my post above, when Jean already posted his comment. The example I initially posted, used the simpler function x^3+2 - with a comment, that such solvers might not be able to solve this for too complex functions (without further assistance); when I noticed, that sympy is able to solve the riddle for the function exp(x) * (x**3+2) on its own (and that Hongyi might as well have meant said function).

In this case, the differentiable function is f’(x) = 2 * x ** 2, which is always positve (greater than zero) except at point of zero. So, the function is monotonic. What’s wrong here?

Regards,
HY

In this case, the differentiable function is f’(x) = 2 * x ** 2, which is always positve (greater than zero) except at point of zero. So, the function is monotonic. What’s wrong here?

Nothing; it just means that looking at the points where the derivative is zero does not always give you the greatest intervals where the function is monotonic.

There is a difference between a “monotonic increase/decrease” and a “strict monotonic increase/decrease”; the latter excludes points, whose derivative is zero, the former not…

So, if you want to be mathematically precise, you have to have a detailed look at said “root” points. I think, Jean wanted to point that out, right?

There is a difference between a “monotonic increase/decrease” and a “ strict monotonic increase/decrease”; the latter excludes points, whose derivative is zero, the former not…

So, if you want to be mathematically precise, you have to have a detailed look at said “root” points. I think, Jean wanted to point that out, right?

Yes, except that in the x**3 example above, the function is strictly monotonic in the strict (pun intended) mathematical sense, but its derivative is still 0 at the point 0 :slight_smile:

1 Like