Bird
0
0

What is the output of this code snippet?

medium📝 Predict Output Q13 of 15
SciPy - Advanced Optimization
What is the output of this code snippet?
from scipy.optimize import minimize

obj_fun = lambda x: (x[0]-2)**2 + (x[1]-3)**2
constraint = {'type': 'ineq', 'fun': lambda x: x[0] + x[1] - 4}
result = minimize(obj_fun, [0, 0], constraints=constraint, method='SLSQP')
print(round(result.fun, 2))
A0.00
B1.00
C2.00
D4.00
Step-by-Step Solution
Solution:
  1. Step 1: Understand the objective function

    The function measures distance squared from point (2,3).
  2. Step 2: Apply the constraint and minimize

    The constraint requires x[0] + x[1] >= 4. The closest point to (2,3) on this line is (1,3), giving value (1-2)^2+(3-3)^2=1.
  3. Final Answer:

    1.00 -> Option B
  4. Quick Check:

    Minimum distance squared with constraint = 1.00 [OK]
Quick Trick: Check closest point on constraint line to target point [OK]
Common Mistakes:
  • Ignoring the constraint
  • Rounding incorrectly
  • Confusing objective function value with variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes