CountRoots[f,x]
gives the number of real roots of the univariate function f in x.
CountRoots[f,{x,a,b}]
gives the number of roots between a and b.
CountRoots
CountRoots[f,x]
gives the number of real roots of the univariate function f in x.
CountRoots[f,{x,a,b}]
gives the number of roots between a and b.
Details
Examples
open all close allBasic Examples (4)
Count the number of polynomial roots between 0 and 5:
CountRoots[(x - 1)(x - 2)(x - 3)(x - 4), {x, 0, 5}]Count roots of a polynomial in a closed rectangle:
CountRoots[x ^ 17 - 1, {x, 0, 1 + I}]Count roots of a real elementary function in a real interval:
CountRoots[Sin[z / 2]^5 + Tan[4z]^3, {z, 0, 10 ^ 10}]Count roots of a holomorphic function in a closed rectangle:
CountRoots[Cos[x ^ 3], {x, -2 - 2I, 2 + 2I}]Plot3D[With[{x = u + I v}, Log@Abs[Cos[x ^ 3]]], {u, -2, 2}, {v, -2, 2}, Mesh -> None]Scope (20)
Basic Uses (8)
Find the number of the real roots:
CountRoots[(x ^ 2 - 2)(x ^ 2 - 5), x]Count roots in a real interval:
CountRoots[(x ^ 2 - 2)(x ^ 2 - 5), {x, -2, 2}]Plot[(x ^ 2 - 2)(x ^ 2 - 5), {x, -2, 2}]Count roots in a closed rectangle:
CountRoots[(x ^ 4 - 2)(x ^ 4 - 20), {x, -2 - 2I, 2 + 2I}]Plot3D[With[{x = u + I v}, Log@Abs[(x ^ 4 - 2)(x ^ 4 - 20)]], {u, -2, 2}, {v, -2, 2}, Mesh -> None]Count roots in a vertical line segment:
CountRoots[1 + x ^ 2, {x, -2I, 2I}]Count roots in a horizontal line segment:
CountRoots[1 + x ^ 2, {x, -1 + I, 1 + I}]Multiple roots are counted with their multiplicities:
CountRoots[x ^ 3, {x, -1, 1}]For a root of multiplicity
, all the derivatives
for
also vanish:
Plot[Evaluate@Table[D[x ^ 3, {x, n}], {n, 0, 2}], {x, -1, 1}, PlotLabels -> {"f", "f'", "f''"}]Roots at the endpoints of the interval are included:
CountRoots[(x - 1)(x - 2), {x, 0, 2}]Roots on the boundary of the rectangle are included:
CountRoots[x ^ 4 - 1, {x, 0, 1 + I}]Real Elementary Functions (6)
Count the real roots of a high-degree polynomial:
CountRoots[x ^ 1000000 - 2x ^ 777777 + 3x ^ 12345 + 9x ^ 67 - 10, x]Find the number of non-negative roots of an algebraic function involving high-degree radicals:
CountRoots[2x ^ (123451 / 67890) - x ^ 2 + 4Sqrt[x] - 4x - 9 / 8, {x, 0, ∞}]Count the non-negative roots of a function involving irrational real powers:
CountRoots[x ^ Pi - x ^ Sqrt[2] - Sqrt[3]x + 2 ^ (1 / 3), {x, 0, ∞}]Count the real roots of a real exp-log function:
CountRoots[E ^ (2E ^ x) - Log[x ^ 2 + 1] - 20x - 11, x]Count the real roots of a tame real elementary function:
CountRoots[Cos[1 / (x ^ 2 + 1)] - Sin[E ^ -x ^ 2], x]This shows the plot of the function:
Plot[Cos[1 / (x ^ 2 + 1)] - Sin[E ^ -x ^ 2], {x, -3, 3}]Count roots of a real elementary function in a bounded interval:
CountRoots[2Sin[Exp[x]] - Cos[Pi x] - 1 / 2, {x, 0, 7}]This shows the plot of the function:
Plot[2Sin[Exp[x]] - Cos[Pi x] - 1 / 2, {x, 0, 7}]Holomorphic Functions (3)
Count roots of a holomorphic elementary function in a closed rectangle:
CountRoots[Cos[Sin[x] + 2] - Cos[2 x] - 1, {x, -3 - 3I, 3 + 3I}]Count roots of a holomorphic special function in a closed rectangle:
CountRoots[FresnelC[x] - x - 1, {x, -2 - 2I, 2 + 2I}]Find the number of roots of an analytic function in a bounded real interval:
CountRoots[BesselJ[2, x] - Sin[x ^ 2], {x, -3, 3}]The double root at zero is counted with multiplicity:
Plot[BesselJ[2, x] - Sin[x ^ 2], {x, -3, 3}]Meromorphic Functions (3)
Count roots of a meromorphic elementary function in a closed rectangle:
CountRoots[Tan[z] - Log[z + 3] - z ^ 2, {z, -2 - 2I, 2 + 2I}]Visualize roots and poles of the function:
f = Tan[z] - Log[z + 3] - z ^ 2;
g = f /. z -> x + I y;
rts = {Re[z], Im[z]} /. Solve[f == 0 && -2 ≤ Re[z] ≤ 2 && -2 ≤ Im[z] ≤ 2, z];
Show[{ContourPlot[{Re[g] == 0, Im[g] == 0}, {x, -2, 2}, {y, -2, 2}, Contours -> 0], Graphics[{PointSize[Large], Green, Tooltip[Point[rts], "Root"], Red, Tooltip[Point[{{-Pi / 2, 0}, {Pi / 2, 0}}], "Pole"]}]}]Count roots of a meromorphic special function in a closed rectangle:
CountRoots[Gamma[z] - z - 1, {z, -2 - 2I, 2 + 2I}]Visualize roots and poles of the function:
f = Gamma[z] - z - 1;
g = f /. z -> x + I y;
rts = Quiet[{Re[z], Im[z]} /. Solve[f == 0 && -2 ≤ Re[z] ≤ 2 && -2 ≤ Im[z] ≤ 2, z]];
Show[{ContourPlot[{Re[g] == 0, Im[g] == 0}, {x, -2, 2}, {y, -2, 2}, Contours -> 0], Graphics[{PointSize[Large], Green, Tooltip[Point[rts], "Root"], Red, Tooltip[Point[{{-2, 0}, {-1, 0}, {0, 0}}], "Pole"]}]}]Find the number of roots of a meromorphic function in a bounded real interval:
CountRoots[Gamma[x] + E ^ x - 7x, {x, -3, 3}]Plot[Gamma[x] + E ^ x - 7x, {x, -3, 3}]Applications (4)
The number of 17
roots of unity in the closed unit square in the first quadrant:
poly = x ^ 17 - 1;
CountRoots[poly, {x, 0, 1 + I}]Roots on the boundary are counted:
Graphics[{{GrayLevel[0.7], Rectangle[{0, 0}, {1, 1}]}, {Red, PointSize[Medium], Point[{Re[x], Im[x]} /. NSolve[poly == 0, x]]}}]Check that a function has exactly one root in an interval:
f = Tan[x] - E ^ x + 7x ^ 2;
CountRoots[f, {x, 8 / 5, 9 / 5}]Use FindRoot to approximate the root:
FindRoot[f, {x, 8 / 5, 9 / 5}, Method -> "Brent", WorkingPrecision -> 30]Compute a contour integral of logarithmic derivative of a function using the formula
, where
is the number of roots for a holomorphic function
:
f = Sinh[5z] - z;
ldf = D[f, z] / f;
2 Pi I CountRoots[f, {z, -1 - I, 1 + I}]Compare with the result of numeric integration:
NIntegrate[ldf, {z, -1 - I, 1 - I, 1 + I, -1 + I, -1 - I}] - %//ChopTest stability of equilibria at 0 of linear dynamical systems
by counting the roots of the CharacteristicPolynomial[m,x] in the right half-plane:
m = {{0, 0, 0, 0, 0, 0, -78375471}, {1, 0, 0, 0, 0, 0, -34057401}, {0, 1, 0, 0, 0, 0, -5727428}, {0, 0, 1, 0, 0, 0, -639271}, {0, 0, 0, 1, 0, 0, -53497}, {0, 0, 0, 0, 1, 0, -2905}, {0, 0, 0, 0, 0, 1, -85}};f = CharacteristicPolynomial[m, x];Use a bound for max absolute value of the roots:
rb = 1 + Max[Abs[Drop[#, -1] / Last[#]]]&[CoefficientList[f, x]]Count the roots in the right half-plane:
CountRoots[f, {x, -rb I, rb + rb I}]Since all eigenvalues of m have negative real parts, the equilibrium is asymptotically stable:
y = Table[x[i][t], {i, 7}];
sol = DSolveValue[{D[y, t] == m.y, (y /. t -> 0) == 1}, y, t]//N;
Plot[sol, {t, 0, 30}]m = {{0, 0, 0, 0, 0, 0, 78375471}, {1, 0, 0, 0, 0, 0, -34057401}, {0, 1, 0, 0, 0, 0, -5727428}, {0, 0, 1, 0, 0, 0, -639271}, {0, 0, 0, 1, 0, 0, -53497}, {0, 0, 0, 0, 1, 0, -2905}, {0, 0, 0, 0, 0, 1, -85}};This time there are roots with non-negative real parts:
f = CharacteristicPolynomial[m, x];
rb = 1 + Max[Abs[Drop[#, -1] / Last[#]]]&[CoefficientList[f, x]];
CountRoots[f, {x, -rb I, rb + rb I}]The equilibrium is not asymptotically stable:
y = Table[x[i][t], {i, 7}];
sol = DSolveValue[{D[y, t] == m.y, (y /. t -> 0) == 1}, y, t]//N;
Plot[sol, {t, 0, 30}]Properties & Relations (5)
The number of complex roots of a polynomial is equal to its degree:
poly = x ^ 10 - 17x ^ 7 + 21x ^ 3 - 55x ^ 2 + 22x + 2;This gives a bound on absolute values of roots of a polynomial:
CauchyBound[f_, x_] := Module[{cfl, n},
cfl = CoefficientList[f, x];
n = Length[cfl] - 1;
Max[Table[(n Abs[cfl[[n + 1 - k]] / cfl[[n + 1]]]) ^ (1 / k), {k, n}]]]b = Ceiling[CauchyBound[poly, x]]The polynomial indeed has 10 roots within the Cauchy bounded region:
CountRoots[poly, {x, -b - b I, b + b I}]The number of real roots of a polynomial with
nonzero terms is at most
:
poly = Expand[x(x ^ 4 - 1)(x ^ 4 - 2)(x ^ 4 - 3)]This polynomial has the maximal possible number of
real roots:
CountRoots[poly, {x, -Infinity, Infinity}]Use Reduce to find polynomial roots:
poly = (x ^ 2 - 2)(x ^ 3 - 5x + 1);CountRoots[poly, {x, 0, 3}]Reduce[poly == 0 && 0 ≤ x ≤ 3, x]Use RootIntervals to find isolating intervals for roots:
poly = (x ^ 4 - 2)(x ^ 8 - 3);CountRoots[poly, {x, -Infinity, Infinity}]RootIntervals[poly]Use NumberFieldSignature to count the real roots and the pairs of complex roots of a polynomial:
poly = x ^ 8 - 2;NumberFieldSignature[poly, x]CountRoots[poly, {x, -Infinity, Infinity}]Possible Issues (1)
Neat Examples (1)
Related Guides
Text
Wolfram Research (2007), CountRoots, Wolfram Language function, https://reference.wolfram.com/language/ref/CountRoots.html (updated 2017).
CMS
Wolfram Language. 2007. "CountRoots." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2017. https://reference.wolfram.com/language/ref/CountRoots.html.
APA
Wolfram Language. (2007). CountRoots. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/CountRoots.html
BibTeX
@misc{reference.wolfram_2026_countroots, author="Wolfram Research", title="{CountRoots}", year="2017", howpublished="\url{https://reference.wolfram.com/language/ref/CountRoots.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_countroots, organization={Wolfram Research}, title={CountRoots}, year={2017}, url={https://reference.wolfram.com/language/ref/CountRoots.html}, note=[Accessed: 12-June-2026]}