I've been fighting with Mathematica's Manipulate function for the last few days for a project.
I'm working on tweaking assumptions and boundary conditions that go into a physical model. For this, I want to be able to plot different equations and adjust the parameters and have the graphs update on the fly. Manipulate seems to be the perfect tool for the job -- except that I can't get it to work. The plots won't update when the parameters are changed.
Basic example:
a =.;
b =.;
c =.;
func1[x_] := a*x;
func2[x_] := a*x^2 + b*x + c;
funcNamesList := {"Linear", "Quadratic"};
funcList := {func1[x], func2[x]}
Manipulate[
Plot[function, {x, -5, 5}], {function,MapThread[Function[#1 -> #2],
{funcList, funcNamesList}]}, {a, -5, 5}, {b, -5, 5}, {c, -5, 5},
LocalizeVariables -> False
]
I can get, for example, func1
to refresh by clicking func1
, adjusting a
, and then clickingfunc1
again, but I'm hoping to have it update when I adjust a
because the real functions I'm using are rather temperamental with respect to their parameters.
-Because I'll be dealing with long functions that have different parameters, using a list of functions is useful.
EDIT:
In case it produces any ideas for anyone, here are some working examples of the individual components of what I want to do (from the Wolfram documentation):
Plot graphs and have them update when parameters are changed:
Manipulate[
Plot[Sin[a x + b], {x, 0, 6}], {{a, 2, "Multiplier"}, 1, 4},
{{b, 0, "Phase Parameter"}, 0, 10}
]
Note: This breaks when the function is taken outside:
func[x] := Sin[a x + b];
Manipulate[
Plot[func[x], {x, 0, 6}], {{a, 2, "Multiplier"}, 1, 4},
{{b, 0, "Phase Parameter"}, 0, 10}, LocalizeVariables -> False
]
Example of changing the function being plotted:
Manipulate[
Plot[f[x], {x, 0, 2 Pi}], {f, {Sin -> "sine", Cos -> "cosine", Tan -> "tangent"}}
]
Edit 2
Changed func2 from a*x^2
to a*x^2 + b*x + c
to reflect the fact that the functions may have different parameters.
Edit 3 Added the tidbit I use to get nice names on the function buttons.
I'd do this in a slightly different way:
but this may be unsuitable for what you want. Do your functions have different signatures?
EDIT: I've renamed the parameter to
b
to make it clearer that is it just a parameter being passed, as opposed to a global variable as you were using it.There are two problems that prevent your
Manipulate
statement from working.First, while the
Manipulate
variablea
is global due to theLocalizeVariables -> False
setting, thePlot
variablex
is not.x
is local to thePlot
expression.The second problem is that
Manipulate
, by default, assumesTrackedSymbols -> Full
. This means that only symbols that explicitly appear in the manipulated expression are tracked. Note thata
does not appear in the expression, so it is not tracked.We can correct both problems thus:
The changes are:
funcList
was changed to{func1, func2}
Plot
expression was changed tofunction[x]
, thereby referencing the localx
variable.Manipulate
optionTrackedSymbols :> {a, function}
was added.function
is initially unset.