一些重要的函数图像 matplotlib

CreateTime 2020-12-04
UpdateTime 2021-12-12

import math
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
import warnings
warnings.filterwarnings("ignore")

幂函数

\begin{aligned} y =& x \\ y =& \sqrt x \\ y =& \frac{1}{x} \\ y =& x^2 \\ y =& x^3 \\ \end{aligned}
x = np.linspace(-3 , 3, 64)
x1 = np.sqrt(x)
x2 = np.power(x, 2)
x3 = np.power(x, 3)
x4 = 1/x

plt.plot(x,x,"red",lw=1)
plt.plot(x,x1,"orange",lw=1)  
plt.plot(x,x2,"magenta",lw=1)  
plt.plot(x,x3,"blue",lw=1)
plt.plot(x,x4,"green",lw=1)  
plt.grid(True)
plt.ylim([-2, 2])
plt.xlabel('X')
plt.ylabel('Y')
plt.axhline(color='grey')
plt.axvline(color='grey')
plt.show()

png

指数函数和对数函数

\begin{aligned} y =& a^x \\ y =& \log_a x \end{aligned}
x = np.linspace(-3 , 3 ,64)
x1 = np.power(np.e, x)
x2 = np.log(x)

plt.plot(x,x,"grey",lw=1)
plt.plot(x,x1,"magenta",lw=1) 
plt.plot(x,x2,"orange",lw=1)    
plt.box()
plt.grid(True)
plt.ylim([-3, 3])
plt.xlabel('X')
plt.ylabel('Y')
plt.axhline(color='grey')
plt.axvline(color='grey')
plt.show()

png

三角函数

正弦和余弦

\begin{aligned} y &= \sin x \\ y &= \cos x \end{aligned}
x = np.linspace(-2*np.pi,2*np.pi, 64)
sin = np.sin(x)
cos = np.cos(x)

plt.plot(x,sin,"red",lw=1)
plt.plot(x,cos,"orange",lw=1)  
plt.box()
plt.grid(True)
plt.ylim([-1.5, 1.5])
plt.xlabel('X')
plt.ylabel('Y')
plt.axhline(color='grey')
plt.axvline(color='grey')
plt.axis('equal')
plt.show()

png

正切和余切

\begin{aligned} y = \tan x \\ y = \cot x \end{aligned}
tan = np.tan(x)
cot = 1/tan

plt.plot(x,tan,"red",lw=1)
plt.plot(x,cot,"blue",lw=1)  
plt.grid(True)
plt.box()
plt.ylim([-1.5, 1.5])

plt.axhline(color='grey')
plt.axvline(color='grey')
plt.show()

png

正割和余割

\begin{aligned} y = \sec x \\ y = \csc x \end{aligned}
sec = 1/cos
csc = 1/sin

plt.plot(x,sec,"magenta",lw=1)
plt.plot(x,csc,"blue",lw=1)  
plt.grid(True)
plt.box()
plt.ylim([-1.5, 1.5])

plt.axhline(color='grey')
plt.axvline(color='grey')
plt.show()

png

心形线

y = a( 1 - \cos \theta)

t = np.linspace(-2*np.pi,2*np.pi,64)
r = 2 * (1 - np.cos(t))

ax = plt.subplot(111, projection='polar')
ax.plot(t, r,"magenta",lw=1)
plt.grid(True)
plt.box()
plt.axhline(color='grey')
plt.axvline(color='grey')
plt.show()

png

三叶玫瑰线

r = a\sin 3 \theta

t = np.linspace(-2*np.pi,2*np.pi, 128)
r = 2 * np.sin(3 * t)

ax = plt.subplot(111, projection='polar')
ax.plot(t, r,"red",lw=1)
plt.grid(True)
plt.box()
plt.axhline(color='grey')
plt.axvline(color='grey')
plt.show()

png

四叶玫瑰线

r = a\sin 4 \theta

t = np.linspace(-2*np.pi,2*np.pi, 128)
r = 2 * np.sin(4 * t)

ax = plt.subplot(111, projection='polar')
ax.plot(t, r,"red",lw=1)
plt.grid(True)
plt.box()
plt.axhline(color='grey')
plt.axvline(color='grey')
plt.show()

png

阿基米德螺旋线

r = a\theta

t = np.linspace(-2*np.pi,2*np.pi, 128)
r = 2 * t

ax = plt.subplot(111, projection='polar')
ax.plot(t, r,"brown",lw=1)
plt.grid(True)
plt.box()
plt.axhline(color='grey')
plt.axvline(color='grey')
plt.show()

png

伯努利双纽线

r^2 = 2a^2\cos 2\theta

t = np.linspace(-2*np.pi,2*np.pi, 128)
r = 2 * np.cos(2 * t)

ax = plt.subplot(111, projection='polar')
ax.plot(t, r,"magenta",lw=1)
plt.grid(True)
plt.box()
plt.axhline(color='grey')
plt.axvline(color='grey')
plt.show()

png

摆线

摆线方程
$$
\begin{aligned}
x =& a(t - \sin t) \
y =& a(1 - \cos t)
\end{aligned}
$$

t = np.linspace(-2*np.pi,2*np.pi, 64)
x = 2 * (t - np.sin(t))
y = 2 * (1 - np.cos(t))
plt.grid()
plt.box()
plt.ylim([-1, 3])
plt.axes().set_aspect('equal', 'datalim')
plt.axhline(color='grey')
plt.axvline(color='grey')
plt.plot(x,y,"brown",lw=1)
plt.axis('equal')
plt.show()

png

星形线

\begin{aligned}x =& r\cos^3 t \\y =& r\sin^3 t \end{aligned}
t = np.linspace(-4,4, 64)
x = 2 * np.power(np.cos(t), 3)
y = 2 * np.power(np.sin(t), 3)
plt.grid()
plt.box()
plt.ylim([-2, 2])
plt.axhline(color='grey')
plt.axvline(color='grey')
plt.axis('equal')
plt.plot(x,y,"brown",lw=1)
plt.show()

png

关于泰勒级数的理解

x = np.linspace(-3*np.pi,3*np.pi, 64)
sin = np.sin(x)
y3 = x - 1/math.factorial(3) * (x ** 3)
y5 = x - 1/math.factorial(3) * (x ** 3) + 1/math.factorial(5) * (x ** 5)

plt.plot(x,sin,"red",lw=1)
plt.plot(x,x,"blue",lw=1)  
plt.plot(x,y3,"green",lw=1)  
plt.plot(x,y5,"brown",lw=1)
plt.grid(True)
plt.box()
plt.ylim([-3, 3])
plt.xlabel('X')
plt.ylabel('Y')
plt.axhline(color='grey')
plt.axvline(color='grey')
plt.show()

png

绘制函数图像如下:

\begin{aligned} y = &\sin x \\ y = & x \\ y = & x -\frac{1}{3!}x^3 \\ y = & x -\frac{1}{3!}x^3 +\frac{1}{5!}x^5 \\ \end{aligned}

如上图所示,可以看到 \sin x 的泰勒级数展开式,每增加一项,以线性多项式函数来逼近 \sin x 就越精确,这也就是线性的泰勒级数与非线性函数相等的直观表示。

悬链线

\displaystyle y=a \cosh(\frac{x}{a})

x = np.linspace(-8, 8, 32)
y = 2 * np.cosh(x / 2)

plt.plot(x, y, "magenta",lw=1)
plt.grid(True)
plt.box()
plt.ylim([0, 10])
plt.xlabel('X')
plt.ylabel('Y')
plt.axhline(color='grey')
plt.axvline(color='grey')
plt.show()

png

悬链线看起来真像抛物线,伯努利兄弟俩对这个颇有研究

一些重要的二维函数图像

抛物柱面

y = x^2

fig = plt.figure()
ax = fig.gca(projection='3d')
x = np.linspace(-5, 5, 16)
z = np.linspace(-5, 5, 3)
X, Z = np.meshgrid(x, z)
Y = X**2

alpha = 0.5
cmap = cm.bone

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.title(r"$y = x^2$")
surf = ax.plot_wireframe(X, Y, Z, alpha=alpha, cmap=cmap)
plt.show()

png

椭球面

\begin{aligned} \frac{x^2}{a^2} + \frac{y^2}{b^2} + \frac{z^2}{c^2} = 1 \end{aligned}
fig = plt.figure()
ax = fig.gca(projection='3d')

a, b, c = 3, 4, 5

theta = np.linspace(0, 2*np.pi, 16)
phi = np.linspace(0, np.pi, 16)

X = a * np.outer(np.cos(theta), np.sin(phi))
Y = b * np.outer(np.sin(theta), np.sin(phi))
Z = c * np.outer(np.ones(np.size(theta)), np.cos(phi))

alpha = 0.5
cmap = cm.bone

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.title(r"$\frac{x^2}{a^2} + \frac{y^2}{b^2} + \frac{z^2}{c^2} = 1$")
surf = ax.plot_wireframe(X, Y, Z, alpha=alpha, cmap=cmap)
plt.show()

png

椭圆抛物面

\displaystyle \frac{x^2}{a^2} + \frac{y^2}{b^2} = \frac{z}{c}

fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.linspace(-5, 5, 10)
Y = np.linspace(-5, 5, 10)
X, Y = np.meshgrid(X, Y)
Z = X ** 2 + Y ** 2 
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.title(r"$\frac{x^2}{a^2} + \frac{y^2}{b^2} = \frac{z}{c}$")
surf = ax.plot_wireframe(X, Y, Z, cmap=cm.afmhot)
plt.show()

png

椭圆锥面

\displaystyle \frac{x^2}{a^2} + \frac{y^2}{b^2} = \frac{z^2}{c^2}

fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.linspace(-5, 5, 16)
Y = np.linspace(-5, 5, 16)
X, Y = np.meshgrid(X, Y)
Z = np.sqrt(X ** 2 + Y ** 2)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.title(r"$\frac{x^2}{a^2} + \frac{y^2}{b^2} = \frac{z^2}{c^2}$")
surf = ax.plot_wireframe(X, Y, Z, cmap=cm.afmhot)
plt.show()

