import matplotlib.pyplot as plt import numpy as np class piecewise: def __init__(self,time,values): if len(time) != len(values): print("Wrong arguments to piecewise function") self.time = time self.values = values def __call__(self,t): for t_, v_ in zip(self.time,self.values): if t < t_: return v_ return 0 #Example code: define and plot a piecewise constant function phases = [4,28,33] sigma = piecewise(phases,[20,2,0]) t = np.linspace(0,50,100) s = [] #class does not work for arrays because of if test, hence a for-loop: for t_ in t: s.append(sigma(t_)) plt.plot(t,s) plt.show()