# Rosenbrock’s banana function

Rosenbrock’s banana function is a famous test case for optimization software. It’s called the banana function because of its curved contours. The definition of the function is The function has a global minimum at (1, 1). If an optimization method starts at the point (-1.2, 1), it has to find its way to the other side of a flat, curved valley to find the optimal point.

Rosenbrock’s banana function is just one of the canonical test functions in the paper “Testing Unconstrained Optimization Software” by Moré, Garbow, and Hillstrom in ACM Transactions on Mathematical Software Vol. 7, No. 1, March 1981, pp 17-41. The starting point (-1.2, 1) mentioned above comes from the starting point required in that paper.

I mentioned a while back that I was trying out Python alternatives for tasks I have done in Mathematica. I tried making contour plots with Python using `matplotlib`. This was challenging to figure out. The plots did not look very good, though I imagine I could have made satisfactory plots if I had explored the options. Instead, I fired up Mathematica and produced the plot above easily using the following code.

```Banana[x_, y_] := (1 - x)^2 + 100 (y - x*x)^2
ContourPlot[Banana[x, y], {x, -1.5, 1.5}, {y, 0.7, 2.0}]
``` ## 7 thoughts on “Rosenbrock’s banana function”

1. Sergey

John, don’t give up on Python! In SAGE, that will be something like

x,y = var(‘x,y’)
banana(x,y) = (1 – x)^2 + 100*(y – x*x)^2
contour_plot(banana(x,y),(x,-1.5, 1.5), (y, 0.7, 2.0),cmap=’pink’)

2. Hector

And in matplotlib it’s something like

``` from pylab import mgrid, contourf, cm, show f = lambda x,y: (1 - x)**2 + 100*(y - x**2)**2 x,y = mgrid[-1.5:1.5:50j, 0.7:2:50j] contourf(x, y, f(x,y), cmap=cm.Purples_r) show() ```

3. Graham

+1 for Sage, and it’s even easier than Sergey makes it look; since variables get automatically recognized in a function definition, you can skip the line
``` x, y = var('x,y') ```
and go straight to
``` banana(x, y) = (1 - x)^2 + 100(y - x * x)^2 ```
The “cmap” option in contour_plot is optional as well.

4. Graham

Sorry to post again, but Sage also handles the optimization question nicely; using the “minimize” command, which can use a couple of different algorithms to find the minimum of a function:
``` sage: minimize(banana, [-1.2, 1], algorithm='powell') Optimization terminated successfully. Current function value: 0.000000 Iterations: 23 Function evaluations: 665 (1.0, 1.0) ```
If I had left the “algorithm” option blank, I think it would have used Broyden-Fletcher-Goldfarb-Shannon. This algorithm yielded a slightly different answer.

5. Art Scott

Hi John.
Jon Harrop (Flying Frog) wrote about gradient and used banana to illustrate; no plot though.

And the following defines the famous Rosenbrock “banana” function that is notoriously difficult to minimize due to its curvature around the minimum:

> let rosenbrock (xs: vector) =
let x, y = xs., xs.
pown (1.0 – x) 2 + 100.0 * pown (y – pown x 2) 2;;
val rosenbrock : vector -> floatThe minimum at (1, 1) may be found quickly and easily using the functions defined above as follows:

> let xs =
vector[0.0; 0.0]
{"ft_lang_label":"__label__en","ft_lang_prob":0.85963845,"math_prob":0.95857835,"size":3834,"snap":"2019-43-2019-47","text_gpt3_token_len":1097,"char_repetition_ratio":0.10992167,"word_repetition_ratio":0.0,"special_character_ratio":0.29029733,"punctuation_ratio":0.18160096,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.993185,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,7,null,7,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-11-18T19:03:22Z\",\"WARC-Record-ID\":\"<urn:uuid:235b5ea4-b1b6-46fb-8d5b-bedbcced0740>\",\"Content-Length\":\"50006\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:0a4a8e97-3951-495f-83ff-e15faac16b4a>\",\"WARC-Concurrent-To\":\"<urn:uuid:e9a63107-2c00-4827-89fa-16c3649c8fdf>\",\"WARC-IP-Address\":\"74.208.236.113\",\"WARC-Target-URI\":\"https://www.johndcook.com/blog/2010/07/28/rosenbrocks-banana-function/\",\"WARC-Payload-Digest\":\"sha1:AM7JKJVMYBBEAAWZXL4GJDS6K4G4Q3HH\",\"WARC-Block-Digest\":\"sha1:EEFCYEEKMYDMU4PFOMQRUOU2DWLZ4MCF\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-47/CC-MAIN-2019-47_segments_1573496669813.71_warc_CC-MAIN-20191118182116-20191118210116-00521.warc.gz\"}"}