分左右子图,用Python绘制曲面z=xe⁻ˣ²⁻ʸ²在区域x, yE[-2,2]上的图形,以及在区域 x²+y²≤4上的图形
时间: 2025-01-26 16:10:14 浏览: 42
要使用Python绘制曲面 \( z = xe^{-x^2-y^2} \) 在区域 \( x, y \in [-2, 2] \) 和 \( x^2 + y^2 \leq 4 \) 上的图形,我们可以使用 `matplotlib` 和 `numpy` 库。以下是一个示例代码:
```python
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 创建网格点
x = np.linspace(-2, 2, 100)
y = np.linspace(-2, 2, 100)
X, Y = np.meshgrid(x, y)
# 计算Z值
Z = X * np.exp(-X**2 - Y**2)
# 创建图形
fig = plt.figure(figsize=(12, 5))
# 绘制在区域x, y ∈ [-2, 2]上的图形
ax1 = fig.add_subplot(121, projection='3d')
ax1.plot_surface(X, Y, Z, cmap='viridis')
ax1.set_title('曲面 z = xe⁻ˣ²⁻ʸ² 在区域 x, y ∈ [-2, 2] 上')
ax1.set_xlabel('X')
ax1.set_ylabel('Y')
ax1.set_zlabel('Z')
# 绘制在区域x² + y² ≤ 4上的图形
theta = np.linspace(0, 2 * np.pi, 100)
radius = 2
x_circle = radius * np.cos(theta)
y_circle = radius * np.sin(theta)
X_circle, Y_circle = np.meshgrid(x_circle, y_circle)
Z_circle = X_circle * np.exp(-X_circle**2 - Y_circle**2)
ax2 = fig.add_subplot(122, projection='3d')
ax2.plot_surface(X_circle, Y_circle, Z_circle, cmap='viridis')
ax2.set_title('曲面 z = xe⁻ˣ²⁻ʸ² 在区域 x² + y² ≤ 4 上')
ax2.set_xlabel('X')
ax2.set_ylabel('Y')
ax2.set_zlabel('Z')
plt.tight_layout()
plt.show()
```
这段代码首先创建一个二维网格,然后计算每个网格点的 \( z \) 值。接着,使用 `matplotlib` 的 `plot_surface` 函数绘制曲面图。代码中包含两个子图,一个显示在区域 \( x, y \in [-2, 2] \) 上的曲面,另一个显示在区域 \( x^2 + y^2 \leq 4 \) 上的曲面。
阅读全文
相关推荐



















