2
$\begingroup$

I've been struggling to find a way to find an interval solution to an equation/inequality. I know mathematica can do this since for example,

NumberLinePlot[Sin[x] < 0.5, {x, 0, 6.28}]

gives a plot of the correct interval. How can I have mathematica just give me the Interval instead of the plot?

$\endgroup$
3
  • $\begingroup$ Like this? Reduce[0 <= x <= 2 \[Pi] && Sin[x] < 1/2, x]. The || symbol means Or. $\endgroup$
    – march
    Commented Nov 1, 2015 at 5:41
  • $\begingroup$ Reduce[Sin[x] < 1/2 && 0 < x < 2 π, x] gives 0 < x < π/6 || (5*π)/6 < x < 2*π. The || march refers is in the result. $\endgroup$
    – m_goldberg
    Commented Nov 1, 2015 at 5:47
  • $\begingroup$ Ah ok, thanks march and goldberg! Does mathematica have a built in function to convert the result into intervals? I can write a (sloppy) function for it myself but I'd rather use built-in functions. $\endgroup$
    – ra91
    Commented Nov 1, 2015 at 5:58

2 Answers 2

3
$\begingroup$

Note that

foo = Reduce[{
    Sin[x] < 1/2,
    0 <= x <= 2 π
  }, x, Reals]
(* 0 <= x < π/6 || (5 π)/6 < x <= 2 π *)

almost gives you what you want. We can rewrite this to be in interval form,

ineqsToIntervals[x_Or] := List @@ (
    x /. {
      Inequality[a_, Less, _, Less, b_] :> 
        Row[{"(", a, ",", b, ")"}],
      Inequality[a_, LessEqual, _, Less, b_] :> 
        Row[{"[", a, ",", b, ")"}],
      Inequality[a_, Less, _, LessEqual, b_] :> 
        Row[{"(", a, ",", b, "]"}],
      Inequality[a_, LessEqual, _, LessEqual, b_] :> 
        Row[{"[", a, ",", b, "]"}]
    }
)

so that ineqsToIntervals[foo] gives $\{ [0,\frac{\pi}{6}), (\frac{5\pi}{6},2\pi] \}$.

$\endgroup$
2
  • 1
    $\begingroup$ Note: you do not need to specify the domain to Reduce. The inequality 0 <= x <= 2 π is sufficient to tell Reduce it is working over the reals. $\endgroup$
    – m_goldberg
    Commented Nov 1, 2015 at 6:20
  • $\begingroup$ Awesome, thanks! $\endgroup$
    – ra91
    Commented Nov 1, 2015 at 6:31
3
$\begingroup$

You might try

List @@ (Drop[#, {2, -2}]& /@ List @@@ Reduce[Sin[x] < 1/2 && 0 < x < 2 π, x])

{{0, π/6}, {(5 π)/6, 2 π}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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