Courses

Root Finding in MATLAB: Video Tutorial

Watch the video
Comments

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

Illustration for Root Finding in MATLAB: Video Tutorial
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 ff is a value of xx where the function becomes zero:

f(x)=0f(x) = 0

MATLAB has a different tool for each form of this problem. When ff is a polynomial, all of its roots can be found at once. When ff 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, xx and f(x)f(x) become vectors. The last video treats the special case of a linear system, Ax=bAx = 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.

Root Finding in MATLAB: Video TutorialPlaylist, 4 videosWatch on YouTube (opens in a new tab)

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

anxn+an−1xn−1+⋯+a1x+a0a_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 x2−x−1=0x^2 - x - 1 = 0, with the coefficient vector [1 -1 -1]. The video compares the output of roots with the quadratic formula, x=(1±5)/2x = (1 \pm \sqrt{5})/2, and the two results agree (about −0.618-0.618 and 1.6181.618).

Find the roots of a quadratic polynomialMATLAB
p = [1 -1 -1];
roots(p)

The second example is the cubic equation x3−3x2+4x−5=0x^3 - 3x^2 + 4x - 5 = 0. Here roots returns one real root (about 2.21342.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 (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 x0x_0 where the search starts.

The example is the equation

x2−5e−x+1=0x^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.95810.9581.

Find a nonlinear root with fzeroMATLAB
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 −π/2-\pi/2 or π/2\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 (opens in a new tab)). The limit of fzero is one equation with one unknown. For a system F(x)=0F(x) = 0, where xx and FF are vectors, MATLAB offers fsolve.

The example system has two unknowns, xx and yy:

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

First the unknowns are renamed x1x_1 and x2x_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)(1, 1), fsolve reports that the equation is solved and returns x1=3x_1 = 3 and x2=5x_2 = 5. The second output gives the function value at the solution, which is almost zero.

Solve a system of nonlinear equationsMATLAB
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 Ax=bAx = b, where AA is an n×nn \times n matrix of coefficients, bb is the right-hand side and xx holds the unknowns. When AA is not singular, the solution is x=A−1bx = A^{-1}b.

The example system is:

x−2y+3z=2x+4y−5z=65x−y−z=12\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=3x = 3, y=2y = 2 and z=1z = 1. For computation, solve the system with A\b or linsolve rather than explicitly forming the inverse.

Solve a linear system with linsolveMATLAB
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, 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.

Plain text
Mostapha Kalami Heris, Root Finding in MATLAB: Video Tutorial (YPTNM190520S01), Yarpiz, 2019. https://yarpiz.com/root-finding-yptnm190520s01
BibTeX
@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}
}

Comments

Sign in or create a free account to join the discussion. No password needed.