0
0
SciPydata~10 mins

Minimizing multivariate functions (minimize) in SciPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Minimizing multivariate functions (minimize)
Define function f(x, y)
Choose initial guess (x0, y0)
Call minimize(f, x0)
Algorithm iterates to find minimum
Check convergence
Return minimum
End
We start by defining the function to minimize, pick a starting point, then use minimize to find the lowest value by iterating until it converges.
Execution Sample
SciPy
from scipy.optimize import minimize

def f(x):
    return (x[0]-1)**2 + (x[1]+2)**2

result = minimize(f, [0, 0])
print(result.x)
This code finds the minimum of a function with two variables starting from (0,0).
Execution Table
StepCurrent guess (x, y)Function value f(x,y)ActionNotes
1[0.0, 0.0]5.0Evaluate f at startInitial guess
2[0.5, -1.0]1.25Move towards minimumImproved guess
3[0.75, -1.5]0.3125Move closerBetter guess
4[0.9, -1.8]0.058Move closerNear minimum
5[0.99, -1.98]0.0004Move closerVery close
6[1.0, -2.0]0.0ConvergedMinimum found
7--StopConvergence criteria met
💡 Algorithm stops when function value change is below threshold and guess is near minimum.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
x[0.0, 0.0][0.5, -1.0][0.75, -1.5][0.9, -1.8][0.99, -1.98][1.0, -2.0][1.0, -2.0]
f(x)5.01.250.31250.0580.00040.00.0
Key Moments - 3 Insights
Why does the algorithm start from [0, 0] and not directly at the minimum?
The algorithm needs a starting guess to begin searching. It improves the guess step-by-step as shown in the execution_table rows 1 to 6.
What tells the algorithm to stop iterating?
When the function value changes very little between steps and the guess is close to the minimum, the algorithm stops, as seen in step 6 and 7.
Why is the function value decreasing at each step?
Because the algorithm moves the guess towards points where the function value is lower, aiming to find the minimum, as shown in the decreasing f(x) values in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What is the current guess and function value?
A[0.5, -1.0], 1.25
B[0.9, -1.8], 0.058
C[0.75, -1.5], 0.3125
D[1.0, -2.0], 0.0
💡 Hint
Check the row labeled step 3 in the execution_table.
At which step does the algorithm converge to the minimum?
AStep 6
BStep 5
CStep 4
DStep 7
💡 Hint
Look for the step where the function value is 0.0 and action is 'Converged' in execution_table.
If the initial guess was changed to [2, 2], what would likely happen in the execution_table?
AThe function values would increase at each step.
BThe steps would start from [2, 2] with a higher function value and then decrease.
CThe algorithm would immediately find the minimum at step 1.
DThe algorithm would not run because initial guess must be [0, 0].
💡 Hint
Initial guess affects starting point and function value, as shown in variable_tracker start values.
Concept Snapshot
Use scipy.optimize.minimize to find minimum of multivariate functions.
Define function f(x) returning scalar.
Provide initial guess x0 as list or array.
minimize iterates guesses to reduce f(x).
Stops when convergence criteria met.
Result contains minimum location in result.x.
Full Transcript
We start by defining a function with multiple variables that we want to minimize. We pick a starting guess for the variables. Then we call scipy.optimize.minimize with the function and the guess. The algorithm evaluates the function at the guess, then moves the guess step-by-step to lower function values. It repeats this until it finds a point where the function value cannot be lowered further. This point is the minimum. The process is shown step-by-step in the execution table and variable tracker. Beginners often wonder why we need a starting guess and how the algorithm knows when to stop. The starting guess is necessary to begin searching, and the algorithm stops when changes become very small. The final result gives the minimum point found.