# Find roots for a function on a range [closed]

Assuming I have function F[x_]:= (x-1)(x-2)(x-5) and I want to find all roots on a range from $x=0$ up to $x=$3, is there a way to do it?

• Reduce[(-5 + x) (-2 + x) (-1 + x) == 0 && 0 <= x <= 3, x]
– Moo
Commented Nov 7, 2016 at 14:26

f[x_] := (x - 1) (x - 2) (x - 5)


Add the range as a constraint in Solve or NSolve

soln1 = Solve[{f[x] == 0, 0 <= x <= 3}, x]

(*  {{x -> 1}, {x -> 2}}  *)


Use ReplaceAll (/.) to get a list of the values

xVal = x /. soln1

(*  {1, 2}  *)

NSolve[{f[x] == 0, 0 <= x <= 3}, x]

(*  {{x -> 1.}, {x -> 2.}}  *)

• And also NSolve[f[x]==0, x \[Element] Interval[{0,3}]], just to give one more way :-) Commented Nov 7, 2016 at 14:40
• what about the brackets {} can i remove them ? also, can i stor the solved values in a list ?? Finally can i foe example tell mathematica that i need the first two roots with out knowing the range ? Commented Nov 7, 2016 at 14:52
• for some reson the solve and Nsolve did not work, it tells me "Solve::eqf: x<=3 is not a well-formed equation." Commented Nov 7, 2016 at 15:18
• @LoveEva - presumably you did not copy and paste the code above correctly; however, without showing your code I cannot guess. Whether you can get the first two roots without specifying a range (or even if Solve or NSolve can solve the equation) depends on the specific equation. Commented Nov 7, 2016 at 15:45
• @anderstood - the documentation for NSolve states "NSolve[{Subscript[expr, 1], Subscript[expr, 2], ...}, vars] is equivalent to NSolve[Subscript[expr, 1]&&Subscript[expr, 2]&& ..., vars]. " Documentation for Solve has a similar statement. Commented Nov 7, 2016 at 16:27