Meshgrid and contours

A common task is a graphical representation of a scalar function over a rectangle:

Meshgrid and contours

For this, first we have to generate a grid on the rectangle [a,b] x [c,d]. This is done using the meshgrid command:

n = ... # number of discretization points along the x-axis
m = ... # number of discretization points along the x-axis 
X,Y = meshgrid(linspace(a,b,n), linspace(c,d,m))

X and Y are arrays with (n,m) shape such that Meshgrid and contours contains the coordinates of the grid point Meshgrid and contours as shown in the next figure (Figure 6.6):

Meshgrid and contours

Figure 6.6: A rectangle discretized by meshgrid

A rectangle discretized by meshgrid will be used  to visualize the behavior of an iteration. Bur first we will use it to plot level curves of a function. This is done by the command contour.

As an example we choose Rosenbrock's banana function:

Meshgrid and contours

It is used to challenge optimization methods. The function values descend towards a banana-shaped valley, which itself decreases slowly towards the function’s global minimum at (1, 1).

First we display the level curves using contour.

rosenbrockfunction = lambda x,y: (1-x)**2+100*(y-x**2)**2 
X,Y = meshgrid(linspace(-.5,2.,100), linspace(-1.5,4.,100))
Z = rosenbrockfunction(X,Y) 
contour(X,Y,Z,logspace(-0.5,3.5,20,base=10),cmap='gray') 
title('Rosenbrock Function: ')
xlabel('x')
ylabel('y')

This plots the level curve at the levels given by the fourth parameter and uses the colormap gray. Furthermore, we used logarithmically spaced steps from 100.5 to 103 using the function logscale to define the levels, as shown in the next figure.

Meshgrid and contours

Figure 6.7: A contour plot of Rosenbrock function

In the preceding example, an anonymous function indicated by the keyword lambda is used to keep the code compact. Anonymous functions are explained in section Anonymous functions - the lambda keyword in Chapter 7, Functions, Anonymous functions. If levels are not given as arguments to contour, the function chooses appropriate levels by itself .

The contourf function performs the same function as contour but fills the plot with colors according to different levels. Contour plots are ideal for visualizing the behavior of a numerical method. We illustrate this here by showing the iterations of an optimization method.

We continue the preceding example and depict the steps towards the minimum of the Rosenbrock function generated by Powell's method, [27], which we will apply to find the minimum of the Rosenbrock function:

import scipy.optimize as so
rosenbrockfunction = lambda x,y: (1-x)**2+100*(y-x**2)**2
X,Y=meshgrid(linspace(-.5,2.,100),linspace(-1.5,4.,100))
Z=rosenbrockfunction(X,Y)
cs=contour(X,Y,Z,logspace(0,3.5,7,base=10),cmap='gray')
rosen=lambda x: rosenbrockfunction(x[0],x[1])
solution, iterates = so.fmin_powell(rosen,x0=array([0,-0.7]),retall=True)
x,y=zip(*iterates)
plot(x,y,'ko') # plot black bullets
plot(x,y,'k:',linewidth=1) # plot black dotted lines
title("Steps of Powell's method to compute a  minimum")
clabel(cs)

The iterative method fmin_powell applies Powell's method to find a minimum. It is started by a given start value of x0 and reports all iterates when the option retall=True is given. After sixteen iterations, the solution x=0, y=0 was found. The iterations are depicted as bullets in the following contour plot (Figure 6.8).

Meshgrid and contours

Figure 6.8: A contour plot of Rosenbrock function with a search path of an optimization method

contour also creates a contour set object that we assigned to the variable cs. This is then used by clabel to annotate the levels of the corresponding function values, as shown in the preceding figure (Figure 6.8).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.220.92