# Numerical Root Finding Methods in Python and MATLAB: Video Tutorial

URL: https://yarpiz.com/numerical-root-finding-methods-in-python-and-matlab-yprf191221
Author: Mostapha Kalami Heris
Published: 2020-01-13
Updated: 2026-10-30
Topics: Courses

> Implement bisection, a bracketed secant update and Newton-Raphson in Python and MATLAB. Compare assumptions, stopping criteria and two worked equations.

Numerical root finding approximates solutions of $f(x) = 0$. A root can cross the horizontal axis or touch it without a sign change. Simple equations have exact formulas. A linear equation $ax + b = 0$, with $a \ne 0$, has the root $x = -b/a$, and the quadratic formula solves a quadratic equation. General formulas using radicals also exist for cubic and fourth degree polynomials, but no such formula covers every polynomial of degree five or higher. Numerical methods also handle many nonlinear equations for which an exact expression is unavailable or impractical.

This free course covers three methods, from theory to implementation, in both Python and MATLAB, written from scratch. Mostapha Kalami Heris teaches the three videos, with a total length of about 1 hour 15 minutes.

## The three methods

- **Bisection method:** starts from an interval $[a, b]$ in which $f$ changes sign and halves the interval at every step.
- **Bracketed secant update (called the secant method in the course):** starts from an interval with a sign change and uses the zero of the line through $(a, f(a))$ and $(b, f(b))$. Because the implementation preserves the bracket, it follows the false position, or regula falsi, approach rather than the usual unbracketed secant iteration.
- **Newton-Raphson method:** also known as Newton's method. It starts from a single point, not an interval, and follows the tangent line of the function to the next guess.

For a continuous function, the interval methods preserve a sign change and keep at least one root bracketed. Bisection steadily reduces the interval, while false position can make slow progress when one endpoint hardly moves. Newton's method does not preserve a bracket. Near a simple root it can converge quickly, but a poor starting point or a small derivative can cause failure.

## The test problems

All three methods are tested on the same two equations, so their results can be compared:

$$
x^2 - x - 1 = 0
\qquad \text{and} \qquad
x^2 - e^{-x} - 1 = 0
$$

The first equation is the characteristic equation of the Fibonacci sequence. Its positive root is the golden ratio, $\varphi = (1 + \sqrt{5})/2 \approx 1.618$, so the numerical answer is easy to check. The second equation has no simple closed form solution, and it has a root near $x = 1.147$.

## Part 1: The bisection method

The first video (35:42) introduces the root finding problem and then the bisection method. If $f$ is continuous and $f(a)$ and $f(b)$ have different signs, which means $f(a)\,f(b) < 0$, then the interval $[a, b]$ contains at least one root. With no other knowledge about the function, the midpoint is a sensible guess:

$$
c = \frac{a + b}{2}
$$

If $f(c)$ is close enough to zero, $c$ is accepted as the root. Otherwise, $c$ replaces the endpoint whose function value has the same sign as $f(c)$. The new interval is half as long and still contains a sign change. The steps repeat until $f(c)$ is small enough or a maximum number of iterations is reached.

The video then writes the method in Python (from 12:05). A first, simple version uses an exact test $f(c) = 0$ and a fixed number of iterations. The video then improves it with two parameters:

- `tol`, a tolerance. The search stops when $|f(c)|$ or the interval width $|b - a|$ falls below it. It also returns an endpoint at once if that endpoint is already a root.
- `maxit`, the maximum number of iterations, with a default of 100.

The video shows how these two parameters work together. A very small tolerance with only 10 iterations does not reach the required accuracy, and more iterations fix this. Finally, the same function is written in MATLAB (from 26:50), together with two short scripts that solve the two test equations.

## Part 2: The bracketed secant method

The second video (16:38) keeps the structure of the bisection code and changes only how the next point is chosen. It draws the line through $(a, f(a))$ and $(b, f(b))$:

