       Re: Loops in Manipulate

• To: mathgroup at smc.vnet.net
• Subject: [mg107684] Re: [mg107655] Loops in Manipulate
• From: "David Park" <djmpark at comcast.net>
• Date: Mon, 22 Feb 2010 19:05:15 -0500 (EST)
• References: <31633330.1266826380053.JavaMail.root@n11> <001c01cab3bf\$2555b5e0\$700121a0\$@net> <CD984E9F-AFF4-4A52-9CC2-71885C6F5E22@optonline.net>

```One of the problems I often see in MathGroup is attempting some kind of
complicated calculation inside some high level operation, such as Plot or
Manipulate, without sufficiently testing out the calculation by itself, and
making certain it produces what is required for the higher level operation.

I'm not completely certain of your objective but I hope this will be close
enough.

Here is a routine, f, that I believe carries out your calculation.

f[n_Integer?Positive][t_Integer?Positive] :=
Module[
{n1, n2, list = Table[5, {n}]},
Do[
n1 = RandomInteger[{1, n}];
n2 = RandomInteger[{1, n}];
If[n1 != n2 && list[[n2]] != 0, {list[[n1]] = list[[n1]] + 1,
list[[n2]] = list[[n2]] - 1}], {t}];
list]

You can test it with various cases.

f
{4, 4, 7, 4, 6, 5}

Then it is easy to write a Manioulate:

Manipulate[
f[t],
{t, 1, 5, 1, Appearance -> "Labeled"}]

But probably better here would be a SetterBar.

Manipulate[
f[t],
{{t, 1}, SetterBar[Dynamic[t], Range] &}
]

David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/

From: benson sundheim [mailto:nyuout at optonline.net]

On Feb 22, 2010, at 8:01 AM, David Park wrote:

> A Do statement doesn't have any output so you would need something
> more.
>
> Maybe you could first write and test outside the Manipulate a
> routine with
> the control variable q:
>
> f[q_]:=...
>
> Then if that works and produces the display you want put it in the
> Manipulate.
>
> Otherwise post a specific example.
>
>
> David Park
> djmpark at comcast.net
> http://home.comcast.net/~djmpark/
>
>
> From: nyu [mailto:nyuout at optonline.net]
>
> In trying to set up Manipulate wherein the controlled variable is part
> of the iterator in a Do loop or the target in a While loop, I get
> sliders but no other output  than  {Null}. ???????????
>
>
e.g.;
n = 10;
list = Table[5, {n}];
Manipulate[
{Do[
{
n1 = Random[Integer, {1, n}],
n2 = Random[Integer, {1, n}],
If[n1 != n2 && list[[n2]] != 0, {list[[n1]] = list[[n1]] + 1,
list[[n2]] = list[[n2]] - 1}], Print[list]
}, {i, 1, t, 1}
]}, {t, 1, 5, 1}
]

```

• Prev by Date: Re: Re: DiscreteUniformDistribution PDF isn't piecewise?
• Next by Date: Re: Error List Plot with Legends
• Previous by thread: Re: Loops in Manipulate
• Next by thread: Re: Loops in Manipulate