# Contour plot of Rosenbrock function

I want to replicate with Mathematica the following plot (obtained with matplotlib.pyplot module in Python) concering the so-called Rosenbrock function

Here is my effort in Mathematica

 f[x_, y_] := (1 - x)^2 + 100 (y - x^2)^2

ContourPlot[f[x, y], {x, -0.5, 2}, {y, -1.5, 4}, Contours -> 20,
PlotLabel -> "Rosenbrock function" <> ToString[f[x, y]],


which produces

### Questions

1. How should I modify the PlotLabel in order to appear properly (In fact, is it possible the math text to be LaTeX mathematical formulas?)

2. The function values descend towards a banana-shaped valley, which itself decreases slowly towards the function’s global minimum at (1, 1). How can I make Mathematica to depict this? (Compare the two figures.)

(EDIT) I add for reference the Python script

import numpy as np
import matplotlib.pyplot as plt

#Plot of Rosenbrock's banana function: f(x,y)=(1-x)^2+100(y-x^2)^2

rosenbrockfunction = lambda x,y: (1-x)**2+100*(y-x**2)**2

n = 100 # number of discretization points along the x-axis
m = 100 # number of discretization points along the x-axis
a=-0.5; b=2. # extreme points in the x-axis
c=-1.5; d=4. # extreme points in the y-axis

X,Y = np.meshgrid(np.linspace(a,b,n), np.linspace(c,d,m))

Z = rosenbrockfunction(X,Y)

plt.contour(X,Y,Z,np.logspace(-0.5,3.5,20,base=10),cmap='gray')
plt.title(r'$$\textrm{Rosenbrock Function: } f(x,y)=(1-x)^2+100(y-x^2)^2$$')
plt.xlabel('x')
plt.ylabel('y')
plt.rc('text', usetex=True)
plt.rc('font', family='serif')

plt.show()

• Try PlotLabel -> Row@{"Rosenbrock function: ", f[x, y]}. Regarding contours: Mathematica uses equi-spaced contours according to the function value by default. In the top plot they are clearly much denser around the minimum. You need to specify explicit values in Contours. – Szabolcs Apr 11 '17 at 11:34
• To deal with the contours, the simplest way may be to apply an appropriate monotonic transformation to the function value, e.g. raise it to a small power (e.g. 0.45, just below 0.5, will produce almost equispaced contours in x-y coordinates due to the parabolic nature of the function.) Lower that power to increase the density around the minimum, increase it for the reverse effect. – Szabolcs Apr 11 '17 at 11:37
• @Szabolcs: Thanks for the comments. – Dimitris Apr 11 '17 at 13:42

How should I modify the PlotLabel in order to appear properly?

PlotLabel -> Row[{"Rosenbrock function ", f[x, y]}]


(In fact, is it possible the math text to be LaTeX mathematical formulas?)

Use my MaTeX package.

MaTeX[f[x, y]]


MaTeX["\\text{Rosebrock function: $" <> ToString@TeXForm[f[x, y]] <> "$}"]


The function values descend towards a banana-shaped valley, which itself decreases slowly towards the function’s global minimum at (1, 1). How can I make Mathematica to depict this? (Compare the two figures.)

Mathematica uses contours that correspond to equi-spaced function values. To make the contours denser (instead of less dense) towards the minimum, you either need to explicitly set a list of Contours, or use a monotonic transformation on the function values. Power functions are often useful for this. Since this function is built from squares, a power of 0.5 makes the contours appears approximately equi-spaced on the plot plane. A value smaller than this makes them denser around the minimum.

ContourPlot[
f[x, y]^0.33, {x, -0.5, 2}, {y, -1.5, 4},
Contours -> 30, PlotPoints -> 60,

• Why Mathematica does not support LaTeX math formulas? (Thanks for the nice package you have written!) Just for completeness the code for generating the contours in Python is as follows (first figure): 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') Logarithmically spaced steps from $10^.5$ to $10^3$ define the levels using the function logscale. – Dimitris Apr 11 '17 at 13:46
• @dimitris, ah, then you should do something like ContourPlot[f[x, y], {x, -0.5, 2}, {y, -1.5, 4}, Contours -> 10^Subdivide[-1/2, 7/2, 20], ContourShading -> None, PlotLabel -> Row[{"Rosenbrock function: ", TraditionalForm[f[x, y]]}], PlotRange -> All] in Mathematica. (Also, you should have added that code to your post instead of leaving it in a comment.) – J. M.'s ennui Apr 11 '17 at 16:32