5
$\begingroup$

I have a simple Piecewise function that defines a square pulse:

f[x_] := 
 Piecewise[{
  {0, x < -(1/2)},
  {1/2, x == -(1/2)},
  {1, -(1/2) < x < 1/2}, 
  {1/2, x == 1/2},
  {0, x > 1/2}
 }]

However, Plot produces a non-joined-up graph:

Plot[f[x], {x, -1, 1}, GridLines -> Automatic]

enter image description here

Mathematica seems to be ignoring the definition f[x] = {1/2, x == -(1/2)}. But I want to specify that f takes half-values at the discontinuities.

Why is Mathematica happy at x = 1/2 but not at x = -(1/2)? What am I doing wrong?

$\endgroup$
  • 2
    $\begingroup$ Add Exclusions -> None to the Plot $\endgroup$ – MarcoB Jun 22 at 14:15
  • $\begingroup$ Thanks @MarcoB. Can you post that as an answer? $\endgroup$ – Richard Burke-Ward Jun 22 at 14:22
6
$\begingroup$

Adding Exclusions -> None will take care of that:

Plot[f[x], {x, -1, 1}, GridLines -> Automatic, Exclusions -> None]

plot without discontinuities

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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