| Author |
Comment/Response |
yehuda
|
10/25/12 1:33pm
The If command return nothing
what you need is to generate a side effect, that is, a function that would generate the graphics no matter what its return value is
Use Print for that purpose
The following code WILL NOT generate the graphical output, since in each branch of the If command there is a non graphic command
f[x_] := If[x < 0.5, Plot[Sin[t], {t, 0, 6}]; y = 0,
Plot[Cos[t], {t, 0, 6}]; y = 1]
Now, notice that
f[0.1] returns 0
and
f[2] returns 1
and f[any number];
displays nothing since it ends with a semicolon
and for the side effect
g[x_] := If[x < 0.5, Print[Plot[Sin[t], {t, 0, 6}]]; y = 0,
Print[Plot[Cos[t], {t, 0, 6}]]; y = 1]
so
g[0.1] plots a sine and returns 0
g[2] plots a cosine and returns 1
g[0.1];
plots a sine and returns 0 (but displays nothing since it ends with a semicolon)
and
g[2];
plots a cosine and returns 1 (but displays nothing since it ends with a semicolon)
so g works with side effects
URL: , |
|