Cody

Problem 481. Rosenbrock's Banana Function and its derivatives

Solution 3379068

Submitted on 25 Oct 2020
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
assert(isempty(regexp(fileread('Rosenbrock_banana.m'),'assert.m')))

2   Fail
x = [0; 0]; assert(isequal(Rosenbrock_banana(x),1))

Unrecognized function or variable 'x1'. Error in Rosenbrock_banana (line 3) gradient = [400*x1^3 -400*x1.*x2 + 2.*x1 - 2; 200*(x2-x1.^2)]; Error in Test2 (line 2) assert(isequal(Rosenbrock_banana(x),1))

3   Fail
x = [1; 1]; assert(isequal(Rosenbrock_banana(x),0))

Unrecognized function or variable 'x1'. Error in Rosenbrock_banana (line 3) gradient = [400*x1^3 -400*x1.*x2 + 2.*x1 - 2; 200*(x2-x1.^2)]; Error in Test3 (line 2) assert(isequal(Rosenbrock_banana(x),0))

4   Fail
x = [1; -1]; assert(isequal(Rosenbrock_banana(x),400))

Unrecognized function or variable 'x1'. Error in Rosenbrock_banana (line 3) gradient = [400*x1^3 -400*x1.*x2 + 2.*x1 - 2; 200*(x2-x1.^2)]; Error in Test4 (line 2) assert(isequal(Rosenbrock_banana(x),400))

5   Fail
x = [-1; 0.5]; assert(isequal(Rosenbrock_banana(x),29))

Unrecognized function or variable 'x1'. Error in Rosenbrock_banana (line 3) gradient = [400*x1^3 -400*x1.*x2 + 2.*x1 - 2; 200*(x2-x1.^2)]; Error in Test5 (line 2) assert(isequal(Rosenbrock_banana(x),29))

6   Fail
x = [0; 0]; [~,grad]=Rosenbrock_banana(x); assert(isequal(grad,[-2; 0]))

Unrecognized function or variable 'x1'. Error in Rosenbrock_banana (line 3) gradient = [400*x1^3 -400*x1.*x2 + 2.*x1 - 2; 200*(x2-x1.^2)]; Error in Test6 (line 2) [~,grad]=Rosenbrock_banana(x);

7   Fail
x = [0; 0]; [~,~,Hess]=Rosenbrock_banana(x); assert(isequal(Hess,diag([2, 200])))

Unrecognized function or variable 'x1'. Error in Rosenbrock_banana (line 3) gradient = [400*x1^3 -400*x1.*x2 + 2.*x1 - 2; 200*(x2-x1.^2)]; Error in Test7 (line 2) [~,~,Hess]=Rosenbrock_banana(x);

8   Fail
x = [1; 1]; [~,grad]=Rosenbrock_banana(x); assert(isequal(grad,[0; 0]))

Unrecognized function or variable 'x1'. Error in Rosenbrock_banana (line 3) gradient = [400*x1^3 -400*x1.*x2 + 2.*x1 - 2; 200*(x2-x1.^2)]; Error in Test8 (line 2) [~,grad]=Rosenbrock_banana(x);

9   Fail
x = [1; 1]; [~,~,Hess]=Rosenbrock_banana(x); assert(isequal(Hess,[802, -400; -400, 200]))

Unrecognized function or variable 'x1'. Error in Rosenbrock_banana (line 3) gradient = [400*x1^3 -400*x1.*x2 + 2.*x1 - 2; 200*(x2-x1.^2)]; Error in Test9 (line 2) [~,~,Hess]=Rosenbrock_banana(x);

10   Fail
x = [-1.9; 2]; correct_value = 267.6200; correct_grad = -1e3*[1.2294; 0.3220]; correct_Hess = [3534, 760; 760, 200]; [val,grad,Hess]=Rosenbrock_banana(x); assert(isequal(str2num(num2str(val)),correct_value)) assert(isequal(str2num(num2str(grad)),correct_grad)) assert(all(max(abs(Hess-correct_Hess)<1e-8)))

Unrecognized function or variable 'x1'. Error in Rosenbrock_banana (line 3) gradient = [400*x1^3 -400*x1.*x2 + 2.*x1 - 2; 200*(x2-x1.^2)]; Error in Test10 (line 5) [val,grad,Hess]=Rosenbrock_banana(x);

Suggested Problems

More from this Author17

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!