python可视化函数展开

矩形波

周期为 2 π 2\pi 2π的矩形波函数:

z ( t ) = { − 1 , − π ≤ t < 0 1 , 0 ≤ t < π z(t)=\begin{cases}-1,-\pi\le t\lt 0\\\quad 1,\quad0\le t\lt \pi\end{cases} z(t)={1πt<010t<π

= 4 π [ sin ⁡ t + 1 3 sin ⁡ 3 t + ⋯ + 1 2 n + 1 sin ⁡ ( 2 n + 1 ) t + ⋯   ] =\frac{4}{\pi}\left[\sin t+\frac{1}{3} \sin 3 t+\cdots+\frac{1}{2 n+1} \sin (2 n+1) t+\cdots\right] =π4[sint+31sin3t++2n+11sin(2n+1)t+]

import matplotlib.pyplot as plt
import numpy as np
import random


def randomColor(num):
    '''随机颜色'''
    colors = []
    for i in range(num):
        colArr = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
        color = ""
        for i in range(6):
            color += colArr[random.randint(0, 14)]
            if any(color in colExiting for colExiting in colors):
                continue
        colors.append("#" + color)
    return colors


fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
w = np.arange(1, 30, 2)  # 角频率
z = 0
colors = randomColor(len(w))

x = np.arange(-8, 8, 0.01)
font = {'family': 'serif',
        'weight': 'normal',
        'size': 14,
        }
for c, n in zip(colors, np.arange(0, len(w))):
    z_i = np.sin(w[n] * x) / w[n]
    z = z + z_i
    l = r'$\frac{sin(%dt)}{%d}$' % (2 * n + 1, 2 * n + 1)
    ax.scatter(x, w[n], z_i, color=c, alpha=0.5, linewidths=0.01)
    ax.text(12 - n % 2 * 2, w[n] - 1, 0, l, fontdict=font)
ax.scatter(x, -1, z, color='r', alpha=0.5, linewidths=0.01)


ax.set_zlim(-1.5, 1.5)
ax.set_xlabel('t')
ax.set_ylabel('w')
ax.set_zlabel('z')
plt.show()


在这里插入图片描述
在这里插入图片描述

e x \mathrm{e}^{x} ex

e x = 1 + x + x 2 2 ! + ⋯ + x n n ! + ⋯   ) \mathrm{e}^{x}=1+x+\frac{x^{2}}{2 !}+\cdots+\frac{x^{n}}{n !}+\cdots) ex=1+x+2!x2++n!xn+)

import matplotlib.pyplot as plt
import numpy as np
import random


def randomColor(num):
    '''随机颜色'''
    colors = []
    for i in range(num):
        colArr = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
        color = ""
        for i in range(6):
            color += colArr[random.randint(0, 14)]
            if any(color in colExiting for colExiting in colors):
                continue
        colors.append("#" + color)
    return colors


fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
w = np.arange(1, 36, 2) 
z = 0
colors = randomColor(len(w))
x = np.arange(-6, 6, 0.01)
font = {'family': 'serif',
        'weight': 'normal',
        'size': 14,
        }

for c, n in zip(colors, np.arange(0, len(w))):
    z_i = x**n/np.math.factorial(n)
    z = z + z_i
    l = r'$\frac{x^{%d}}{%d!}$' % (n,n )
    ax.scatter(x, w[n], z_i, color=c, alpha=0.5, linewidths=0.01)
    ax.text(12 - n % 2 * 2, w[n] - 1, 0, l, fontdict=font)
ax.scatter(x, -1, z, color='r', alpha=0.5, linewidths=0.01)


ax.set_zlim(-6, 6)
ax.set_zticks([])
ax.set_xlabel('t')
ax.set_ylabel('w')
ax.set_zlabel('z')
plt.show()

在这里插入图片描述
在这里插入图片描述

sin ⁡ x \sin x sinx

sin ⁡ x = x − x 3 3 ! + ⋯ + ( − 1 ) n ( 2 n + 1 ) ! x 2 n + 1 + ⋯ \sin x=x-\frac{x^{3}}{3 !}+\cdots+\frac{(-1)^{n}}{(2 n+1) !} x^{2 n+1}+\cdots sinx=x3!x3++(2n+1)!(1)nx2n+1+

import matplotlib.pyplot as plt
import numpy as np
import random


def randomColor(num):
    '''随机颜色'''
    colors = []
    for i in range(num):
        colArr = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
        color = ""
        for i in range(6):
            color += colArr[random.randint(0, 14)]
            if any(color in colExiting for colExiting in colors):
                continue
        colors.append("#" + color)
    return colors

def getSign(x):
    if x>=0:
        return ''
    else:
        return '-'

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
w = np.arange(1, 30, 2) 
z = 0
colors = randomColor(len(w))
x = np.arange(-8, 8, 0.01)
font = {'family': 'serif',
        'weight': 'normal',
        'size': 14,
        }

for c, n in zip(colors, np.arange(0, len(w))):
    z_i = (-1)**n*x**(2*n+1)/np.math.factorial(2*n+1)
    z = z + z_i
    l=r'$\frac{%sx^{%d}}{%d!}$'% (getSign((-1)**n),2*n+1,2*n+1)
    ax.scatter(x, w[n], z_i, color=c, alpha=0.5, linewidths=0.01)
    ax.text(12 - n % 2 * 3, w[n] - 1, 0, l, fontdict=font)
ax.scatter(x, -1, z, color='r', alpha=0.5, linewidths=0.01)


ax.set_zlim(-6, 6)
ax.set_zticks([])
ax.set_xlabel('t')
ax.set_ylabel('w')
ax.set_zlabel('z')
plt.show()

在这里插入图片描述

cos ⁡ x \cos x cosx

cos ⁡ x = 1 − x 2 2 ! + ⋯ + ( − 1 ) n ( 2 n ) ! x 2 n + ⋯ \cos x=1-\frac{x^{2}}{2 !}+\cdots+\frac{(-1)^{n}}{(2 n) !} x^{2 n}+\cdots cosx=12!x2++(2n)!(1)nx2n+

import matplotlib.pyplot as plt
import numpy as np
import random


def randomColor(num):
    '''随机颜色'''
    colors = []
    for i in range(num):
        colArr = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
        color = ""
        for i in range(6):
            color += colArr[random.randint(0, 14)]
            if any(color in colExiting for colExiting in colors):
                continue
        colors.append("#" + color)
    return colors

def getSign(x):
    if x>=0:
        return ''
    else:
        return '-'

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
w = np.arange(1, 30, 2)
z = 0
colors = randomColor(len(w))
x = np.arange(-8, 8, 0.01)
font = {'family': 'serif',
        'weight': 'normal',
        'size': 14,
        }

for c, n in zip(colors, np.arange(0, len(w))):
    z_i = (-1)**n*x**(2*n)/np.math.factorial(2*n)
    z = z + z_i
    l=r'$\frac{%sx^{%d}}{%d!}$'% (getSign((-1)**n),2*n,2*n)
    ax.scatter(x, w[n], z_i, color=c, alpha=0.5, linewidths=0.01)
    ax.text(12 - n % 2 * 3, w[n] - 1, 0, l, fontdict=font)
ax.scatter(x, -1, z, color='r', alpha=0.5, linewidths=0.01)


ax.set_zlim(-6, 6)
ax.set_zticks([])
ax.set_xlabel('t')
ax.set_ylabel('w')
ax.set_zlabel('z')
plt.show()

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二进制人工智能

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值