What I'm trying to do is simply a variation of a heavyside function, for 3 before the discontinuity and 4 after. But when I plot it, at the discontinuity it's not a straight vertical line but a sort of $x=\alpha*x$ function. Can someone explain why it's like that?figure of the solution+code


1 Answer 1


What happens as you add more points to your domain near $x=x_c$? Also, many (most, all(?)) definitions of the Heaviside step function give a value of the mean of the extremes at the transition point, but you initialize to zero and skip $x=x_c$ in your if-block. You should handle that case, too.

Also, why write your own when numpy supplies one?

Edited to address the comment about the Python numpy.heaviside() function (you can have exactly what you ask for):

import numpy
array([3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 4., 5., 5., 5., 5., 5., 5.,
       5., 5., 5., 5.])

Here I've carefully chosen my parameters to get a point right on the transition point so that there's one there on the output. As for any value you want, you can always manipulate the result to adjust it to the domain and range you like.

  • $\begingroup$ Thanks for your help. As I add more points, it's better, the problem is I'm limited in term of point because it is my initial condition of a differential equation so my mesh can't have too many points. The problem of the heavyside function in Python is that I don't want it for 0 and 1, but rather, for any value I want. $\endgroup$
    – maths_mz
    Nov 13, 2020 at 16:09

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.