python中怎样画分段函数?
发布时间:2026-01-16 17:27:53

绘制分段函数:y=4sin(4πt)-sgn(t-0.3)-sgn(0.72-t)
importnumpyasnp
importmatplotlib.pyplotasplt
defsgn(x):
ifx>0:
return1
elifx<0:
return-1
else:
return0
t=np.arange(0,1,0.01)
y=[]
foriint:
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自学网了解更多Python知识。
