目前只看了p1-高等数学线代的视频和教案,对矩阵的几何意义做了一些回顾。
作业:
- 为不同的a,b取值,绘制该函数的3D表面。请问 a,b取值对该表面形状有大的影响吗?
用ipywidgets 快速设定a,b,x1,x2的取值或显示区间,在b=0时 函数为凸函数,在b非0时非凸函数
b<0 | b = 0 | b>0 | |
|
2. 编写一个算法来找到它的全局最小值及相应的最小解,并在3D图中标出。分析一下你的算法时空效率、给出运行时间。
采用梯度下降法:
import numpy as np
import matplotlib.pyplot as plt
def rosenbrock(X, a=1, b=100):
x, y = X
return (a - x)**2 + b * (y - x**2)**2
def rosenbrock_grad(X, a=1, b=100):
x, y = X
return np.array([
2 * (x - a) - 4 * b * x * (y - x**2),
2 * b * (y - x**2)
])
def gradient_descent(J_grad, x_init, alpha=0.01, epsilon=1e-6, max_iterations=1000):
x = x_init
for i in range(max_iterations):
x = x - alpha * J_grad(x)
if np.linalg.norm(J_grad(x)) < epsilon:
return x, i + 1
return x, max_iterations
# plot the function
x1 = np.arange(-2, 2,0.01)
x2 = np.arange(-2,2,0.01)
x1, x2 = np.meshgrid(x1, x2)
z = f(x1,x2)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(x1, x2, z, cmap='hot',alpha=0.3) # 生成表面, alpha 用于控制透明度
#cset = ax.contourf(x1, x2, z, zdir='z', offset=-100, cmap='coolwarm')
#cset = ax.contourf(x1, x2, z, zdir='x', offset=-40, cmap='coolwarm')
#cset = ax.contourf(x1, x2, z, zdir='y', offset=40, cmap='coolwarm')
#ax4.scatter(min_x1,min_x2,min_z,c='r')
# 设定显示范围
ax.set_xlabel('x1')
ax.set_ylabel('x2')
ax.set_zlabel('f')
#ax.view_init(0, 0)
ax.view_init(30, 45)
start_time = time.time()
x_init = np.zeros(2)
x_min, it = gradient_descent(rosenbrock_grad, x_init, alpha=0.001, max_iterations=100000)
print('x* =', x_min)
print('Rosenbrock(x*) =', rosenbrock(x_min))
print('Grad Rosenbrock(x*) =', rosenbrock_grad(x_min))
print('Iterations =', it)
end_time = time.time()
running_time = end_time-start_time
print("优化的时间:%.2f秒!" % running_time)
ax.scatter(x_min[0],x_min[1],rosenbrock(x_min),c='r')
plt.show()
输出: