12.9: Finding Roots

Fundamentals of MATLAB Programming · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under CC BY-NC-SA, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    12.9: Finding Roots

    A root of a function is an x-value where the function equals zero. Roots are also called zeros or x-intercepts. Root-finding is common in engineering because many problems can be written as equations of the form f(x) = 0.

    Roots of Polynomials with roots

    If the function is a polynomial represented by a coefficient vector, MATLAB can find its roots using the roots function. The basic format is:

    r = roots(polynomialCoefficients);

    Example \(\PageIndex{1}\)

    finding the roots of \(f(x) = 4*x^3 - 2*x^2 - 8*x + 3\).

    r = roots([4 -2 -8 3])
    Solution

    r =

    -1.3660
    1.5000
    0.3660

    Roots of Nonlinear Functions with fzero

    Not every function is a polynomial. For nonlinear functions such as sine, cosine, exponential, or Bessel functions, MATLAB provides fzero.

    The basic format is:

    xRoot = fzero(functionHandle, startingGuess);

    The second input can be a single starting guess or a two-value interval. If it is an interval, the function should change sign over that interval.

    Example \(\PageIndex{2}\)

    Finding the root of cos(2x) near x = 0.1.

    myFun = @(x, c) cos(c*x);
    
    c = 2;
    
    xRoot = fzero(@(x) myFun(x, c), 0.1)
    Solution

    xRoot = 0.7854

    Example \(\PageIndex{3}\)

    Finding the root of the Bessel function of the first kind between the [2 3] interval.

    anonFct = @(x) besselj(0, x);
    
    xBracket = [2 3];
    
    xRoot = fzero(anonFct, xBracket)
    Solution

    xRoot = 2.4048

    Note

    Common Mistake

    fzero finds one root near the starting guess or inside the interval. If a function has multiple roots, you may need to try different starting guesses or intervals.


    This page titled 12.9: Finding Roots was last modified on Tue, 30 Jun 2026 02:11:52 GMT and is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Zara Noroozi.