7.4

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import griddata

def f(x, y):
return (x2 - 2*x) * np.exp(-x2 - y**2 - x*y)
x_min, x_max = -3, 3
y_min, y_max = -4, 4

num_points = 1000
x_random = np.random.uniform(x_min, x_max, num_points)
y_random = np.random.uniform(y_min, y_max, num_points)

z_random = f(x_random, y_random)

grid_x, grid_y = np.mgrid[x_min:x_max:100j, y_min:y_max:100j]

grid_z = griddata((x_random, y_random), z_random, (grid_x, grid_y), method='cubic')

plt.figure(figsize=(10, 8))
plt.contourf(grid_x, grid_y, grid_z, levels=50, cmap='viridis')
plt.scatter(x_random, y_random, c='white', s=10, label='随机散乱点')
plt.title('函数f(x, y)的插值结果')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.grid(True)
plt.show()
print("3022")

posted @ 2024-11-10 15:17  Tsuki*  阅读(3)  评论(0编辑  收藏  举报