Bird
Raised Fist0
SciPydata~20 mins

Root finding (root, brentq) in SciPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Root Finding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of root finding with scipy.optimize.root
What is the output of this code snippet that finds the root of the function f(x) = x**3 - 1 using scipy.optimize.root starting from x0=0.5?
SciPy
from scipy.optimize import root

def f(x):
    return x**3 - 1

sol = root(f, 0.5)
print(round(sol.x[0], 4))
A0.5
B1.0
C-1.0
D0.0
Attempts:
2 left
💡 Hint
Think about the cube root of 1.
❓ data_output
intermediate
2:00remaining
Number of iterations in brentq root finding
Using scipy.optimize.brentq to find the root of f(x) = cos(x) - x in the interval [0, 1], how many iterations does the solver perform?
SciPy
from scipy.optimize import brentq
import numpy as np

def f(x):
    return np.cos(x) - x

root, result = brentq(f, 0, 1, full_output=True)
print(result.iterations)
A10
B3
C7
D5
Attempts:
2 left
💡 Hint
Brent's method is efficient and usually converges quickly.
🧠 Conceptual
advanced
2:00remaining
Why brentq requires a sign change interval
Why does the scipy.optimize.brentq method require the function values at the interval endpoints to have opposite signs?
ABecause brentq uses the Intermediate Value Theorem which guarantees a root if the function changes sign over the interval.
BBecause brentq only works with positive functions.
CBecause brentq uses derivative information which requires sign change.
DBecause brentq approximates the function with a polynomial that must cross zero.
Attempts:
2 left
💡 Hint
Think about what guarantees a root exists in an interval.
🔧 Debug
advanced
2:00remaining
Identify the error in root finding code
What error will this code produce when trying to find the root of f(x) = log(x) - 2 using scipy.optimize.root starting from x0=0?
SciPy
from scipy.optimize import root
import numpy as np

def f(x):
    return np.log(x) - 2

sol = root(f, 0)
print(sol.x)
ARuntimeWarning: divide by zero encountered in log
BTypeError: 'float' object is not iterable
CValueError: math domain error
DNo error, prints root
Attempts:
2 left
💡 Hint
Consider the domain of the log function at zero.
🚀 Application
expert
3:00remaining
Find root of a noisy function using brentq
Given the noisy function f(x) = x^2 - 4 + noise, where noise is a small random value, which approach best finds a root near x=2 using scipy.optimize.brentq?
SciPy
import numpy as np
from scipy.optimize import brentq

np.random.seed(0)
noise = lambda: np.random.normal(0, 0.1)

def f(x):
    return x**2 - 4 + noise()

# Which code snippet correctly finds the root near 2?
Abrentq(f, 1, 3)
Bbrentq(f, 0, 1)
Cbrentq(lambda x: x**2 - 4, 1, 3)
Dbrentq(lambda x: x**2 - 4 + 0.5, 1, 3)
Attempts:
2 left
💡 Hint
Brentq requires the function to have opposite signs at the interval ends and be continuous.

Practice

(1/5)
1. What is the main purpose of the scipy.optimize.root function in root finding?
easy
A. To calculate the maximum value of a function
B. To integrate a function over an interval
C. To find where a function equals zero using an initial guess
D. To sort an array of numbers

Solution

  1. Step 1: Understand the purpose of root finding

    Root finding means locating points where a function's value is zero.
  2. Step 2: Identify what scipy.optimize.root does

    This function finds roots starting from an initial guess, aiming to solve f(x) = 0.
  3. Final Answer:

    To find where a function equals zero using an initial guess -> Option C
  4. Quick Check:

    Root finding = find zero points [OK]
Hint: Root means function value zero, root() finds it from guess [OK]
Common Mistakes:
  • Confusing root finding with integration
  • Thinking root finds maxima or minima
  • Assuming root sorts data
2. Which of the following is the correct way to import the brentq function from scipy.optimize?
easy
A. from scipy.optimize import brentq
B. import brentq from scipy.optimize
C. from scipy import brentq
D. import scipy.optimize.brentq

