1
$\begingroup$

For instance, I have a some "commands" that I use regularly, for example:

  Manipulate[Plot[fun, {var, ldv, udv}, PlotRange -> {{plx1, plx2}, {ply1, ply2}}],    {av , lav, avu}].

So I define a custom function to avoid all the parethesis and braces as follows:

  cman[fun_, var_, ldv_, udv_, plx1_, plx2_, ply1_, ply2_, av_, lav_, uav_ ] := 
  Manipulate[Plot[fun, {var, ldv, udv}, PlotRange -> {{plx1, plx2}, {ply1, ply2}}] ,  {av , lav, uav}].

This works exactly how I want it to, for this special case. However what do we do if we have more manipulation variables. For instance I would like to similarly flatten out the following code,

  Manipulate[Plot[fun, {var, ldv, udv}, PlotRange -> {{plx1, plx2}, {ply1, ply2}}],  {av1 , lav1, uav1}, {av2 , lav2, uav2},{av3 , lav3, uav3}, ...{avn , lavn, uan}]

(of course the three dots are not really code but just their to indicate the idea that i want to be able to stack as many manipulate variables as possible).

The strategy that I came up with is as follows: I can condense the manipulation variables into one variable by starting with the , List[av1 , lav1, uav1, av2 , lav2, uav2, av3 , lav3, uav3, ...avn , lavn, uan]

Then hit the list with

   Partition[List[av1 , lav1, uav1, av2 , lav2, uav2, av3 , lav3, uav3, ...avn ,       lavn, uan],3]

Which give the list,

   {{av1 , lav1, uav1}, {av2 , lav2, uav2},{av3 , lav3, uav3}, ...{avn , lavn, uan}}.

However I can not put the code in like that so I must turn this list into a sequence, for which the following is used:

  l2s[{}] := Sequence[]; l2s[L_] := Sequence[First[L], l2s[Rest[L]]]

I then attempt to flatten my code with the following:

  Cman[fun_, var_, ldv_, udv_, plx1_, plx2_, ply1_, ply2_, L__ ] := 
  Manipulate[Plot[fun, {var, ldv, udv}, PlotRange -> {{plx1, plx2}, {ply1, ply2}}], 
  l2s[Partition[List[L], 3]]].

This gives however gives me the following error:

Manipulate::vsform: "Manipulate argument l2s[Partition[{a,0,1},3]] does not have the correct form for a variable specification"

My question is therefore twofold: 1) is their a way to make it work through the error, since

 l2s[Partition[List[1, 2, 3, 4, 5, 6], 3]]

give the output Sequence[{1, 2, 3}, {4, 5, 6}]?

2) Is their another better way to do this?

I want to know if I can make my approach work, so I can better understand the program. For the same reason, I am also interested in better alternatives.

Note: For my practical purposes, the following code is pretty good.

  custman[fun_, var_, ldv_, udv_, plx1_, plx2_, ply1_, ply2_, L1__] := 
  Manipulate[
  Plot[fun, {var, ldv, udv}, 
  PlotRange -> {{plx1, plx2}, {ply1, ply2}}], L1].

However one must use the function as follows,

 custman[a x^2 + b, x, 0, 1, 0, 1, 0, 9, {a, 0, 1}, {b, 0, 1}]

I want use the function in the following way:

 custman[a x^2 + b, x, 0, 1, 0, 1, 0, 9, a, 0, 1, b, 0, 1]
$\endgroup$
2
  • $\begingroup$ Try Evaluate[Sequence @@ Partition[{L1}, 3]] $\endgroup$ Nov 21, 2013 at 10:56
  • 1
    $\begingroup$ I think one is better off writing {a, 0, 1}, {b, 0, 1},... but one is entitled to one's own taste. $\endgroup$
    – Michael E2
    Nov 22, 2013 at 0:48

1 Answer 1

1
$\begingroup$

Simon Woods, in his comment, gave you the code pattern you need. Here is an example of it applied to a somewhat simpler Manipulate. It shouldn't be too difficult to generalize.

custom[obj_, ctrls__] :=
 Manipulate[
   obj,
   Evaluate[Sequence @@ Partition[{ctrls}, 3]]]
custom[a x^2 + b, a, 0, 1, b, 0, 1]

manip.png

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.