Python绘制分段函数

时间:2021-09-11 07:40:42

1.绘制分段函数:y=4sin(4πt)-sgn(t-0.3)-sgn(0.72-t)

import numpy as np
import matplotlib.pyplot as plt
#绘制分段函数:y=4sin(4πt)-sgn(t-0.3)-sgn(0.72-t) 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绘制分段函数