How do i Count[] the number of roots in the Domain and printout the intervals in x containing these roots.

Given the function

f[x_] := (1/x) Sin[ x]

and the Domain

{x, -2.5 Pi, 2.5 Pi}
  • 1
    $\begingroup$ Solve[] and NSolve[] can solve equations with constraints like f[x] == 0 && -2.5 Pi <= x <= 2.5 Pi. In such an approach Length[] will be easier to use than Count[]. $\endgroup$ – Michael E2 Apr 21 '16 at 16:16
  • $\begingroup$ Have you seen this? $\endgroup$ – J. M. will be back soon Apr 21 '16 at 16:20
  • $\begingroup$ I'm afraid not yet. will check it out. thank you @J.M. $\endgroup$ – Nabil Apr 21 '16 at 16:23

You can use FindInstance to Find several instances:

FindInstance[f[x] == 0 && -2.5 Pi < x < 2.5 Pi, x, 4]


or some others:

Reduce[f[x] == 0 && -2.5 Pi < x < 2.5 Pi, x]


using Length, as suggested by @Michael E2



sol = NSolve[{f[x] == 0, -2.5 Pi < x < 2.5 Pi}, x]




points = {x, f[x]} /. sol // Chop

{{-6.28319, 0}, {-3.14159, 0}, {3.14159, 0}, {6.28319, 0}}

Plot[f[x], {x, -2.5 Pi, 2.5 Pi}, Epilog -> {Red, PointSize[Large], Point@points}]

enter image description here

You can show the Intervals like so:

NumberLinePlot[f[x] == 0, {x, -2.5 Pi, 2.5 Pi}]

enter image description here


I'll address only the root counting portion. In this paper, Delves and Lyness present a method based on contour integration for counting all the complex roots of a function within a given contour. In your specific case, it goes like this:

Round[Chop[NIntegrate[f'[z]/f[z], {z, -5 π/2, -5 π I/2, 5 π/2, 5 π I/2, -5 π/2}]/(2 π I)]]

where I used an anticlockwise square contour with two corners on the endpoints of interest. Luckily, the function is known to have only real roots, so the count obtained is accurate. For more complicated functions, a different contour might be needed.

  • $\begingroup$ So glad you posted this. Now I can just upvote and don't have to think about coding it. $\endgroup$ – Daniel Lichtblau Apr 21 '16 at 20:52
  • $\begingroup$ @Daniel, I'll admit I haven't yet gotten to actually implementing the full Delves-Lyness; it's surprisingly tricky to make something reliable out of it. Thanks. $\endgroup$ – J. M. will be back soon Apr 22 '16 at 0:49
  • $\begingroup$ Here is some related code I was able to dredge up, also far from complete. My general feeling is it is better to subdivide than to try to go the "find polynomial with same roots" route. $\endgroup$ – Daniel Lichtblau Apr 22 '16 at 2:45
  • $\begingroup$ Okay, I finally found the related MSE post I was looking for. Not perfect but the approach can be effective. $\endgroup$ – Daniel Lichtblau Apr 22 '16 at 2:59
  • $\begingroup$ Heh, interesting. I sent you an e-mail in the meantime. $\endgroup$ – J. M. will be back soon Apr 22 '16 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.