Re: Giving an arbitrary number of arguments to Manipulate using Apply
- To: mathgroup at smc.vnet.net
- Subject: [mg94650] Re: Giving an arbitrary number of arguments to Manipulate using Apply
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Fri, 19 Dec 2008 07:22:17 -0500 (EST)
- Organization: Uni Leipzig
- References: <gidf9a$db$1@smc.vnet.net>
- Reply-to: kuska at informatik.uni-leipzig.de
Hi,
testFunc[expr_, vars_] :=
Module[{newFunc, args, i, p},
newFunc = Apply[Plus, expr] /. vars[[1]] -> p;
args :=
Join[{Hold[Plot][newFunc, {p, 0, 10},
Epilog -> Text[vars[[1]], {5, 1}]]},
Table[{vars[[i]], 0, 10}, {i, 1, Length[vars]}]];
Apply[Manipulate, args] // ReleaseHold]
testFunc[{Sin[a x], b + x^2, -b x}, {x, a, b}]
??
Regards
Jens
Yoichi Aso wrote:
> 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
>