0
0
SciPydata~20 mins

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

Choose your learning style9 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.