Solution

  1. Step 1: Recall Python import syntax for functions

    To import a function from a module, use: from module import function.
  2. Step 2: Apply to brentq from scipy.optimize

    The correct syntax is: from scipy.optimize import brentq.
  3. Final Answer:

    from scipy.optimize import brentq -> Option A
  4. Quick Check:

    Correct import syntax = from module import function [OK]
Hint: Use 'from module import function' to import specific functions [OK]
Common Mistakes:
  • Using 'import function from module' which is invalid
  • Trying to import directly from scipy without optimize
  • Using dot notation in import statement incorrectly
3. What will be the output of this code snippet?
from scipy.optimize import brentq

def f(x):
    return x**3 - 2*x - 5

root = brentq(f, 2, 3)
print(round(root, 3))
medium
A. 2.095
B. 2.0946
C. 2.0945
D. 2.0946 (rounded to 4 decimals)

Solution

  1. Step 1: Understand the function and interval

    The function is f(x) = x³ - 2x - 5. We look for root between 2 and 3.
  2. Step 2: Use brentq to find root and round output

    Using brentq finds root near 2.0945514815. Rounded to 3 decimals is 2.095.
  3. Step 3: Check rounding precision in options

    Options B (2.0946), C (2.0945), D (notes 4 decimals) are near unrounded root, but print(round(root,3)) outputs 2.095.
  4. Final Answer:

    2.095 -> Option A
  5. Quick Check:

    Root near 2.095 rounded 3 decimals = 2.095 [OK]
Hint: brentq finds root in interval; round final answer carefully [OK]
Common Mistakes:
  • Confusing rounding digits
  • Using wrong interval causing error
  • Misunderstanding brentq output as exact integer
4. Identify the error in this code snippet and choose the correct fix:
from scipy.optimize import brentq

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

root = brentq(g, 3, 5)
print(root)
medium
A. No error; code runs fine
B. Interval must include a sign change; use brentq(g, 1, 3)
C. Function g(x) is incorrect; change to g(x) = x**2 + 4
D. Import statement is wrong; use import brentq from scipy.optimize

Solution

  1. Step 1: Check function values at interval ends

    g(3) = 9 - 4 = 5 (positive), g(5) = 25 - 4 = 21 (positive). No sign change.
  2. Step 2: Understand brentq requires sign change

    brentq needs f(a) and f(b) to have opposite signs to find root.
  3. Step 3: Fix interval to include root

    Use interval (1, 3) because g(1) = -3 (negative), g(3) = 5 (positive), sign change exists.
  4. Final Answer:

    Interval must include a sign change; use brentq(g, 1, 3) -> Option B
  5. Quick Check:

    brentq needs sign change in interval [OK]
Hint: Check function signs at interval ends before brentq [OK]
Common Mistakes:
  • Ignoring sign change requirement
  • Changing function unnecessarily
  • Incorrect import syntax
5. You want to find the root of h(x) = cos(x) - x using scipy.optimize.root. Which initial guess is most appropriate and why?
from scipy.optimize import root
import numpy as np

def h(x):
    return np.cos(x) - x

sol = root(h, ?)
print(sol.x)
hard
A. -1, because negative guesses always work better
B. 2, because cos(2) is near zero
C. 5, because root is always positive and large
D. 0.5, because root is near where cos(x) crosses x close to 0.5

Solution

  1. Step 1: Understand the function behavior

    h(x) = cos(x) - x crosses zero near 0.7 (between 0 and 1).
  2. Step 2: Choose initial guess close to root

    Initial guess 0.5 is close to root, helping root() converge quickly.
  3. Step 3: Evaluate other options

    2 and 5 are far from root; -1 is negative and root is positive, so less suitable.
  4. Final Answer:

    0.5, because root is near where cos(x) crosses x close to 0.5 -> Option D
  5. Quick Check:

    Good initial guess near root speeds root() convergence [OK]
Hint: Pick initial guess near expected root for root() [OK]
Common Mistakes:
  • Choosing initial guess far from root
  • Assuming root is always positive and large
  • Believing negative guesses always work better
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard