python 绘制分段函数图像

绘制分段函数:

      y = \left\{\begin{matrix} cos(x).....(0<x<30) & \\ x............(30\leq x <50)& \\ sin(x)......(50\leq x)& \end{matrix}\right.

首先导入matplotlib和numpy包:

import matplotlib.pyplot as plt
import numpy as np

规定x范围0-100,1000个点:

x = np.linspace(0,100,1000)

设置区间:

interval0 = [1 if (i<30) else 0 for i in x]
interval1 = [1 if (i>=30 and i<50) else 0 for i in x]
interval2 = [1 if (i>=50) else 0 for i in x]

列函数式:

y = np.cos(x)* interval0 + x * interval1 + np.sin(x)*interval2

绘图显示:

plt.plot(x,y)
plt.show()

结果:

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值