3
$\begingroup$

My goal is to implement the three things below.

(i) Number of white balls(w ) and red balls(r) are constrained to be 2 ~ 29.

(ii)Total number(n) of balls is w+r and it shoud not exceed 30.

(iii)sampleSize is constrained to be at least 1 and at most n-1.

My code is as below;

Manipulate[n = w + r;  
 If[2 <= n <= 30, 
  Grid[{{"w", "r", "n=w+r", "sampleSize=n-1"}, {w, r, n, sampleSize}},
    Frame -> All], "not adjustable, w+r lager than 30"], {{w, 1}, 1, 
  29, 1}, {{r, 1}, 1, 29, 1}, {sampleSize, 1, n - 1, 1}, 
 ControlPlacement -> Left]

There are two problem to my code.How to improve?

  1. My sampleSize was exceeding n-1 as below, violating (iii).

enter image description here

  1. To implement (ii), is there a way to limit the maximum of w and r sliders so that w+r is always less than 29 whenever moving the w or r sliders? (I have used message to appear as below using If[] function to implement (ii), but I think there must be another way)

enter image description here

$\endgroup$

2 Answers 2

5
$\begingroup$
$Version

(* "13.3.1 for Mac OS X ARM (64-bit) (July 24, 2023)" *)

Clear["Global`*"]

Manipulate[
 n = w + r;
 sampleSize = Min[sampleSize, n - 1];
 If[n <= 30,
  Grid[{
    {" w ", " r ", "n\[ThinSpace]=\[ThinSpace]w+r", "sample size"},
    {w, r, n, sampleSize}},
   Frame -> All],
  "not adjustable, w+r larger than 30"],
 {{w, 10}, 1, 30, 1, Appearance -> "Labeled"},
 {{r, 10}, 1, 30 - w, 1, Appearance -> "Labeled"},
 {{sampleSize, 5, "sample size"}, 1, w + r - 1, 1,
  Appearance -> "Labeled"},
 ControlPlacement -> Left]

enter image description here

$\endgroup$
2
  • $\begingroup$ The control spec in your answer "{sampleSize, 1,w+r-1, 1}" shows expected behavior, exactly constrined to n-1 ! Could I have additional question? When I try "{sampleSize, 1, n, 1}", w slider to 18 and r slider to 6, then n slider can go only to till 19, not 23(=18+6-1) Could I know what makes the difference using "w+r-1" and "n-1" in control spec? $\endgroup$
    – Soon
    Commented Jan 4 at 2:35
  • 1
    $\begingroup$ n is defined in the body of the Manipulate, the control specs are external to the body. In control specs you should only use constants or other control variables. $\endgroup$
    – Bob Hanlon
    Commented Jan 4 at 2:44
5
$\begingroup$

You can use a Slider with a custom control function for this kind of thing:

Manipulate[
 noCells = Min[sampleSize, w];
 If[2 <= n <= 30, 
  Grid[{{"w", "r", "n=w+r", "sampleSize=n-1"}, {w, r, n, sampleSize}},
    Frame -> All], "not adjustable, w+r lager than 30"],
 Grid[{
   {Slider[
     Dynamic[w,
      Function[
       w = #;
       n = w + r; 
       If[n > 30,
        r = 30 -  w;
        n = w + r
        ]
       ]
      ],
     {1, 29, 1}
     ]
    },
   {
    Slider[
     Dynamic[r,
      Function[
       r = #;
       n = w + r; 
       If[n > 30,
        w = 30 -  r;
        n = w + r
        ]
       ]
      ],
     {1, 29, 1}
     ]
    },
   {Slider[Dynamic[sampleSize], Dynamic[{1, n - 1, 1}]]}
   }
  ],
 Initialization :> (
   w = 1;
   r = 1;
   n = w + r;
   sampleSize = 1
   )
 ]
$\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.