Bird
Raised Fist0
SciPydata~10 mins

Root finding (root, brentq) in SciPy - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Root finding (root, brentq)
Define function f(x)
↓
Choose interval or initial guess
↓
Call root finding method
↓
Iterative search for root
↓
Check if f(root) approx 0
Yes No↓
Return root
We start with a function and an initial guess or interval. The root finding method iteratively searches for a point where the function value is zero, adjusting guesses until it finds the root.
Execution Sample
SciPy
from scipy.optimize import root, brentq

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

sol = root(f, 1.5)
root_brentq = brentq(f, 1, 2)
This code finds the root of f(x) = x^3 - x - 2 using two methods: root with initial guess 1.5, and brentq on interval [1, 2].
Execution Table
StepMethodInputFunction evalActionResult
1rootInitial guess x=1.5f(1.5)=1.875Check if close to 0Not close, iterate
2rootNext guess x=1.7f(1.7)=3.213Check if close to 0Not close, iterate
3rootNext guess x=1.4f(1.4)=0.744Check if close to 0Not close, iterate
4rootNext guess x=1.521f(1.521)=0.045Check if close to 0Close, iterate
5rootNext guess x=1.5214f(1.5214)=0.0001Check if close to 0Very close, stop
6rootReturn rootf(1.5214)=~0Root foundRoot approx 1.5214
1brentqInterval [1, 2]f(1)=-2, f(2)=4Check sign changeSign change detected
2brentqMidpoint 1.5f(1.5)=1.875Check signRoot in [1,1.5]
3brentqMidpoint 1.25f(1.25)=-0.797Check signRoot in [1.25,1.5]
4brentqMidpoint 1.375f(1.375)=-0.123Check signRoot in [1.375,1.5]
5brentqMidpoint 1.4375f(1.4375)=0.388Check signRoot in [1.375,1.4375]
6brentqMidpoint 1.40625f(1.40625)=0.132Check signRoot in [1.375,1.40625]
7brentqMidpoint 1.390625f(1.390625)=0.004Check signRoot in [1.375,1.390625]
8brentqMidpoint 1.3828125f(1.3828125)=-0.059Check signRoot in [1.3828125,1.390625]
9brentqMidpoint 1.38671875f(1.38671875)=-0.027Check signRoot in [1.38671875,1.390625]
10brentqMidpoint 1.388671875f(1.388671875)=-0.011Check signRoot in [1.388671875,1.390625]
11brentqMidpoint 1.3896484375f(1.3896484375)=-0.003Check signRoot in [1.3896484375,1.390625]
12brentqMidpoint 1.39013671875f(1.39013671875)=0.0005Check signRoot in [1.3896484375,1.39013671875]
13brentqMidpoint 1.389892578125f(1.389892578125)=-0.001Check signRoot in [1.389892578125,1.39013671875]
14brentqMidpoint 1.3900146484375f(1.3900146484375)=-0.0003Check signRoot in [1.3900146484375,1.39013671875]
15brentqMidpoint 1.39007568359375f(1.39007568359375)=0.0001Check signRoot in [1.3900146484375,1.39007568359375]
16brentqMidpoint 1.390045166015625f(1.390045166015625)=-0.0001Check signRoot in [1.390045166015625,1.39007568359375]
17brentqMidpoint 1.3900604248046875f(1.3900604248046875]=0.0Check signRoot found
18brentqReturn rootf(1.3900604248046875)=0Root foundRoot approx 1.39006
💡 root stops when function value is close to zero; brentq stops when interval is narrowed to root
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
root_guess1.51.71.41.5211.52141.52141.5214
brentq_interval_low111.251.3751.3751.38964843751.390045166015625
brentq_interval_high21.51.51.51.43751.390136718751.39007568359375
function_valuef(1.5)=1.875f(1.7)=3.213f(1.4)=0.744f(1.521)=0.045f(1.5214)=0.0001f(1.5214)=~0f(1.5214)=~0
Key Moments - 3 Insights
Why does the root method try different guesses instead of just returning the first one?
Because the first guess usually does not make the function zero. The method tries new guesses to get closer to the root, as shown in execution_table rows 1-5.
Why does brentq need an interval where the function changes sign?
Brentq relies on the function values at interval ends having opposite signs to guarantee a root inside. This is shown in execution_table row 6 where f(1) and f(2) have opposite signs.
How do we know when the root finding stops?
The process stops when the function value at the guess is very close to zero or the interval is very small, as shown in execution_table rows 5 and 17-18.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table for the root method. What is the function value at the final root guess?
AApproximately 0
BApproximately 1.5
CApproximately 3.2
DApproximately -2
💡 Hint
Check the 'Function eval' column at step 6 for root method.
At which step does brentq first detect a sign change in the function values?
AStep 1
BStep 6
CStep 2
DStep 10
💡 Hint
Look at the 'Action' column for brentq method in execution_table row 6.
If the initial interval for brentq was [2, 3] instead of [1, 2], what would happen?
AThe method would find the root faster
BThe method would fail because no sign change in [2,3]
CThe method would return 2 immediately
DThe method would ignore the interval and use initial guess
💡 Hint
Brentq requires a sign change in the interval to work, see execution_table row 6.
Concept Snapshot
Root finding finds x where f(x)=0.
root() uses initial guess and iterative solver.
brentq() needs interval with sign change.
Both stop when f(x) approx 0.
Use root for general cases, brentq for bracketed roots.
Full Transcript
This lesson shows how root finding works using scipy's root and brentq methods. We start with a function and either an initial guess or an interval. The root method tries guesses and checks function values until it finds a root where f(x) is near zero. Brentq requires an interval where the function changes sign, then narrows the interval by checking midpoints until it finds the root. The execution table traces each step, showing guesses, function values, and decisions. Key moments clarify why guesses change, why sign changes matter, and when the process stops. The visual quiz tests understanding of function values at steps, sign change detection, and interval requirements. The snapshot summarizes usage and behavior of root and brentq methods.

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