Finding the minimum of a function on a closed interval with Python
<p>Updated: How do I find the minimum of a function on a closed interval [0,3.5] in Python? So far I found the max and min but am unsure how to filter out the minimum from here. </p>

<pre><code>import sympy as sp

x = sp.symbols('x')

f = (x**3 / 3) - (2 * x**2) + (3 * x) + 1

fprime = f.diff(x)

all_solutions = [(xx, f.subs(x, xx)) for xx in sp.solve(fprime, x)]

print (all_solutions)
</code></pre>

<p>Here's a possible solution using sympy:</p>

<pre><code>import sympy as sp

x = sp.Symbol('x', real=True)

f = (x**3 / 3) - (2 * x**2) - 3 * x + 1
#f = 3 * x**4 - 4 * x**3 - 12 * x**2 + 3

fprime = f.diff(x)

all_solutions = [(xx, f.subs(x, xx)) for xx in sp.solve(fprime, x)]
interval = [0, 3.5]
interval_solutions = filter(
    lambda x: x[0] &gt;= interval[0] and x[0] &lt;= interval[1], all_solutions)

print(all_solutions)
print(interval_solutions)
</code></pre>

<p><code>all_solutions</code> is giving you all points where the first derivative is zero, <code>interval_solutions</code> is constraining those solutions to a closed interval. This should give you some good clues to find minimums and maximums :-)</p>

python numpy sympy