绕X轴旋转的3D旋转体绘制问题:y=(x-2)²渲染异常
问题描述
需要绘制由曲线$y=x2$、$y=(x-2)2$与坐标轴围成区域绕X轴旋转得到的3D旋转体,编写了如下代码:
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # Define the function to rotate def f(x): return x**2 def g(x): return (x-2)**2 # Define the range of x values to plot x = np.linspace(0, 1, 100) x2=np.linspace(1, 2, 100) # Define the range of angles to rotate over theta = np.linspace(0, 2*np.pi, 100) # Create a meshgrid of x and theta values X, Theta = np.meshgrid(x, theta) X2, Theta = np.meshgrid(x2, theta) # Calculate the corresponding cylindrical coordinates R = X Y = R*np.sin(Theta) Z = R*np.cos(Theta)*f(X) R2 = X2 Y2 = R2*np.sin(Theta) Z2 = R2*np.cos(Theta)*g(X2) # Create the 3D plot fig = plt.figure(figsize = (11,8)) ax = fig.add_subplot(111, projection='3d') ax.plot_surface(X, Y, Z) ax.plot_surface(X2, Y2, Z2) ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') plt.show()
运行后发现$y=x2$对应的曲面渲染正常,但$y=(x-2)2$对应的曲面渲染异常,请问问题原因是什么?
问题原因
核心错误是绕X轴旋转的坐标转换逻辑完全错误:
- 绕X轴旋转时,原平面中的$x$坐标保持不变,原$y$值才是旋转半径$R$,而非用$x$本身当作半径。
- 代码中错误地将
R = X、R2 = X2,把x轴数值当成旋转半径,导致第二个曲面在x∈[1,2]时,错误地用12的数值作为半径,而实际应该用$(x-2)^2$(范围是10)作为旋转半径。 - 同时Z轴的计算逻辑也错误,正确的绕X轴旋转坐标转换应为:
$Y = R \cdot \sin(\theta)$,$Z = R \cdot \cos(\theta)$,其中$R$是原曲线的$y$值(即$f(X)$或$g(X2)$),而非代码里Z = R*np.cos(Theta)*f(X)这种错误的乘积形式。
比如当x=2时,$(x-2)^2=0$,旋转后应该是一个点,但代码里用x=2作为半径,会生成一个半径为2的圆,完全不符合实际旋转体的形态,最终导致曲面渲染异常。
内容的提问来源于stack exchange,提问作者AtKin
相关产品推荐
相关产品推荐

