I am trying to define multiple functions with the same set of parameters and then put them in a Manipulate to simulate their behavior.
Ideally it should be something like that:
par={a,b,c};
f[q_,a_,b_,c_]:=q+a+b+c;
g[q_,a_,b_,c_]:=q-a-b-c;
Then afterwards I would like to have something like
Manipulate[
Plot[f[q-5, par] + g[q+5, par], {q,0,10}],
{{a,0},-1,1},{{b,0},-1,1},{{c,0},-1,1}]
I know that I can use the Apply command and write something like
f @@ par
The problem is that then I don't know how to still operate on the q parameter and, in addition, even if I add the q parameter in par and then write f@@par, the Manipulate does not work anymore.
Is it possible to solve this issue or I have to stick with this piece of code?
Manipulate[Plot[f[q-5,a,b,c] + g[q+5,a,b,c], {q,0,10}],{{a,0},-1,1},{{b,0},-1,1},{{c,0},-1,1}]
Manipulate
localizes its variables, while the symbols inpar
are global (in the"Global`"
context). Your workaround seems reasonable. There's also the optionLocalizeVariables -> False
, but on the face of it, I prefer your workaround. $\endgroup$LocalizeVariables -> False
workaround that would keep my code way more smooth. The problem is that it does not seem to work even with a single function likef@@par
. $\endgroup$a
,b
, andc
, and that that is the expected behavior. $\endgroup$f[q - 5, Sequence @@ par]^2 + g[q + 5, Sequence @@ par]
so I could see whether a solution was working. :) $\endgroup$