Challenge - 5 Problems
Root Finding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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))
Attempts:
2 left
💡 Hint
Think about the cube root of 1.
✗ Incorrect
The function f(x) = x^3 - 1 has a root at x=1 because 1^3 - 1 = 0. Starting from 0.5, the root finder converges to 1.0.
❓ data_output
intermediate2: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)
Attempts:
2 left
💡 Hint
Brent's method is efficient and usually converges quickly.
✗ Incorrect
The brentq method typically converges in about 5 iterations for this function and interval.
🧠 Conceptual
advanced2: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?
Attempts:
2 left
💡 Hint
Think about what guarantees a root exists in an interval.
✗ Incorrect
Brent's method relies on the Intermediate Value Theorem, which states that if a continuous function changes sign over an interval, there must be a root inside.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Consider the domain of the log function at zero.
✗ Incorrect
np.log(0) is undefined and causes a divide by zero warning during evaluation.
🚀 Application
expert3: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?
Attempts:
2 left
💡 Hint
Brentq requires the function to have opposite signs at the interval ends and be continuous.
✗ Incorrect
Because f includes random noise, it may not have a guaranteed sign change or continuity. Using the noise-free function x^2 - 4 ensures brentq works correctly.