绘制分段函数:y=4sin(4πt)-sgn(t-0.3)-sgn(0.72-t)
import numpy as np import matplotlib.pyplot as plt def sgn(x): if x > 0: return 1 elif x < 0: return -1 else: return 0 t = np.arange(0, 1, 0.01) y = [] for i in t: y_1 = 4 * np.sin(4 * np.pi * i) - sgn(i - 0.3) - sgn(0.72 - i) y.append(y_1) plt.plot(t, y) plt.xlabel("t") plt.ylabel("y") plt.title("Heavsine") plt.show()
效果如下:
更多Python知识请关注。