I would like to plot a function like this one:

f[x_] = Piecewise[{{x, x ∈ Integers && x > 0}, {1/2, x < 0}}, Indeterminate]

DiscretePlot[f[x], {x, -2, 2}] 

Plot[f[x], {x, -2, 2}]

When I use DiscretePlot I get only the first part and when I use Plot I get only the part with y = 1/2. How can I solve this?

  • $\begingroup$ it works for me. can you provide your code. $\endgroup$ May 7 '15 at 15:16
  • $\begingroup$ fro the DiscretePlot try AxesOrigin -> {0, 0}, for Plot, it is ok because Plot dose not do Discrete Plot $\endgroup$ May 7 '15 at 15:23
  • $\begingroup$ When I add AxesOrigin -> {0, 0} to DiscretePlot, there are points (x, 1/2) where x is Integer, still it's not continuous $\endgroup$
    – adolzi
    May 7 '15 at 15:25
  • $\begingroup$ DiscretePlot does not do continuous plotting. continuous can be done with Plot. but remember, your function when x>0 is not continues it is discrete (f[0.5] return Indeterminate) $\endgroup$ May 7 '15 at 15:28
  • $\begingroup$ I know, and it is the most important for me, because I need to create a function in part discrete and in part continuous. But I have problem with plot $\endgroup$
    – adolzi
    May 7 '15 at 15:32
Show[Plot[f[x], {x, -2, 0}], 
 DiscretePlot[f[x], {x, 0, 2}, AxesOrigin -> {0, 0}], 
 PlotRange -> All]

enter image description here


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.