2
$\begingroup$

I am trying to use Manipulate to create a function which allows a user to play with a probability density. The following generates a static density over the range 2000 - 4000, then allows the user to calculate the probability that the variable lies between two bounds: $aLower$ and $aUpper$:

fShowInterval[aLower_, aUpper_] := 
 Module[{dist, p1, p2}, 
  dist = PDF[UniformDistribution[{2000, 4000}], #] &;
  p1 = Plot[dist@x, {x, aLower, aUpper}, Filling -> Axis, 
FillingStyle -> Blue, BaseStyle -> {FontSize -> 16}, 
PlotStyle -> {Gray, Thickness[0.005]}, 
AxesLabel -> {"value $ ", "probability density "}, 
PlotRange -> {{1500, 4500}, Full}, Ticks -> {True, False}, 
PlotLabel -> N[(1/2000)*(aUpper - aLower)]];
  p2 = Plot[dist@x, {x, 1500, 4500}, Filling -> None, 
PlotRange -> {{1500, 4500}, Full}, BaseStyle -> {FontSize -> 16}, 
PlotStyle -> {Gray, Thickness[0.005]}, 
AxesLabel -> {"value $ ", "p(v)"}];
  Show[p1, p2, 
   Epilog -> {{Dashed, Line[{{2000, 0}, {2000, 1/2000}}]}, {Dashed, 
  Line[{{4000, 0}, {4000, 1/2000}}]}}, ImageSize -> 600]]

Manipulate[
 fShowInterval[aLower, aUpper], {aLower, 2000, aUpper - 1, 
  100}, {aUpper, 2001, 4000, 100}]

With a result like:

enter image description here

The first slider stops the lower bound being higher than the upper. However, there are no corresponding constraints on the upper slider; meaning that it is possible to move this slider so that the lower bound is above the upper. I have tried modifying the Manipulate statement to:

Manipulate[
 fShowInterval[aLower, aUpper], {aLower, 2000, aUpper - 1, 
  100}, {aUpper, aLower+1, 4000, 100}]

However, I get a slew of errors, and the graph does not display.

$\endgroup$
4
$\begingroup$

You can use IntervalSlider in version 10.0 and above. However, you need to explicitly tell Manipulate to use it.

Manipulate[
 fShowInterval[Sequence @@ probRange], {probRange, 2000, 4000, 
  IntervalSlider, Method -> "Push", MinIntervalSize -> 1},
 Initialization :> (probRange = {2500, 3500};)]

enter image description here

Method -> "Push"will keep the interval endpoints from crossing one another. MinIntervalSize -> 1 will keep the endpoints from touching each other.

With IntervalSlider you can also click drag the interval by selecting the region between the two endpoints. Not very interesting for this distribution but it will be for many others.

$\endgroup$
  • $\begingroup$ That's really great - thanks! Best, Ben $\endgroup$ – ben18785 Jun 18 '15 at 21:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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