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}
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}
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.
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}]
Solve[]andNSolve[]can solve equations with constraints likef[x] == 0 && -2.5 Pi <= x <= 2.5 Pi. In such an approachLength[]will be easier to use thanCount[]. – Michael E2 Apr 21 '16 at 16:16CountRoots[f[x], {x, -2.5*Pi, 2.5*Pi}]? – user688486 Dec 25 '23 at 09:59