Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'Plotting in the If[]' topicStudent Support Forum > General > "Plotting in the If[]"

< Previous Comment | Next Comment >Help | Reply To Comment | Reply To Topic
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: ,

Subject (listing for 'Plotting in the If[]')
Author Date Posted
Plotting in the If[] PhysicsGuy 10/24/12 8:59pm
Re: Plotting in the If[] yehuda 10/25/12 1:33pm
Re: Plotting in the If[] Bill Simpson 10/25/12 1:51pm
Re: Re: Plotting in the If[] PhysicsGuy 10/25/12 1:58pm
< Previous Comment | Next Comment >Help | Reply To Comment | Reply To Topic