3
$\begingroup$

I've reduced some of my code to the code here

Manipulate[
(*output here here*)
{j, 1, 40, 1},
{k, 1, Dynamic[j], 1},
]

The problem I'd like to fix is that if I set j to 40, then k to 35, and then set j down to 20, a bunch of errors get thrown. Is there a way to add something to the code here that when j gets updated k gets reset to 1?

$\endgroup$
2
  • $\begingroup$ try replacing {j, 1, 40, 1} with {j, 1, 40, 1, TrackingFunction -> (j = #; k = 1; &)}? $\endgroup$
    – kglr
    Commented Jan 30 at 7:08
  • 1
    $\begingroup$ .. or with {j, 1, 40, 1, TrackingFunction -> (j = #; k = Min[#, k]; &)} to reset k to j when j moves below current value of k (as in Nasser's answer). $\endgroup$
    – kglr
    Commented Jan 30 at 7:42

2 Answers 2

3
$\begingroup$

I assume by error, you mean you get this red marking.

enter image description here

This just says upper limit of slider is reached. You can throttle this like this

Manipulate[
 If[k > j, k = j];
 Row[{"j=", j, ", k=", k}],
 {{j, 1, "j"}, 1, 40, 1, Appearance -> "Labeled"},
 {{k, 1, "k"}, 1, Dynamic[j], 1, Appearance -> "Labeled"},
 TrackedSymbols :> {j, k}
 ]

And now if upper limit for k is more than j, it will go to match j.

You should also always add TrackedSymbols

enter image description here

$\endgroup$
2
$\begingroup$

Using an explicit Dynamic is not necessary.

$Version

(* "14.0.0 for Mac OS X ARM (64-bit) (December 13, 2023)" *)

Clear["Global`*"]

Manipulate[
 k = Min[k, j];
 StringForm["j = ``; k = ``", j, k],
 {{j, 20}, 1, 40, 1, Appearance -> "Labeled"},
 {{k, 10}, 1, j, 1, Appearance -> "Labeled"},
 SynchronousUpdating -> False,
 TrackedSymbols :> {j, k}]
$\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.