Message Boards Message Boards

0
|
3990 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:
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}];
 
POSTED BY: Mark Turtsinskyi
4 Replies

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.

POSTED BY: David Reiss
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 BY: Jim Baldwin

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 BY: Sean Clarke

On my system it seems to work as expected if only I remove the ";" character at the very end.

POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract