Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Solution 87669

Submitted on 10 May 2012 by Thomas Gorman

Correct

128Size
Leading solution size is 76.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test
Code Input and Output
1
Pass
 
%%
% Rosenbrock's banana function
F=@(x) 100*(x(2)-x(1).^2).^2 + (1-x(1)).^2;
gradF=@(x) [100*(4*x(1).^3-4*x(1).*x(2))+2*x(1)-2; 100*(2*x(2)-2*x(1).^2)];
x0 = [-1.9; 2.0];
x1=[
   -1.4478
    2.1184];
x2=[
    1.7064
    2.9446];
f1=6.0419;
f2=0.6068;
[xmin,fmin]=SteepestDescent(F,gradF,x0,0.01,1)
assert(norm(xmin-x1)<0.2||norm(xmin-x2)<0.2)
assert( abs(fmin-f1)<0.5|| abs(fmin-f2)<0.5) % 2 local min
ai =
    0.0029
xmin =
    1.7158
    2.9470
fmin =
    0.5133
2
Pass
 
%%
% Rosenbrock's banana function
F=@(x) 100*(x(2)-x(1).^2).^2 + (1-x(1)).^2;
gradF=@(x) [100*(4*x(1).^3-4*x(1).*x(2))+2*x(1)-2; 100*(2*x(2)-2*x(1).^2)];
x0 = [0; 0];
xcorrect=[1;1];
fcorrect=0;
[xmin,fmin]=SteepestDescent(F,gradF,x0) % 20 iterations default
assert(norm((xmin-xcorrect),inf)<1)
assert(abs(fmin-fcorrect)<0.8);
ai =
    0.0807
ai =
    0.0050
ai =
    0.0312
ai =
    0.0050
ai =
    0.0209
ai =
    0.0050
ai =
    0.0166
ai =
    0.0050
ai =
    0.0145
ai =
    0.0050
ai =
    0.0132
ai =
    0.0049
ai =
    0.0121
ai =
    0.0049
ai =
    0.0111
ai =
    0.0049
ai =
    0.0103
ai =
    0.0049
ai =
    0.0094
ai =
    0.0050
xmin =
    0.3676
    0.1351
fmin =
    0.4000
3
Pass
 
%%
% Rosenbrock's banana function
F=@(x) 100*(x(2)-x(1).^2).^2 + (1-x(1)).^2;
gradF=@(x) [100*(4*x(1).^3-4*x(1).*x(2))+2*x(1)-2; 100*(2*x(2)-2*x(1).^2)];
x0 = [1.1; 0.9];
xcorrect=[1;1];
fcorrect=0;
[xmin,fmin]=SteepestDescent(F,gradF,x0,1e-2,2000)
assert(isequal(round(xmin),xcorrect))
assert(isequal(round(fmin),fcorrect))
ai =
   8.9722e-04
ai =
   9.4208e-04
ai =
   9.8918e-04
ai =
    0.0010
ai =
    0.0011
ai =
    0.0011
ai =
    0.0012
ai =
    0.0013
ai =
    0.0013
ai =
    0.0014
ai =
    0.0015
ai =
    0.0015
ai =
    0.0016
ai =
    0.0017
ai =
    0.0018
ai =
    0.0019
ai =
    0.0020
ai =
    0.0021
ai =
    2.2428
ai =
   9.8561e-04
xmin =
    0.9982
    0.9965
fmin =
   3.1139e-06