python分段函数如何编写?


本文摘自php中文网,作者coldplay.xixi,侵删。

python分段函数如何编写?

python编写分段函数的方法:

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

代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

#!/usr/bin/python

# -*- coding:utf-8 -*-

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()

662f89dd2589ebdf83b68bef2e39eaa.png

51b2feb05988a2cf457839a1c0cf03f.png

2.使用Matplotlib绘制分段函数:

阅读剩余部分

相关阅读 >>

Python如何把数字变成日期

Python中常见字符串方法推荐

Python1-100怎样偶数求和?

Python是脚本语言吗

Python列表如何去重?

Python实现简单配置发送邮件的功能

Python线程池threadpool使用篇

Python如何创建空列表

Python实现简单的图片文字识别脚本

Python目录如何修改(实例解析)

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...