求解二元函数最小值 发表于 2019-10-16 | 分类于 ai 12345678910111213141516171819202122232425262728import numpy as npimport matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3Ddef f_2d(x, y): return x ** 2 + 3 * x + y ** 2 + 8 * y + 1def df_2d(x, y): return 2 * x + 3, 2 * y + 8x, y = 4, 4learning_rate = 0.01for itr in range(200): v_x, v_y = df_2d(x, y) x, y = x - learning_rate * v_x, y - learning_rate * v_y print(x, y, f_2d(x, y))x = np.linspace(-10, 10)y = np.linspace(-10, 10)X, Y = np.meshgrid(x, y)fig = plt.figure()ax = fig.gca(projection='3d')surf = ax.plot_surface(X, Y, f_2d(X, Y))plt.show()