Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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]
share|improve this question
    
Try Evaluate[Sequence @@ Partition[{L1}, 3]] –  Simon Woods Nov 21 '13 at 10:56
1  
I think one is better off writing {a, 0, 1}, {b, 0, 1},... but one is entitled to one's own taste. –  Michael E2 Nov 22 '13 at 0:48

1 Answer 1

up vote 0 down vote accepted

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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