MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Giving an arbitrary number of arguments to Manipulate using Apply

  • To: mathgroup at smc.vnet.net
  • Subject: [mg94674] Re: [mg94635] Giving an arbitrary number of arguments to Manipulate using Apply
  • From: "David Park" <djmpark at comcast.net>
  • Date: Fri, 19 Dec 2008 07:26:41 -0500 (EST)
  • References: <20783330.1229605981096.JavaMail.root@m02>

Sometimes I put Print statements into the code so I can see what the
intermediate expressions are. The problem here is to prevent the Plot from
being evaluate until after it is in the Manipulate statement. So use a dummy
plot function and then substitute Plot after the Manipulate statement is
composed. (Manipulate has the Attribute HoldAll.)

testFunc[expr_, vars_] := 
 Module[{newFunc, args, i, p, plotf}, 
  newFunc = Apply[Plus, expr] /. vars[[1]] -> p;
  Print[newFunc];
  args := 
   Join[{plotf[newFunc, {p, 0, 10}, 
      Epilog -> Text[vars[[1]], Scaled[{.5, .9}]]]}, 
    Table[{vars[[i]], 0, 10}, {i, 1, Length[vars]}]];
  Print[args];
  Apply[Manipulate, args] /. plotf -> Plot]

testFunc[{Sin[a x], b + x^2, -b x}, {x, a, b}]


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


From: Yoichi Aso [mailto:aso at caltech.edu] 

Hello,

I have a problem with using Manipulate and Apply.
I'd like to ask for your advice on this.

Here is what I want to do:
I have a list of expressions, expr, and a list of variables, vars.
Each element of expr is a function of variables in vars.
For example, expr={Sin[a x], x^2+b, - b x}; vars = {x,a,b}.
Now, I want to write a function which takes expr and vars as arguments
and does the following.
First, it adds together the elements of expr to make a new function,
newFunc.
Then it creates a Manipulate object with sliders for all the variables in
vars.
The first argument of the Manipulate is a Plot of newFunc as a
function of the first variable
in vars.The value of the other variables are determined by the
sliders. Along with the plot,
the value of the first variable is shown as a Text object in the plot.

Here is a code I came up with to implement the above task.

testFunc[expr_, vars_] := Module[{newFunc, args, i, p},
  newFunc = Apply[Plus, expr] /. vars[[1]] -> p;
  args :=
   Join[{Plot[newFunc, {p, 0, 10}, Epilog -> Text[vars[[1]], {5, 1}]]},
    Table[{vars[[i]], 0, 10}, {i, 1, Length[vars]}]];
  Apply[Manipulate, args]
  ]

However, when I execute, for example,
testFunc[{Sin[a x], b + x^2, -b x}, {x, a, b}]
it shows nothing in the plot area.
I'm guessing it is because Join tries to evaluate the arguments, and
at this moment, Plot cannot draw a graph
because the variables a and b do not have numerical values.
I enclosed the Join with Hold and used ReleaseHold around the Apply,
but still no plot shown.
I used Apply because I don't know the length of vars in advance.

I'd appreciate it if anyone can suggest a solution to this problem.

Thank you in advance,
Yoichi

-- 
----------------------------------------
Yoichi Aso
LIGO Laboratory, California Institute of Technology
aso at caltech.edu or asoy01 at gmail.com




  • Prev by Date: Re: Giving an arbitrary number of arguments to Manipulate using Apply
  • Next by Date: Re: Giving an arbitrary number of arguments to Manipulate using Apply
  • Previous by thread: Re: Giving an arbitrary number of arguments to Manipulate using Apply
  • Next by thread: Re: Giving an arbitrary number of arguments to Manipulate using Apply