Math and Science with Python

Find the Minimum of a Function with scipy.optimize.minimize

Search for the input that makes a function smallest, choose a method, and keep the answer inside bounds.

  • Intermédiaire
  • 3 min de lecture
  • 1 exemple exécutable
Ce que vous allez apprendre
  • minimize with a starting point x0
  • Reading .x, .fun and .success
  • Methods and bounds
Results from the example
Bowl
  • (3, -1)
  • value 2
Rosenbrock
  • (1, 1)
  • BFGS
With bounds
  • (2, -0.5)
  • value 3.25

Search for the lowest point

Découvrez SciPy Optimize minimize in Python avec un exemple pratique modifiable et exécutable en ligne.

Points clés

  1. The function takes one array of parameters and returns one number.
  2. result.x is the best point and result.fun the value there.
  3. method="BFGS" picks the algorithm; the default depends on bounds and constraints.
  4. With bounds, the minimum of f moves to the edge, (2, -0.5), with value 3.25.

Quand l’utiliser ?

  • Solve equations, integrals and differential equations in a few lines.
  • Fit models to measured data and analyse signals in science and engineering classes.
  • Check homework answers numerically and see where a hand calculation went wrong.

Code d’exemple

main.py
import numpy as np
from scipy.optimize import minimize

# A bowl-shaped function with its minimum at (3, -1)
def f(p):
    x, y = p
    return (x - 3)**2 + (y + 1)**2 + 2

result = minimize(f, x0=[0, 0])
print("Minimum at:", result.x.round(4).tolist())
print(f"Minimum value: {result.fun:.4f}")
print("Success:", result.success)

# The Rosenbrock function: a curved valley, minimum at (1, 1)
def rosen(p):
    x, y = p
    return (1 - x)**2 + 100 * (y - x**2)**2

result = minimize(rosen, x0=[-1.2, 1], method="BFGS")
print("Rosenbrock minimum:", result.x.round(4).tolist())

# Bounds: keep x between 0 and 2, y between -0.5 and 0.5
result = minimize(f, x0=[1, 0], bounds=[(0, 2), (-0.5, 0.5)])
print("With bounds:", result.x.round(4).tolist(), f"value {result.fun:.4f}")
Obtenir un bouton Run pour cet exemple

Résultat de l’exécution

Minimum at: [3.0, -1.0]
Minimum value: 2.0000
Success: True
Rosenbrock minimum: [1.0, 1.0]
With bounds: [2.0, -0.5] value 3.2500

Explication du code

Cet exemple montre SciPy Optimize minimize in Python en pratique. Modifiez les valeurs d’entrée et relancez le code pour voir ce qui change.

Modifiez les valeurs et exécutez le programme avec le compilateur Python en ligne CodeUtility, sans installation locale.

Exercices d’approfondissement

Essayez de modifier le code en suivant les pistes ci-dessous. Changer le code et observer le résultat vous fera bien mieux comprendre la leçon que la simple lecture.

  1. Change the function or the data and predict the result before running.
  2. Compare the numerical answer with the exact one from SymPy or a hand calculation.
  3. Plot the result with Matplotlib to see what the numbers mean.
Exécuter dans le compilateur Python →

Leçon terminée ?

Marquez-la comme apprise pour suivre votre progression.

S'entraîner avec un défi

Utilisez le contenu de cette leçon pour résoudre un vrai problème. Chaque défi s'exécute dans le navigateur, avec des tests instantanés et une revue par l'IA.

Tous les défis

Outils pour cette leçon