Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
SciPy - Advanced Optimization
Identify the error in this code snippet:
import numpy as np
from scipy.optimize import least_squares

def fun(x):
    return x**2 - 4

res = least_squares(fun, x0=2, jac='3-point')
print(res.x)
A'jac' parameter value '3-point' is invalid; should be '3-point' without quotes
B'jac' parameter value '3-point' is invalid; correct value is '3-point' (string) but spelled '3-point' is wrong
CThere is no error in the code snippet
D'jac' parameter value '3-point' is invalid; correct value is '3-point' with quotes
Step-by-Step Solution
Solution:
  1. Step 1: Check valid values for 'jac' parameter

    The 'jac' parameter accepts '2-point', '3-point', 'cs' (complex step), callable, or None.
  2. Step 2: Identify if there is an error

    The code passes '3-point' as a string, which is valid. The code runs correctly.
  3. Final Answer:

    There is no error in the code snippet -> Option C
  4. Quick Check:

    'jac'='3-point' string is valid = B [OK]
Quick Trick: 'jac' accepts specific strings like '3-point' exactly [OK]
Common Mistakes:
  • Passing 'jac' without quotes
  • Using invalid strings for 'jac'
  • Confusing 'jac' with 'fun'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes