1
$\begingroup$

EDIT: CHanged the code. Please try again t see if it works well on our PCs.

I am creating a simple Manipulate[] notebook. However when I place Table[] in the code, like:

Manipulate[ 
 OK = True;


 ai = ConstantArray[0, MaxIter + 1]; 
 bi = ConstantArray[0, MaxIter + 1]; 
 ci = ConstantArray[0, MaxIter + 1];
 ai[[1]] = a0; bi[[1]] = b0; ci[[1]] = (ai[[1]] + bi[[1]])/2;
 Table[ (*This instruction causes problems*)
  a = ai[[j - 1]]; b = bi[[j - 1]]; c = ci[[j - 1]];
  If[(f /. x -> a) (f /. x -> c) <= 0, ai[[j]] = ai[[j - 1]]; 
   bi[[j]] = ci[[j - 1]];, 
   If[(f /. x -> b) (f /. x -> c) <= 0, ai[[j]] = ci[[j - 1]]; 
     bi[[j]] = bi[[j - 1]];, OK = False; break;];];
  ci[[j]] = (ai[[j]] + bi[[j]])/2;
  , {j, 2, i}];

 If[OK, Show[Plot[f, {x, a0, b0}, PlotStyle -> Thick], 
   PlotLabel -> "Metoda bisekcji", ImageSize -> Large]]
 , 
 (*controls*)

 Dynamic@Column@{
    Control[{{f, x^2 - 1, "f(x)="}, {x^2 - 1, Cos[x] - x}}],
    Control[{{method, "Bisekcji", "Metoda:"}, {"Bisekcji", "Newtona", 
       "Iteracji prostej", "Siecznych"}}],
    Sequence @@ 
     If[method == "Bisekcji" || 
       method == 
        "Siecznych" , {Control[{{a0, 1/2, 
          "\!\(\*SubscriptBox[\(a\), \(0\)]\)="}}], 
       Control[{{b0, 2, 
          "\!\(\*SubscriptBox[\(b\), \(0\)]\)="}}]}, {Control[{{x0, 2,
           "\!\(\*SubscriptBox[\(x\), \(0\)]\)="}}]}],
    Control[{{i, 1, "Iteracja"}, 1, 5, 1}]
    },
 (*Initialization*)
 Initialization :> (
   MaxIter = 5;
   ) 
 ]

The cell containing the GUI keeps evaluating all the time and the displayed content keeps being refreshed and looks like shaking. Is it normal?

$\endgroup$
11
  • 1
    $\begingroup$ The behavior you describe is not replicated on my Mac with Mathematica 9.0.1 Why not try ClearAll[x,j,i] before running the code? $\endgroup$
    – DavidC
    Commented Oct 14, 2013 at 12:45
  • 1
    $\begingroup$ Working as expected on Mathematica 9.0.1 on Win7. $\endgroup$
    – Ymareth
    Commented Oct 14, 2013 at 13:01
  • 1
    $\begingroup$ This question appears to be off-topic because the described problem cannot be reproduced. $\endgroup$
    – Yves Klett
    Commented Oct 14, 2013 at 13:02
  • 2
    $\begingroup$ What did you post your first example for? Now you have completely different and horribly formatted code which does not at all look inviting. $\endgroup$
    – Yves Klett
    Commented Oct 14, 2013 at 13:34
  • 3
    $\begingroup$ what's break? $\endgroup$
    – cormullion
    Commented Oct 14, 2013 at 13:49

1 Answer 1

4
$\begingroup$

The problem is that some variables are set in the main body of the Manipulate every time the Manipulate is updated. This triggers another update...ad infinitum.

Use TrackedSymbols to control when an update is triggered.

Manipulate[OK = True;
 ai = ConstantArray[0, MaxIter + 1];
 bi = ConstantArray[0, MaxIter + 1];
 ci = ConstantArray[0, MaxIter + 1];
 ai[[1]] = a0; bi[[1]] = b0; ci[[1]] = (ai[[1]] + bi[[1]])/2;
 Table[(*This instruction causes problems*)a = ai[[j - 1]]; 
  b = bi[[j - 1]]; c = ci[[j - 1]];
  If[(f /. x -> a) (f /. x -> c) <= 0, ai[[j]] = ai[[j - 1]];
   bi[[j]] = ci[[j - 1]];, 
   If[(f /. x -> b) (f /. x -> c) <= 0, ai[[j]] = ci[[j - 1]];
     bi[[j]] = bi[[j - 1]];, OK = False; break;];];
  ci[[j]] = (ai[[j]] + bi[[j]])/2;, {j, 2, i}];
 If[OK, Show[Plot[f, {x, a0, b0}, PlotStyle -> Thick], 
   PlotLabel -> "Metoda bisekcji", ImageSize -> Large]],

 (*controls*)
 Dynamic@Column@{Control[{{f, x^2 - 1, "f(x)="}, {x^2 - 1, 
       Cos[x] - x}}], 
    Control[{{method, "Bisekcji", "Metoda:"}, {"Bisekcji", "Newtona", 
       "Iteracji prostej", "Siecznych"}}], 
    Sequence @@ 
     If[method == "Bisekcji" || 
       method == 
        "Siecznych", {Control[{{a0, 1/2, 
          "\!\(\*SubscriptBox[\(a\), \(0\)]\)="}}], 
       Control[{{b0, 2, 
          "\!\(\*SubscriptBox[\(b\), \(0\)]\)="}}]}, {Control[{{x0, 2,
           "\!\(\*SubscriptBox[\(x\), \(0\)]\)="}}]}], 
    Control[{{i, 1, "Iteracja"}, 1, 5, 1}]},

 TrackedSymbols :> {i, a0, b0, method, f},

 (*Initialization*)Initialization :> (MaxIter = 5;)]

The rest of it needs work, apparently, but TrackedSymbols solves the continual-updating problem. One can also simply set TrackedSymbols -> True to track only the control variables.

$\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.