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

Correct

238Size
Leading solution size is 122.
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]=BFGS_Quasi_Newton(F,gradF,x0,0.01,1) % single steepest descent
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
iter alpha f(alpha)  norm(c)
   0 0.0000 267.6200 1270.8691
   1 0.0004   6.0204   1.6920
xmin =
          -1.4521814453125
            2.117291015625
fmin =
          6.02035111158688
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=[
    0.2927
    0.0506];
fcorrect=0.63;
[xmin,fmin]=BFGS_Quasi_Newton(F,gradF,x0,1e-2,2) % two iterations
assert(norm(xmin-xcorrect)<0.1)
assert( abs(fmin-fcorrect)<0.01)
iter alpha f(alpha)  norm(c)
   0 0.0000   1.0000   2.0000
   1 0.0806   0.7711   5.2003
   2 0.0098   0.6237   7.5488
xmin =
         0.293138805215789
        0.0507131696991079
fmin =
         0.623677791785989
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 = [0;0];
xcorrect = [1;1;];
fcorrect = 0;
[xmin,fmin]=BFGS_Quasi_Newton(F,gradF,x0)
assert(norm(xmin-xcorrect)<0.01)
assert(abs(fmin-fcorrect)<0.01);
iter alpha f(alpha)  norm(c)
   0 0.0000   1.0000   2.0000
   1 0.0806   0.7711   5.2003
   2 0.0098   0.6237   7.5488
   3 0.6744   0.4362   2.9602
   4 0.7897   0.3172   3.8160
   5 1.3353   0.2743   6.6469
   6 5.5758   0.0183   1.6494
   7 0.0966   0.0172   0.3335
   8 4.4928   0.0060   1.9915
   9 2.2007   0.0012   0.7472
  10 0.5005   0.0001   0.2988
  11 2.1621   0.0000   0.0867
  12 0.6740   0.0000   0.0012
  13 1.1440   0.0000   0.0000
xmin =
          0.99999963957031
         0.999999356221718
fmin =
      7.24057125016987e-13
4
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)];
HessF=@(x) 200*[4*x(1).^2-2*(x(2)-x(1).^2)+1/100, -2*x(1);
                -2*x(1), 1];
xcorrect = [1;1];
fcorrect = 0;
x0 = [-1.9; 2];
[xmin,fmin]=BFGS_Quasi_Newton(F,gradF,x0,1e-4)
assert(isequal(round(xmin),xcorrect))
assert(isequal(round(fmin),fcorrect))
iter alpha f(alpha)  norm(c)
   0 0.0000 267.6200 1270.8691
   1 0.0004   6.0204   1.6920
   2 0.0864   5.9008  19.6322
   3 6.2485   5.3456  36.0218
   4 3.5329   4.0383  28.6153
   5 0.2788   3.6720   3.7520
   6 1.3761   3.1057  17.4518
   7 1.7003   2.7678  20.0084
   8 0.9365   2.1467   4.6768
   9 0.5341   1.7479   7.9198
  10 1.0640   1.6516  10.5990
  11 3.6971   0.9423   6.8115
  12 0.2643   0.8467   1.8447
  13 1.8696   0.6372   5.1444
  14 2.5470   0.4593   7.2784
  15 0.4177   0.3375   1.9342
  16 1.0873   0.2375   4.1169
  17 1.6420   0.1890   6.7381
  18 1.0246   0.1027   2.8612
  19 0.6739   0.0610   2.7611
  20 1.5333   0.0472   4.7146
  21 1.7406   0.0151   2.2041
  22 0.5299   0.0053   1.1737
  23 2.1143   0.0028   1.8166
  24 1.0176   0.0004   0.2330
  25 1.1406   0.0000   0.2085
  26 1.4584   0.0000   0.0098
  27 0.8172   0.0000   0.0005
  28 1.0511   0.0000   0.0000
xmin =
          1.00000000001576
          1.00000000004913
fmin =
        3.123986307874e-20