之前已经有发过求解一元一次的最小值。先理解好那个,才好理解这个。梯度下降不难的。
方程 x**2 + y**2 + 4
import numpy as np import matplotlib.pyplot as plt # 定义二元一次方程 def function(x, y): return x**2 + y**2 + 4 # 梯度函数 def gradient(x, y): return 2 * x, 2 * y # 梯度下降法 def gradient_descent(learning_rate, max_iterations): point = [8, 8] # 初始点 trajectory = [point] for _ in range(max_iterations): grad = gradient(*point) point = [point[i] - learning_rate * grad[i] for i in range(len(point))] trajectory.append(point) min_point = trajectory[-1] min_value = function(*min_point) print(f"最小点:({min_point[0]}, {min_point[1]})") print(f"最小值:{min_value}") return np.array(trajectory) # 设置参数 learning_rate = 0.1 max_iterations = 100 # 运行梯度下降法 trajectory = gradient_descent(learning_rate, max_iterations) # 可视化结果 fig = plt.figure() ax = fig.add_subplot(111, projection='3d') x = np.linspace(-10, 10, 100) y = np.linspace(-10, 10, 100) X, Y = np.meshgrid(x, y) Z = function(X, Y) ax.plot_surface(X, Y, Z, cmap='viridis', alpha=0.8) ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Function Value') # 绘制梯度下降轨迹 trajectory = np.array(trajectory) ax.scatter(trajectory[:, 0], trajectory[:, 1], function(trajectory[:, 0], trajectory[:, 1]), color='r') plt.show()
输出数值结果:
输出可视化结果: