--- /dev/null
+function f = rosenbrock(x1, x2)
+ ##
+ ## The Rosenbrock function. See Dennis & Schnabel, Appendix B, problem #1.
+ ## (The "regular" Rosenbrock function is simply the Extended Rosenbrock
+ ## with m=1).
+ ##
+ ## This function has a minimum at x=(1,1) with f(x) == 0. The
+ ## suggested starting point is x0=(-1.2, 1).
+ ##
+ f = 100*(x2 - x1^2)^2 + (1 - x1)^2;
+end
--- /dev/null
+function f = rosenbrock1(x)
+ ##
+ ## A version of the Rosenbrock function which takes a column
+ ## 2-vector instead of two distinct arguments. See rosenbrock.m for
+ ## more information.
+ ##
+ if (length(x) == 2)
+ f = rosenbrock(x(1), x(2));
+ else
+ f = NA;
+ end
+end
--- /dev/null
+function g = rosenbrock_gradient(x1, x2)
+ ##
+ ## The gradient of the Rosenbrock function. See rosenbrock.m for more
+ ## information.
+ ##
+ f_x1 = -400*x1*(x2 - x1^2) + 2*x1 - 2;
+ f_x2 = 200*(x2 - x1^2);
+ g = [f_x1; f_x2];
+end
--- /dev/null
+function g = rosenbrock_gradient1(x)
+ ##
+ ## A version of the rosenbrock_gradient() function which takes a
+ ## column 2-vector instead of two distinct arguments. See
+ ## rosenbrock_gradient.m for more information.
+ ##
+ if (length(x) == 2)
+ g = rosenbrock_gradient(x(1), x(2));
+ else
+ g = NA;
+ end
+end
--- /dev/null
+function H = rosenbrock_hessian(x1, x2)
+ ##
+ ## The Hessian of the Rosenbrock function. See rosenbrock.m for more
+ ## information.
+ ##
+ H = zeros(2,2);
+ H(1,1) = 1200*x1^2 - 400*x2 + 2;
+ H(1,2) = -400*x1;
+ H(2,1) = -400*x1;
+ H(2,2) = 200;
+end
--- /dev/null
+function H = rosenbrock_hessian1(x)
+ ##
+ ## A version of the rosenbrock_hessian() function which takes a column
+ ## 2-vector instead of two distinct arguments. See rosenbrock_hessian.m
+ ## for more information.
+ ##
+ if (length(x) == 2)
+ H = rosenbrock_hessian(x(1), x(2));
+ else
+ H = NA;
+ end
+end
--- /dev/null
+## Test the optimal point.
+
+unit_test_equals("rosenbrock1([1;1]) == 0", ...
+ 0, ...
+ rosenbrock1([1;1]));
+
+## It should fail with the wrong number of coordinates.
+f = rosenbrock1([1]);
+unit_test_equals("rosenbrock1 fails with too few coordinates", ...
+ true, ...
+ isna(f));
+
+f = rosenbrock1([1;2;3]);
+unit_test_equals("rosenbrock1 fails with too many coordinates", ...
+ true, ...
+ isna(f));
--- /dev/null
+## The gradient should be zero at the optimal point.
+
+unit_test_equals("rosenbrock_gradient(1,1) == 0", ...
+ 0, ...
+ rosenbrock_gradient(1,1));
--- /dev/null
+## Test the optimal point.
+
+unit_test_equals("rosenbrock(1,1) == 0", ...
+ 0, ...
+ rosenbrock(1,1));