# Root Finding in MATLAB: Video Tutorial

URL: https://yarpiz.com/root-finding-yptnm190520s01
Author: Mostapha Kalami Heris
Published: 2019-07-02
Updated: 2026-10-30
Topics: Courses

> Solve polynomial, nonlinear and linear equations in MATLAB with roots, fzero, fsolve and linsolve. Compare the worked examples and learn how to check residuals.

This video tutorial shows how to solve equations numerically with the built-in functions of MATLAB. It covers polynomial equations, a single nonlinear equation, systems of nonlinear equations and systems of linear equations. It is part of the full Numerical Computations course, which you can find on [this page](https://yarpiz.com/numerical-computations-in-matlab-video-tutorial-572).

## The problem

A root of a function $f$ is a value of $x$ where the function becomes zero:

$$
f(x) = 0
$$

MATLAB has a different tool for each form of this problem. When $f$ is a polynomial, all of its roots can be found at once. When $f$ is a general nonlinear function, a solver starts from an initial guess and searches for one root. When there are several equations and several unknowns, $x$ and $f(x)$ become vectors. The last video treats the special case of a linear system, $Ax = b$, which strictly speaking is equation solving rather than root finding.

## Watch online

The tutorial has four sections. They are embedded here as a playlist on YouTube.

::: video {playlist="PLgH3sgdvgO4QJBk7zYUHHo3HQtcFqk6vC" youtube="yCJ73SDvzwk" count="4" title="Root Finding in MATLAB: Video Tutorial"}
:::

## What each video covers

### Section 1: Roots of polynomials with `roots`

Finding roots of polynomials using `roots` ([YouTube](https://youtu.be/yCJ73SDvzwk)). This first video also opens the whole course with a short overview of its topics.

MATLAB stores a polynomial as a vector of its coefficients, from the highest power down to the constant term. The polynomial

$$
a_n x^n + a_{n-1} x^{n-1} + \dots + a_1 x + a_0
$$

is the vector `[an an-1 ... a1 a0]`. The first example is the quadratic equation $x^2 - x - 1 = 0$, with the coefficient vector `[1 -1 -1]`. The video compares the output of `roots` with the quadratic formula, $x = (1 \pm \sqrt{5})/2$, and the two results agree (about $-0.618$ and $1.618$).

```matlab {title="Find the roots of a quadratic polynomial"}
p = [1 -1 -1];
roots(p)
```

The second example is the cubic equation $x^3 - 3x^2 + 4x - 5 = 0$. Here `roots` returns one real root (about $2.2134$) and a pair of complex conjugate roots. The same call works for a polynomial of any degree.

### Section 2: A nonlinear equation with `fzero`

Finding roots of nonlinear functions using `fzero` ([YouTube](https://youtu.be/UKa7rxPpzqw)). The function `fzero` finds one root of a single equation in one unknown. It takes the function and an initial point $x_0$ where the search starts.

The example is the equation

$$
x^2 - 5e^{-x} + 1 = 0
$$

The function is defined as an anonymous function with element-wise operators, so it also works with vectors. A plot made with `ezplot` shows a root close to 1, and `fzero` started from 0 returns about $0.9581$.

```matlab {title="Find a nonlinear root with fzero"}
f = @(x) x.^2 - 5*exp(-x) + 1;
x = fzero(f, 0)
```

The video then shows why the starting point matters for an equation with many roots. With the cosine function, different starting points lead `fzero` to different roots, such as $-\pi/2$ or $\pi/2$. Starting from several points can reveal different roots, but it does not prove that every root has been found.

### Section 3: Systems of nonlinear equations with `fsolve`

Solving systems of nonlinear equations using `fsolve` ([YouTube](https://youtu.be/hfUrZ3F8b2A)). The limit of `fzero` is one equation with one unknown. For a system $F(x) = 0$, where $x$ and $F$ are vectors, MATLAB offers `fsolve`.

The example system has two unknowns, $x$ and $y$:

$$
\begin{aligned}
xy - 5y + 10 &= 0 \\
x^3 - y^2 - 2 &= 0
\end{aligned}
$$

First the unknowns are renamed $x_1$ and $x_2$, and every equation is written with zero on the right-hand side. The function then returns a column vector with one element per equation. Started from $(1, 1)$, `fsolve` reports that the equation is solved and returns $x_1 = 3$ and $x_2 = 5$. The second output gives the function value at the solution, which is almost zero.

```matlab {title="Solve a system of nonlinear equations"}
F = @(x) [x(1)*x(2) - 5*x(2) + 10
          x(1)^3 - x(2)^2 - 2];
[x, fval] = fsolve(F, [1; 1])
```

### Section 4: Systems of linear equations with `linsolve`

Solving systems of linear equations using `linsolve` ([YouTube](https://youtu.be/E7RUl6ll9oE)). A linear system has the form $Ax = b$, where $A$ is an $n \times n$ matrix of coefficients, $b$ is the right-hand side and $x$ holds the unknowns. When $A$ is not singular, the solution is $x = A^{-1}b$.

The example system is:

$$
\begin{aligned}
x - 2y + 3z &= 2 \\
x + 4y - 5z &= 6 \\
5x - y - z &= 12
\end{aligned}
$$

The video solves it in three mathematically equivalent ways: with `inv(A)*b`, with the backslash operator `A\b`, and with the built-in function `linsolve(A, b)`. All three give $x = 3$, $y = 2$ and $z = 1$. For computation, solve the system with `A\b` or `linsolve` rather than explicitly forming the inverse.

```matlab {title="Solve a linear system with linsolve"}
A = [1 -2 3; 1 4 -5; 5 -1 -1];
b = [2; 6; 12];
x = linsolve(A, b)
```

## Which function to use

- `roots`: all roots of a polynomial, given its coefficients.
- `fzero`: one root of one nonlinear equation in one unknown, near a starting point.
- `fsolve`: a solution of a system of nonlinear equations, from a starting point. It is part of Optimization Toolbox, while the other three functions are in core MATLAB.
- `linsolve` or `A\b`: the solution of a system of linear equations.

For the classic iterative methods behind these tools, see [Numerical Root Finding Methods in Python and MATLAB](https://yarpiz.com/numerical-root-finding-methods-in-python-and-matlab-yprf191221), which covers the bisection, secant and Newton-Raphson methods.

## Check the result

Substitute each computed root into the original function. For the system examples, inspect `fval` or the residual `A*x - b`. A small residual shows how closely the equations are satisfied, while a poorly conditioned problem can still make the solution sensitive to small changes in the inputs. Treat each starting point as a separate experiment when a nonlinear system may have several solutions.

## How to cite

Mostapha Kalami Heris, Root Finding in MATLAB: Video Tutorial (YPTNM190520S01), Yarpiz, 2019. https://yarpiz.com/root-finding-yptnm190520s01
