Page MenuHomec4science

plot.py
No OneTemporary

File Metadata

Created
Mon, May 20, 03:21
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
def plotminimizer(f,points,method):
"""
Plot the function f and the successive approximations of the minimizer
Parameters
----------
f : scalar function of a vector of 2 variables.
points : numpy ndarray
2-D array of shape (number of iterations + 1, 2) containing the successive approximations
for the solution obtained with the iterations
method : strig
e.g. "CG" for Conjigate gradient, "BFGS" for quasi-Newton method of Broyden,
Fletcher, Goldfarb, and Shanno
"""
fig = plt.figure(1)
ax = fig.gca(projection='3d')
# Remove grid lines (dotted lines inside plot)
ax.grid(False)
# evaluating the functions at the points
fp = np.zeros(points.shape[0])
i = 0
for p in points:
fp[i] = f(p)
i = i + 1
# plotting points and lines for each point pair
ax.plot(points[:,0], points[:,1], fp, 'ro--', linewidth=2, markersize=5)
# Plot the surface
X = np.linspace(-3, 3, 50)
Y = np.linspace(-3, 3, 50)
X, Y = np.meshgrid(X, Y)
Z = np.zeros((X.shape[0], X.shape[1]))
for i in range(X.shape[0]):
for j in range(X.shape[1]):
Z[i, j] = f(np.array([X[i, j], Y[i, j]]))
ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,linewidth=0, antialiased=False, alpha=0.2)
# Plot the contour lines
ax.contour(X, Y, Z, colors="black")
# Add title and axis names
plt.title('Convergence of '+method+' method')
plt.xlabel('x')
plt.ylabel('y')
plt.show()

Event Timeline