zanmat2
(Mark Turtsinskyi)
October 6, 2014, 8:36am
1
Hi everybody!
When I was trying to compile the following code, a very strange mistake occured (in some cases there was nothing to be shown and in other cases - the code began evaluating and then unexpectedly stopped). In what way might that code be improved or corrected?
Thanks a lot in advance.
<code>
Manipulate[
f[x_] := Sin[t*x];
g[x_] := Cos[u*x];
Plot[Evaluate[(f[x] + g[x])] /. {t -> (s/2), u -> 8*v}, {x, 0,
8*Pi}], {s, 0, 20}, {v, 0, 6}];
</code>
On my system it seems to work as expected if only I remove the “;” character at the very end.
seanc
(Sean Clarke)
October 6, 2014, 3:02pm
3
The semicolon suppresses output.
If you evaluate “2+2”, Mathematica will give “4”. If you evaluate “2+2;”, Mathematica will give nothing as output.
jbaldwin
(Jim Baldwin)
October 6, 2014, 3:13pm
4
I think you also want to add a TrackedSymbols option so that the Manipulate does not continually eat up cpu cycles:
Manipulate[f[x_] := Sin[t*x];
g[x_] := Cos[u*x];
Plot[Evaluate[(f[x] + g[x])] /. {t -> (s/2), u -> 8*v}, {x, 0,
8*Pi}], {s, 0, 20}, {v, 0, 6}, TrackedSymbols :> {s, v}]
dreiss
(David Reiss)
October 6, 2014, 6:24pm
5
TrackedSymbols works well here as Jim points out. An alternative is to specify your function definitions in the Initialization option as in
Manipulate[
Plot[Evaluate[(f[x] + g[x]) /. {t -> (s/2), u -> 8*v}], {x, 0, 8*Pi}],
{s, 0, 20}, {v, 0, 6},
Initialization ->
{f[x_] := Sin[t*x],
g[x_] := Cos[u*x]}]
Note also that I put the replacement rule inside the Evaluate as this is more in line with what Evaluate’s intended use is here.