I want to plot the following:

Plot[{Sqrt[x], Sum[HeavisideTheta[x - n^2], {n, 1, Floor[x]}]}, {x, 0,50}]

which looks like

sum of heaviside step functions

Is there a simple way to get this same plot with vertical lines at the jumps between steps?

  • $\begingroup$ I'm not sure what happened but I was messing around and suddenly the steps appeared, I'm not sure why, the code is exactly the same, but now it is throwing this error: Plot::exclul: {Sin[\[Pi] Re[x]]-0,Sin[\[Pi] Im[x]]-0,Im[-n^2+x]-0,(-n^2+x)-0} must be a list of equalities or real-valued functions. $\endgroup$
    – Kai
    Dec 3 '17 at 3:10

See the option Exclusions:

Plot[{Sqrt[x], Sum[HeavisideTheta[x - n^2], {n, 1, Floor[x]}]}, {x, 0, 50},
 Exclusions -> None]

Mathematica graphics


The only easy method I'm aware of is to make it a parametric plot instead.

wrapper[f_] := {#1, f[#1]} &;
    wrapper[Sum[HeavisideTheta[# - n^2], {n, 1, Floor[#]}] &][x]}
    , {x, 0, 50}

Produces the output:

enter image description here

I also receive this error you mention in your comment. I don't understand why; maybe ParametricPlot thinks the functions may not have real values. If you're confident there's no problem with your input, you can wrap ParametricPlot in Quiet[ ... , ParametricPlot:exclul to prevent this message from appearing.


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.