Bird
0
0

Why does this code raise an error?

medium📝 Debug Q7 of 15
SciPy - Advanced Optimization
Why does this code raise an error?
from scipy.optimize import minimize

obj = lambda x: x[0]**2
cons = {'type': 'ineq', 'fun': lambda x: x[0] - 1}
result = minimize(obj, 0, constraints=cons)
AInitial guess must be an array-like, not a scalar.
BConstraint function must return boolean, not scalar.
CObjective function must have two variables.
DConstraint dictionary must be a list.
Step-by-Step Solution
Solution:
  1. Step 1: Check the initial guess type

    Initial guess should be array-like (e.g., list or numpy array), not a scalar.
  2. Step 2: Identify the error cause

    Passing 0 (scalar) causes dimension mismatch error in minimize.
  3. Final Answer:

    Initial guess must be an array-like, not a scalar. -> Option A
  4. Quick Check:

    Initial guess must be array-like [OK]
Quick Trick: Initial guess must be list or array [OK]
Common Mistakes:
  • Passing scalar instead of array for initial guess
  • Expecting boolean return from constraint
  • Assuming objective needs two variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes