python - Why is the output of my function in binary? -
i wrote differentiable heaviside function , vectorised it. however, output seems odd , binary. code follows:
import numpy np import matplotlib.pyplot plt def heaviside(x, epis): if (x>= epis): y=1 elif (x< -epis): y=0; else: y = 0.5 + x/(2*epis) + np.sin(np.pi*x/epis)/(2*np.pi); print (x, y); return y; x1 = np.linspace(-1, 1, 100); vheaviside = np.vectorize(heaviside); y1 = vheaviside(x1, 0.2); plt.plot(x1, y1, 'b', alpha=0.3) plt.show() the input array in [-1, 1], , outputs supposed continuous heaviside function. however, of output 0 or 1. why?
a simpler , more efficient way of defining vectorized heaviside function numpy using numpy.piecewise:
def heaviside(x, epis): return np.piecewise(x, (x>=epis, x<-epis), (1, 0, lambda x: 0.5 + x/(2*epis) + np.sin(np.pi*x/epis)/(2*np.pi) ) )
Comments
Post a Comment