Message Boards Message Boards

1
|
5787 Views
|
3 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Clamping values in Manipulate[]

Posted 11 years ago
Manipulate[{x, y}, {x, 1, 10, 1}, {y, 1, x, 1}]
If you move the slider for x to a value of 5 and then do the same for y. Now if you decrease the value of x, y becomes invalid as it should never be greater than x. Does anyone know how I can get manipulate to clamp the y value if it becomes invalid.
The Manipulate controller realises that the y value is incorrect as the slider becomes red.

Thanks in advance!
POSTED BY: Lenny Johnson
3 Replies
Posted 11 years ago
http://reference.wolfram.com/mathematica/tutorial/AdvancedManipulateFunctionality.html

a
bout 1/5 of the way down that long web page under the title "Interdependent Controls"
looks like it might apply to what you are trying to do.
POSTED BY: Bill Simpson
One problem with the way indicated in Advanced Manipulate Functionality (linked by Bill Simpson) is that changing one of the tracked symbols in the body of the Manipulate triggers another update.  For instance, if the x slider is moved in
Manipulate[If[y > x, y = x]; {x, y}, {x, 1, 10, 1}, {y, 1, x, 1}]
it triggers an update that cause the body of the Manipulate to be evaluated; if y > x, then setting y = x will trigger yet another update that basically has no effect.  It just repeats the evaluation of the body without changing y since the condition y > x would now be false.  If the rest of the body is more complicated than {x, y} and takes an appreciable amount of time to compute, then the performance might be sluggish.

Another way to go is to customize the control slightly:
Manipulate[
{x, y},
{{x, 1}, Manipulator[Dynamic[x, (x = #; y = Clip[y, {1, x}]) &], {1, 10, 1}] &},
{y, 1, x, 1}]
Here x and y are updated in the same command when the x slider is moved.  This triggers an update of the Manipulate, but the evaluation of the body does not trigger a second update.
POSTED BY: Michael Rogers
Posted 11 years ago
Thanks for the replies all, much appreciated.

Option 1. Is the easiest to read and understand (For a nooby mathematica programmer).

Option 2. As a programmer I appreciate that this approach is the better practice.
POSTED BY: Lenny Johnson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract