Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
1  
Plot[g[z], {z, 0, 1.2}] looks fine. Weird... –  Silvia Mar 30 at 0:35
1  
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. –  Silvia Mar 30 at 0:39
5  
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. –  rcollyer Mar 30 at 0:42
1  
Plot[g[z], {z, 0, 2}, PlotRange -> Full] works also –  Nasser Mar 30 at 0:49
3  
Or PlotRange -> All. But do consider Piecewise for mathematical expressions. Which is for programming. –  Szabolcs Mar 30 at 1:08

1 Answer 1

up vote 5 down vote accepted
Plot[g[z], {z, 0, 2}, PlotRange -> {{0, 2}, All}]

enter image description here

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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