$$
y = \frac{f(b) - f(a)}{b - a}\,(x - a) + f(a)
$$

Setting $y = 0$ gives the point where this line crosses the $x$ axis:

$$
c = \frac{a\,f(b) - b\,f(a)}{f(b) - f(a)}
$$

This looks like a weighted average of $a$ and $b$, and it always lies between them when $f(a)$ and $f(b)$ have different signs. As in bisection, $c$ then replaces the endpoint with the same sign, so the root stays inside the interval.

The course and its filenames use the name "secant." The distinction matters when comparing this code with other implementations: the conventional secant method keeps the two most recent iterates and does not necessarily keep a sign change.

The video also shows that bisection is a special case of this rule. If $f(a)$ and $f(b)$ are replaced by their signs, $-1$ and $+1$, the formula becomes the midpoint $(a + b)/2$. Bisection uses only the sign of the function, while the secant rule also uses its size, which can make convergence faster.

In both Python and MATLAB, the code is a copy of the bisection code where only the update line changes:

```python {title="Secant update within a root bracket"}
c = (a*fb - b*fa)/(fb - fa)
```

## Part 3: The Newton-Raphson method

The third video (22:55) explains Newton's method. It starts from one point $x_0$ and approximates the function there with its tangent line, which has the same value and the same slope as $f$ at that point:

$$
y = f(x_0) + f'(x_0)\,(x - x_0)
$$

The next guess is where this line crosses zero. Repeating the step gives the recurrence

$$
x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)}
$$

The video points out the limits of the method. If $f'(x_n)$ is zero or very close to zero, the step cannot be computed, and in general there is no guarantee of convergence. When the method does converge, it is very fast. The video also mentions that some optimization techniques are derived from this equation.

The code does not need a formula for the derivative. It estimates $f'(x)$ with the central difference

$$
f'(x) \approx \frac{f(x + \Delta x) - f(x - \Delta x)}{2\,\Delta x}
$$

with $\Delta x = 10^{-6}$. The loop stops when $|f(x)|$ falls below the tolerance, or when the estimated derivative is too small. The method is written first in Python (from 06:39) and then in MATLAB (from 14:47). Both versions solve the two test equations from the starting point $x_0 = 2$ and reach the same roots as the two interval methods.

## Course files

The download for this post contains the code written in the videos. The MATLAB folder has the functions `Bisection.m`, `Secant.m` and `NewtonRaphson.m`, and two test scripts for each method (`bisect1.m`, `bisect2.m`, `secant1.m`, `secant2.m`, `newton1.m` and `newton2.m`). The Python folder has `bisection.py`, `secant.py` and `newton.py`. Each Python file defines the method and solves both test equations when it runs.

## Who this course is for

- Students of mathematics, engineering, computer science or related fields
- Professionals in engineering, data analysis, finance and scientific research
- Self-learners interested in numerical mathematics

The course assumes basic mathematics, including functions and derivatives. Familiarity with Python and MATLAB helps, but every implementation step is explained.

## Check convergence

Check both the residual $|f(x)|$ and the change in the iterate or bracket width. Reaching `maxit` is a stop condition, not proof of convergence. Bracketing depends on continuity, so a sign change across a discontinuity does not establish a root. For the finite difference derivative in Newton's method, changing the step size can expose sensitivity to rounding or function noise.

## Watch online

The three videos are available on the Yarpiz YouTube channel. The playlist below contains all parts of the series.

::: video {playlist="PLgH3sgdvgO4QXFTC27OOog4RL2NCUkC0u" title="Numerical Root Finding Methods in Python and MATLAB: Video Tutorial"}
:::

## How to cite

Mostapha Kalami Heris, Numerical Root Finding Methods in Python and MATLAB: Video Tutorial (YPRF191221), Yarpiz, 2020. https://yarpiz.com/numerical-root-finding-methods-in-python-and-matlab-yprf191221
