Bird
0
0

Given the following code snippet, what will be printed during the optimization?

medium📝 Predict Output Q13 of 15
SciPy - Advanced Optimization
Given the following code snippet, what will be printed during the optimization?
from scipy.optimize import minimize

def callback(xk):
    print(f"Step: {xk[0]:.2f}, {xk[1]:.2f}")

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

res = minimize(func, [0, 0], callback=callback)
AMultiple lines showing parameter values approaching (1.00, 2.00)
BOnly one line showing initial parameters [0.00, 0.00]
CNo output because callback is ignored
DError because callback function has wrong signature
Step-by-Step Solution
Solution:
  1. Step 1: Understand the callback usage in minimize

    The callback prints the current parameters at each iteration during optimization.
  2. Step 2: Analyze the expected output

    Since the function minimizes distance to (1,2), parameters will update stepwise, printing multiple lines approaching (1.00, 2.00).
  3. Final Answer:

    Multiple lines showing parameter values approaching (1.00, 2.00) -> Option A
  4. Quick Check:

    Callback prints each step params [OK]
Quick Trick: Callback prints each iteration's parameters [OK]
Common Mistakes:
  • Assuming callback prints only once
  • Thinking callback is ignored by minimize
  • Believing callback signature is incorrect here

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes