Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Sorry for my wording, I'm not sure what the proper term for this is. I want to find the sections of x for which the graph of some function, f(x), is in a given area. For an example of what I'm describing, I want to know how to find the thick black bars in the picture below.

enter image description here

share|improve this question
    
Is this question about the software Mathematica? – Yves Klett Jan 31 at 20:58
    
It is. Again sorry for my wording. I wanted to know if there was a function or some other way to do this in Mathematica. – harpreet bains Jan 31 at 21:14
1  
...is that mac os 7? – Sascha Jan 31 at 22:45
up vote 9 down vote accepted

Use Reduce to get the region as inequalities:

f[x_] := Sin[4 x] + x;
Reduce[-1 <= f[x] <= 1, x, Reals]

(* Root[{1 + Sin[4 #1] + #1 &, -1.45320652256538767403}] <= x <= 
  Root[{1 + Sin[4 #1] + #1 &, -0.71249223361378546370}] || 
 Root[{1 + Sin[4 #1] + #1 &, -0.222621770186095002502}] <= x <= 
  Root[{-1 + Sin[4 #1] + #1 &, 0.222621770186095002502}] || 
 Root[{-1 + Sin[4 #1] + #1 &, 0.71249223361378546370}] <= x <= 
  Root[{-1 + Sin[4 #1] + #1 &, 1.45320652256538767403}] *)

With recently introduced NumberLinePlot it can also be visualized easily. Most of the code below is really styling for aesthetics...

Module[{f},
 f[x_] := Sin[4 x] + x;
 Show[{Plot[{f[x], -1, 1}, {x, -2, 2}, 
    PlotStyle -> {Automatic, Gray, Gray}, Filling -> {2 -> {3}}],
   NumberLinePlot[Reduce[-1 <= f[x] <= 1, x, Reals], x, Spacings -> 0,
     PlotStyle -> Thickness[0.008]]}]]

enter image description here

EDIT: If you're looking only for the visualization, it's actually possible to skip Reduce entirely inside NumberLinePlot and pass the specification (-1 <= f[x] <= 1) directly to it. Extra Reduce doesn't hurt, though...

share|improve this answer
    
Thank you so much, this is exactly what I was looking for. – harpreet bains Jan 31 at 21:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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