Читать книгу Mathematical Programming for Power Systems Operation - Alejandro Garcés Ruiz - Страница 42
Example 2.7
ОглавлениеThe convergence of the algorithm can be visualized by using the module MatplotLib as follows:
import matplotlib.pyplot as plt t = 0.5 conv = [] E = [10,10] for iter in range(50): E += - t*grad_f(E) conv += [np.linalg.norm(grad_f(E))] plt.semilogy(conv) plt.grid() plt.xlabel("Iteration") plt.ylabel("|Gradient|") plt.show()
The result of the script is shown in Figure 2.7. As expected, the convergence rate is linear; that is to say, the convergence plot describes almost a line in a semi-logarithmic scale. The value of ε can be used as convergence criteria (a gradient ‖∇f‖ ≤ 10−4 can be considered as the local optimum for this problem).
Figure 2.7 Convergence of the gradient method.
Notice that addition was simplified by the statement +=. In general, an statement such as x=x+1 is equivalent to x+=1. More details about this aspect are presented in Appendix C.