0
|
4031 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
GROUPS:

# problems with ReplaceAll

Posted 10 years ago
 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.   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}];  
4 Replies
Sort By:
Posted 10 years ago
 On my system it seems to work as expected if only I remove the ";" character at the very end.
Posted 10 years ago
 The semicolon suppresses output.If you evaluate "2+2", Mathematica will give "4". If you evaluate "2+2;", Mathematica will give nothing as output.
Posted 10 years ago
 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}] 
Posted 10 years ago
 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.