1
$\begingroup$

I've noticed that when "Mod" and "Which" are used to the same effect, they plot to slightly different results. Take the following example:

f[x_] = 2 x;
fmod[x_] = Mod[f[x], 1];
Plot[fmod[x], {x, 0, 1}]
fwhich[x_] = Which[x < 1/2, f[x], x > 1/2, f[x] - 1];
Plot[fwhich[x], {x, 0, 1}]

The output graphs are:

enter image description here

enter image description here

How can I graph Which without getting that connecting line?

$\endgroup$

closed as off-topic by Bob Hanlon, m_goldberg, dr.blochwave, MarcoB, Oleksandr R. Sep 11 '15 at 2:03

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Bob Hanlon, m_goldberg, dr.blochwave, MarcoB
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    $\begingroup$ I would suggest that you use a Piecewise function definition in your case, rather than Which: fpiece[x_] = Piecewise[{{f[x], x < 1/2}, {f[x] - 1, x > 1/2}}];. $\endgroup$ – MarcoB Sep 10 '15 at 15:49
  • $\begingroup$ fmod[1/2] evaluates to 0. fwhich[1/2] is undefined. This may be related to the result you are seeing. $\endgroup$ – Jack LaVigne Sep 10 '15 at 23:29
  • $\begingroup$ possible duplicate of Plotting jump function without vertical lines $\endgroup$ – Oleksandr R. Sep 11 '15 at 2:03
5
$\begingroup$

As MarcoB already pointed out in the comments, Piecewise is probably the better alternative.

Additionally, we already have a related question with good answers where you can steal ideas from:

f[x_] = 2 x;
fmod[x_] = Mod[f[x], 1];
fwhich[x_] = Which[x < 1/2, f[x], x > 1/2, f[x] - 1];
Plot[fwhich[x], {x, 0, 1}, Exclusions -> {1/2}]

Mathematica graphics

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.