ex337.py
import numpy as np import matplotlib.pyplot as plt def get_next_point(a0, b0, gamma): # Get the next point $(a_1, b_1)$ starting from the current point $(a_0,b_0)$ # using exact line search. The close-form of $t_{exact}$ is # $\dfrac{a_0^2+\gamma^2 b_0^2}{a_0^2+\gamma^3 b_0^2}$. r = gamma t = (a0**2 + r**2 * b0**2) / (a0**2 + r**3 * b0**2) a1 = (1 - t)*a0 b1 = (1 - t*r)*b0 return a1, b1 def plot_quadratic_convergence(): # plot the convergence of $x$ during the minimization on a quadratic # objective function $f(x_1, x_2)=\frac{1}{2}(x_1^2+\gamma x_2^2)$, # where $\gamma=10$, starting from $x_0=(\gamma, 3)$ gamma = 10 a0 = gamma b0 = 3 num_iteration = 100 x = np.zeros((num_iteration, 1)) y = np.zeros((num_iteration, 1)) for iteration in range(num_iteration): print("({}, {})".format(a0, b0)) x[iteration] = a0 y[iteration] = b0 a1, b1 = get_next_point(a0, b0, gamma) a0, b0 = a1, b1 fig, ax = plt.subplots() ax.set_xlim([-gamma * 1, gamma * 1]) ax.set_ylim([-1 * 4, 1 * 4]) ax.plot(x, y, marker="o",markerfacecolor='red', markeredgecolor='red', markeredgewidth=2) delta = gamma * 1.0 / 100 x = np.arange(-gamma * 1, gamma * 1, delta) y = np.arange(-4.0, 4.0, delta) X, Y = np.meshgrid(x, y) Z = 0.5 * (X ** 2 + gamma * Y ** 2) levels = np.exp(np.arange(-2, 10, 0.25)) CS = ax.contour(X, Y, Z, levels=levels, vmin=0,vmax=100) ax.set_aspect(aspect='2') ax.clabel(CS, fontsize=6, inline=True) ax.set_xlabel(r'$x_1$') ax.set_ylabel(r'$x_2$') ax.set_title(r'$f(x)=\frac{1}{2}\left(x_1^2+\gamma x_2^2\right),\gamma=10,x^{0}=(\gamma, 3)$') plt.savefig('ex337.png', dpi=100) plt.show() if __name__ == "__main__": plot_quadratic_convergence()
Recent Comments