png

单页双曲面

\displaystyle \frac{x^2}{a^2} + \frac{y^2}{b^2} - \frac{z^2}{c^2} = 1

我尝试了好几次,发现双曲面用 matplotlib 画出来不太友好,如果有谁知道,希望能评论告诉我。

fig = plt.figure()
ax = fig.gca(projection='3d')
x = np.linspace(-5, 5, 16)
z = np.linspace(-5, 5, 10)
X, Z = np.meshgrid(x, z)
Y = np.sqrt(1 + Z ** 2 - X ** 2)

alpha = 0.5
cmap = cm.bone

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.title(r"$\frac{x^2}{a^2} + \frac{y^2}{b^2} - \frac{z^2}{c^2} = 1$")
surf = ax.plot_wireframe(X, Y, Z, alpha=alpha, cmap=cmap)
surf = ax.plot_wireframe(X, -Y, Z, alpha=alpha, cmap=cmap)
surf = ax.plot_wireframe(Y, X, Z, alpha=alpha, cmap=cmap)
surf = ax.plot_wireframe(-Y, X, Z, alpha=alpha, cmap=cmap)
plt.show()

png

双叶双曲面

\displaystyle \frac{x^2}{a^2} - \frac{y^2}{b^2} - \frac{z^2}{c^2} = 1

fig = plt.figure()
ax = fig.gca(projection='3d')
x = np.linspace(-5, 5, 32)
z = np.linspace(-5, 5, 32)
X, Z = np.meshgrid(x, z)
Y = np.sqrt(X ** 2 - Z ** 2 - 1)

alpha = 0.5
cmap = cm.bone

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.title(r"$\frac{x^2}{a^2} - \frac{y^2}{b^2} - \frac{z^2}{c^2} = 1$")
surf = ax.plot_wireframe(X, Y, Z, alpha=alpha, cmap=cmap)
surf = ax.plot_wireframe(X, -Y, Z, alpha=alpha, cmap=cmap)
plt.show()

png

马鞍面(双曲抛物面)

\displaystyle \frac{y^2}{b^2} - \frac{x^2}{a^2} = \frac{z}{c}

fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.linspace(-5, 5, 16)
Y = np.linspace(-5, 5, 16)
X, Y = np.meshgrid(X, Y)
Z = Y ** 2 - X ** 2
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.title(r"$\frac{y^2}{b^2} -  \frac{x^2}{a^2} = \frac{z}{c}$")
surf = ax.plot_wireframe(X, Y, Z, cmap=cm.bone)
plt.show()

png

其他重要的二维函数图像

z = xy

fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.linspace(-5, 5, 16)
Y = np.linspace(-5, 5, 16)
X, Y = np.meshgrid(X, Y)
Z = X * Y
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.title(r"$z = xy$")
surf = ax.plot_wireframe(X, Y, Z, cmap=cm.Blues)
plt.show()

png

偏导数存在,不可微的例子

\begin{aligned} z = \frac{xy}{x^2 + y^2} \end{aligned}
fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.linspace(-5, 5, 32)
Y = np.linspace(-5, 5, 32)
X, Y = np.meshgrid(X, Y)
Z = (X * Y) / (X**2 + Y ** 2)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.title(r"$z = \frac{xy}{x^2 + y^2}$")
surf = ax.plot_wireframe(X, Y, Z, cmap=cm.bone)
plt.show()

png

一些二维曲线图像

r(t) = (\sin 3t)(\cos t)\overrightarrow{i} + (\sin 3t)(\sin t)\overrightarrow{j} + t\overrightarrow{k}

fig = plt.figure()
ax = fig.gca(projection='3d')

theta = np.linspace( -np.pi, np.pi, 64)
z = theta
x = np.sin(3*theta) * np.cos(theta)
y = np.sin(3*theta) * np.sin(theta)

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.title(r"$r(t) = (\sin 3t)(\cos t)\overrightarrow{i} +  (\sin 3t)(\sin t)\overrightarrow{j} + t\overrightarrow{k}$")
ax.plot(x, y, z)
plt.show()

png

r(t) = \cos t \overrightarrow{i} + \sin t \overrightarrow{j} + sin 2t \overrightarrow{k}

fig = plt.figure()
ax = fig.gca(projection='3d')

theta = np.linspace(-np.pi, np.pi, 40)
z = np.sin(2 * theta)
x = np.cos(theta)
y = np.sin(theta)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.title(r"$r(t) = \cos t \overrightarrow{i} + \sin t \overrightarrow{j} + sin 2t \overrightarrow{k}$")
ax.plot(x, y, z)
plt.show()

png

螺旋线

r(t) = \cos t \overrightarrow{i} + \sin t \overrightarrow{j} + t \overrightarrow{k}

fig = plt.figure()
ax = fig.gca(projection='3d')

theta = np.linspace(-2* np.pi, 2 * np.pi, 64)
z = theta
x =