3
$\begingroup$

I would like to make a Table whose iterator has a dynamically updated endpoint, i.e.

Table[i,{i,1,Dynamic[n]}]

Is this possible? Here is the context:

n=1
SetterBar[Dynamic@n, Range[1, 10, 1]]
f[x_]:=Module[
         {y},
         y=Table[0,{i,1,Dynamic[n]+4}]
         For[ i=3,i<=Dynamic[n]+4,i++,
                y[[i]]=y[[i-1]]+2y[[i-2]]
         ]
       ]
y=f[Dynamic[x]]

I get the error

Iterator {i,1,4+1} does not have appropriate bounds.

As another example of how I'd like to use this, here is fully functional code:

n=1;
Manipulate[Table[ToExpression["x" <> ToString@i], {i, 1,Dynamic@n + 1}],
           {x1, 1, 10}, {x2, 1, 10}]
$\endgroup$
  • 3
    $\begingroup$ You need to think about where you place Dynamic. In this simple example you should place it outside of Table. If you wrap Table in something else, Dynamic might have to be moved even further away from Table. The idea is to make the expression dynamic, not just one variable. Without knowing what you are planning on using this for any answers that we can provide might not help you that much. $\endgroup$ – C. E. Aug 24 '14 at 18:34
3
$\begingroup$

As mentioned in the comment by Pickett, you have to wrap Dynamic around the expression that is to be displayed. You also forgot to return a value from your function f. Here is a modification of your code that works:

Clear[f]

f[x_] := Module[{y},
  y = Table[0, {i, 1, n + 4}]; 
  For[i = 3, i <= n + 4, i++, y[[i]] = y[[i - 1]] + 2 y[[i - 2]]];
  y]

n = 1;
SetterBar[Dynamic@n, Range[1, 10, 1]]

bar

Dynamic[f[x]]

$\{0,0,0,0,0,0,0,0,0,0\}$

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks so much! I guess I was writing the Dynamics inside-out, or so to say - I also fixed the extra line I added to the question, thanks to you: Manipulate[ Dynamic[Table[ ToExpression["x" <> ToString@i], {i, 1,n + 1}]], {x1, 1, 10}, {x2, 1, 10}] $\endgroup$ – David Roberts Aug 24 '14 at 18:55

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.