# How to Count the number of roots in the Domain from an equation

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}

• 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[]. – Michael E2 Apr 21 '16 at 16:16
• Have you seen this? – J. M. will be back soon Apr 21 '16 at 16:20
• I'm afraid not yet. will check it out. thank you @J.M. – Nabil Apr 21 '16 at 16:23

## 2 Answers

You can use FindInstance to Find several instances:

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


{{x->-6.28319},{x->-3.14159},{x->-6.28319},{x->3.14159}}

or some others:

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


x==-6.28319||x==-3.14159||x==3.14159||x==6.28319

using Length, as suggested by @Michael E2

Length[%]


4

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


{{x->-6.28319},{x->-3.14159},{x->3.14159},{x->6.28319}}

Length[%]


4

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}]


You can show the Intervals like so:

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


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)]]
4


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.

• So glad you posted this. Now I can just upvote and don't have to think about coding it. – Daniel Lichtblau Apr 21 '16 at 20:52
• @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. – J. M. will be back soon Apr 22 '16 at 0:49
• 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. – Daniel Lichtblau Apr 22 '16 at 2:45
• Okay, I finally found the related MSE post I was looking for. Not perfect but the approach can be effective. – Daniel Lichtblau Apr 22 '16 at 2:59
• Heh, interesting. I sent you an e-mail in the meantime. – J. M. will be back soon Apr 22 '16 at 14:00