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

On this page
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.
The problem
A root of a function is a value of where the function becomes zero:
MATLAB has a different tool for each form of this problem. When is a polynomial, all of its roots can be found at once. When 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, and become vectors. The last video treats the special case of a linear system, , 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.
What each video covers
Section 1: Roots of polynomials with roots
Finding roots of polynomials using roots (YouTube (opens in a new tab)). 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
is the vector [an an-1 ... a1 a0]. The first example is the quadratic equation , with the coefficient vector [1 -1 -1]. The video compares the output of roots with the quadratic formula, , and the two results agree (about and ).
p = [1 -1 -1];
roots(p)The second example is the cubic equation . Here roots returns one real root (about ) 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 (opens in a new tab)). The function fzero finds one root of a single equation in one unknown. It takes the function and an initial point where the search starts.
The example is the equation
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 .
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 or . 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 (opens in a new tab)). The limit of fzero is one equation with one unknown. For a system , where and are vectors, MATLAB offers fsolve.
The example system has two unknowns, and :
First the unknowns are renamed and , 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 , fsolve reports that the equation is solved and returns and . The second output gives the function value at the solution, which is almost zero.
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 (opens in a new tab)). A linear system has the form , where is an matrix of coefficients, is the right-hand side and holds the unknowns. When is not singular, the solution is .
The example system is:
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 , and . For computation, solve the system with A\b or linsolve rather than explicitly forming the inverse.
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.linsolveorA\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, 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
If you use this code or tutorial in your work, articles or publications, please cite it.
Mostapha Kalami Heris, Root Finding in MATLAB: Video Tutorial (YPTNM190520S01), Yarpiz, 2019. https://yarpiz.com/root-finding-yptnm190520s01@misc{yarpiz-yptnm190520s01,
author = {{Mostapha Kalami Heris}},
title = {{Root Finding in MATLAB: Video Tutorial}},
howpublished = {Yarpiz},
year = {2019},
note = {Code YPTNM190520S01},
url = {https://yarpiz.com/root-finding-yptnm190520s01}
}Published Updated

Comments