Message Boards Message Boards

0
|
3232 Views
|
4 Replies
|
1 Total Likes
View groups...
Share
Share this post:

How do I use a Do loop on a Manipulate?

Posted 2 years ago

How do I use Table and Do loop iterators inside Manipulate? Is there a way to use Iterators inside a Manipulate function?

 Do[
  ToExpression[
    "CombinedCartesianPlot\[NumberSign]" <> ToString[Mass] <> 
   "\[NumberSign]" <> ToString[Radius], StandardForm,
    Function[sym, 
     sym = Manipulate[Show[
      {If[a == 1, 
        ToExpression[ 
         "CartesianPlotWith\[NumberSign]" <> ToString[Mass] <> 
          "\[NumberSign]" <> ToString[Radius]], Nothing], 
       If[a == 1, 
        ToExpression[
         "CartesianPlotWithout\[NumberSign]" <> ToString[Mass] <> 
          "\[NumberSign]" <> ToString[Radius]], Nothing]},   
      PlotLabel -> 

       "Positions of With and Without Photophoresis of a particle \
with #InsertRadiusHere#", 
      Frame -> True], {{a, 1, "With"}, {1, 0}}, {{b, 1, 
       "Without"}, {1, 0}}, ControlPlacement -> Top, 
     FrameMargins -> 0, TrackedSymbols :> {a, b}], 
      HoldAll]
    ],
  {Mass, 1, 6}, {Radius, 1, 6}
  ]

The Do Loop Iterators (Mass & Radius) would not be evaluated and are labeled Red instead.

4 Replies

Lance,

You should not be using strings to construct expressions in Mathematica except in unusual cases. In this case you can do what you want directly without any strings. I can't understand what you want the final output to look like unless you describe it or draw a picture, but a simple example of a something related would look like this:

Manipulate[
 Column[Table[
   Show[Plot[a * x^(2 + (b - 1)/10), {x, 0, 3.5}], 
    ListPlot[a*{1, 4, 9}], ImageSize -> Medium], {b, 4}]], {a, 1, 5}]

or a grid like this:

Manipulate[
 Grid[Partition[
   Table[
    Show[Plot[a * x^(2 + (b - 1)/10), {x, 0, 3.5}], 
     ListPlot[a*{1, 4, 9}], ImageSize -> Medium], {b, 4}], 2]], {a, 1,
   5}]

You use Table to create a list of Plots and partition it into a Grid (or use Table with two iterators). The you can use manipulate to alter Parameters that you want to change by hand.

Regards,

Neil

POSTED BY: Neil Singer
Posted 2 years ago

There are several things to clear up here before I could answer this question.

  • Show is typically used with Graphics expressions. I'm not sure that this would work even if the iterators resolved the way you expect.
  • Your Function assigns a value to its argument. It might be simpler to just use Set directly, or maybe define some helper function so that this piece of the puzzle is clearer.
  • The value that Function sets its argument to is a Manipulate, so it seems like what you're trying to do is create a bunch of Manipulate expressions. It might be easier to just create a single Manipulate that includes the additional parameters of Mass and Radius.
  • You seem to be creating a symbol that will be the thing that gets assigned a value by the Function, but the symbol itself shows up inside the value you're assigning to it. I'm worried there'll be some sort of infinite recursion happening here.

Here is a possible line of thought. Before trying to "one-line" it, just break it up into pieces:

(*A function for creating a symbol*)
NameMassRadius[mass_, radius_] := 
  Symbol["CombinedCartesianPlot\[NumberSign]" <>ToString[mass] <> "\[NumberSign]" <> ToString[radius]]

(*A function for creating a value to be assigned to a variable*)
MassRadiusValue[mass_, radius_] := SomeFunction[mass, radius]

(*A function to do the assignment*)
AssignMassRadius[name_Symbol, mass_, radius_] := (name = MassRadiusValue[mass, radius])

(*Set up a tabular representation of the assignments you want*)
assignmentData = 
 Table[{NameMassRadius[mass, radius], MassRadiusValue[mass, radius]}, {mass, 1, 3}, {radius, 1, 3}]

(*Do the assignments*)
Scan[Apply[Set], assignmentData, {-3}]

Now, if that all fits in with what you're trying to do, then the next step would be figuring out what MassRadiusValue should really be, presumably some Manipulate expression.

POSTED BY: Eric Rimbey
Posted 2 years ago

Oops, realized that I ultimately chose not to use AssignMassRadius, but hopefully you get the idea.

POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract