2
$\begingroup$

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?

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

1 Answer 1

3
$\begingroup$
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.}}  *)
$\endgroup$
10
  • $\begingroup$ And also NSolve[f[x]==0, x \[Element] Interval[{0,3}]], just to give one more way :-) $\endgroup$
    – Szabolcs
    Commented Nov 7, 2016 at 14:40
  • $\begingroup$ 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 ? $\endgroup$
    – Love Eva
    Commented Nov 7, 2016 at 14:52
  • $\begingroup$ for some reson the solve and Nsolve did not work, it tells me "Solve::eqf: x<=3 is not a well-formed equation." $\endgroup$
    – Love Eva
    Commented Nov 7, 2016 at 15:18
  • $\begingroup$ @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. $\endgroup$
    – Bob Hanlon
    Commented Nov 7, 2016 at 15:45
  • 1
    $\begingroup$ @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. $\endgroup$
    – Bob Hanlon
    Commented Nov 7, 2016 at 16:27

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