提问人:Freya the Goddess 提问时间:1/2/2023 更新时间:1/2/2023 访问量:58
检查我的有界区域图及其朝 x 轴和朝向 y 轴的公转是否正确
Check Whether my Plot of Bounded Region and Its Revolution toward x-axis and toward y-axis are correct
问:
我尝试了这段代码,以便能够在 y=6x 和 y= 6x^ 之间绘制一个有界区域{2}
请检查它是否正确...
我希望有界区域围绕 x 轴和 y 轴旋转,成为旋转的固体。
我想添加一个图例,这样人们就会知道蓝线是 y=6x,橙线是 y=6x^{2}
这是我的 MWE:
# Compare the plot at xy axis with the solid of revolution toward x and y axis
# For region bounded by the line y = 6x and y = 6x^2
import matplotlib.pyplot as plt
import numpy as np
n = 100
fig = plt.figure(figsize=(14, 7))
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222, projection='3d')
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224, projection='3d')
y = np.linspace(0, 10, n)
x1 = (y/6)
x2 = (y/6) ** (1/2)
t = np.linspace(0, np.pi * 2, n)
u = np.linspace(-1, 2, 60)
v = np.linspace(0, 2*np.pi, 60)
U, V = np.meshgrid(u, v)
X = U
Y1 = (6*U**2)*np.cos(V)
Z1 = (6*U**2)*np.sin(V)
Y2 = (6*U)*np.cos(V)
Z2 = (6*U)*np.sin(V)
Y3 = ((1/6)*U**(1/2))*np.cos(V)
Z3 = ((1/6)*U**(1/2))*np.sin(V)
Y4 = (U/6)*np.cos(V)
Z4 = (U/6)*np.sin(V)
xn = np.outer(x1, np.cos(t))
yn = np.outer(x1, np.sin(t))
zn = np.zeros_like(xn)
for i in range(len(x1)):
zn[i:i + 1, :] = np.full_like(zn[0, :], y[i])
ax1.plot(x1, y, x2, y)
ax1.set_title("$f(x)$")
ax2.plot_surface(X, Y3, Z3, alpha=0.3, color='red', rstride=6, cstride=12)
ax2.plot_surface(X, Y4, Z4, alpha=0.3, color='blue', rstride=6, cstride=12)
ax2.set_title("$f(x)$: Revolution around $y$")
# find the inverse of the function
x_inverse = y
y1_inverse = np.power(x_inverse, 1)
y2_inverse = np.power(x_inverse, 1 / 2)
ax3.plot(x_inverse, y1_inverse, x_inverse, y2_inverse)
ax3.set_title("Inverse of $f(x)$")
ax4.plot_surface(X, Y1, Z1, alpha=0.3, color='red', rstride=6, cstride=12)
ax4.plot_surface(X, Y2, Z2, alpha=0.3, color='blue', rstride=6, cstride=12)
ax4.set_title("$f(x)$: Revolution around $x$")
plt.tight_layout()
plt.show()
答:
2赞
コリン
1/2/2023
#1
左下角的蓝线是 y = x
,而不是 y = 6x
的倒数......
我把它改成了y = 1/6x
。
可以通过限制代码中 、 和 值来绘制有界区域的公转。您可以通过查找两个函数的交点来确定限制。np.linspace
y
u
x_inverse
在左上图中,它们在 和 相交。(0, 0)
(1, 6)
在左下角的图中,它们在 和 相交。(0, 0)
(6, 1)
还添加了图例。请参阅下面的完整代码。
我更改了什么:
n = 200
y = np.linspace(0, 6, n)
u = np.linspace(0, 1, n)
v = np.linspace(0, 2*np.pi, n)
x_inverse = np.linspace(0, 6, n)
y1_inverse = np.power(x_inverse/6, 1)
y2_inverse = np.power(x_inverse/6, 1 / 2)
完整代码:
# Compare the plot at xy axis with the solid of revolution toward x and y axis
# For region bounded by the line y = 6x and y = 6x^2
import matplotlib.pyplot as plt
import numpy as np
n = 200
fig = plt.figure(figsize=(14, 7))
ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222, projection='3d')
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224, projection='3d')
y = np.linspace(0, 6, n)
x1 = (y / 6)
x2 = (y / 6) ** (1 / 2)
t = np.linspace(0, np.pi * 2, n)
u = np.linspace(0, 1, n)
v = np.linspace(0, 2 * np.pi, n)
U, V = np.meshgrid(u, v)
X = U
Y1 = (6 * U ** 2) * np.cos(V)
Z1 = (6 * U ** 2) * np.sin(V)
Y2 = (6 * U) * np.cos(V)
Z2 = (6 * U) * np.sin(V)
Y3 = ((1 / 6) * U ** (1 / 2)) * np.cos(V)
Z3 = ((1 / 6) * U ** (1 / 2)) * np.sin(V)
Y4 = (U / 6) * np.cos(V)
Z4 = (U / 6) * np.sin(V)
xn = np.outer(x1, np.cos(t))
yn = np.outer(x1, np.sin(t))
zn = np.zeros_like(xn)
for i in range(len(x1)):
zn[i:i + 1, :] = np.full_like(zn[0, :], y[i])
ax1.plot(x1, y, label='$y=6x$')
ax1.plot(x2, y, label='$y=6x^{2}$')
ax1.legend()
ax1.set_title('$y=6x$ and $y=6x^{2}$')
ax2.plot_surface(X, Y3, Z3, alpha=0.3, color='red', rstride=6, cstride=12)
ax2.plot_surface(X, Y4, Z4, alpha=0.3, color='blue', rstride=6, cstride=12)
ax2.set_title("$f(x)$: Revolution around $y$")
# find the inverse of the function
x_inverse = np.linspace(0, 6, n)
y1_inverse = np.power(x_inverse/6, 1)
y2_inverse = np.power(x_inverse/6, 1 / 2)
ax3.plot(x_inverse, y1_inverse, label='Inverse of $y=6x$')
ax3.plot(x_inverse, y2_inverse, label='Inverse of $y=6x^{2}$')
ax3.set_title('Inverse of $y=6x$ and Inverse of $y=6x^{2}$')
ax3.legend()
ax4.plot_surface(X, Y1, Z1, alpha=0.3, color='red', rstride=6, cstride=12)
ax4.plot_surface(X, Y2, Z2, alpha=0.3, color='blue', rstride=6, cstride=12)
ax4.set_title("$f(x)$: Revolution around $x$")
plt.tight_layout()
plt.show()
评论
0赞
1/3/2023
完全。干得好。投票
评论