7
$\begingroup$

If I define

g[z_] := Which[
  0 <= z <= 0.9,     5*(Min[0.1, z] - Max[0, z - 1]),
  0.9 <= z <= 1.1,   5*(Min[0.1, z] - Max[0, z - 1]) + 5*(Min[1, z] - Max[0.9, z - 1]),
  1.1 <= z <= 2,                                       5*(Min[1, z] - Max[0.9, z - 1]),
  True,              0
  ]

and plot using

Plot[g[z], {z, 0, 2}]

I get

enter image description here

while if I plot using

ListPlot[{#, g[#]} & /@ (Range[1000]/500), PlotRange -> All, Joined -> True]

I get

enter image description here

which is what I expect.

Why does Plot fail to plot my function as expected?

$\endgroup$
  • 1
    $\begingroup$ Plot[g[z], {z, 0, 1.2}] looks fine. Weird... $\endgroup$ – Silvia Mar 30 '14 at 0:35
  • 1
    $\begingroup$ The probability tag is for questions involving probability functions in MMA, which didn't present here, thus IMO it's not a proper tag here. $\endgroup$ – Silvia Mar 30 '14 at 0:39
  • 5
    $\begingroup$ Options[Plot[g[z], {z, 0, 2}], PlotRange] returns PlotRange -> {{0., 2.}, {0.5, 0.5}} so it is shrinking the vertical range. Reporting it. $\endgroup$ – rcollyer Mar 30 '14 at 0:42
  • 1
    $\begingroup$ Plot[g[z], {z, 0, 2}, PlotRange -> Full] works also $\endgroup$ – Nasser Mar 30 '14 at 0:49
  • 3
    $\begingroup$ Or PlotRange -> All. But do consider Piecewise for mathematical expressions. Which is for programming. $\endgroup$ – Szabolcs Mar 30 '14 at 1:08
5
$\begingroup$
Plot[g[z], {z, 0, 2}, PlotRange -> {{0, 2}, All}]

enter image description here

$\endgroup$
  • $\begingroup$ Perfect. Should that have been obvious to me? Where is this stuff documented? $\endgroup$ – orome Mar 30 '14 at 0:32
  • 3
    $\begingroup$ @raxacoricofallapatorius I don't think it's obvious. Actually I'd think this is a possible bug. +1 btw $\endgroup$ – Silvia Mar 30 '14 at 0:34
  • $\begingroup$ @Silvia: I'm glad it's not something I missed. Another one of those bugs. $\endgroup$ – orome Mar 30 '14 at 0:35
  • 2
    $\begingroup$ In fact, Plot[g[z], {z, 0, 2}, PlotRange -> All] will work. I think it a bug, too. $\endgroup$ – m_goldberg Mar 30 '14 at 0:46

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.