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 172752

Submitted on 5 Dec 2012 by @bmtran

Correct

340Size
Leading solution size is 106.
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]=ConjGrad(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.000 267.6200 1270.8691
      1 0.000   6.0719  14.1065
xmin =
   -1.4452
    2.1191
fmin =
    6.0719
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.2926
    0.0505];
fcorrect=0.6238;
[xmin,fmin]=ConjGrad(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.000   1.0000   2.0000
      1 0.081   0.7711   5.2020
      2 0.010   0.6238   7.5242
xmin =
    0.2926
    0.0505
fmin =
    0.6238
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]=ConjGrad(F,gradF,x0) % default 20 iterations
assert(norm(xmin-xcorrect)<0.1)
assert(abs(fmin-fcorrect)<0.01);
iter alpha f(alpha)  norm(c)
      0 0.000   9.6200 150.0119
      1 0.001   0.0006   0.4621
      2 0.001   0.0005   0.0204
      3 2.235   0.0000   0.1923
      4 0.001   0.0000   0.0099
xmin =
    0.9991
    0.9982
fmin =
   8.1952e-07
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)];
x0 = [0; 0];
xcorrect = [1;1];
fcorrect = 0;
[xmin,fmin]=ConjGrad(F,gradF,x0,0.01,100) % Convergence before 100 iterations
assert(norm(xmin-xcorrect)<0.1)
assert(abs(fmin-fcorrect)<0.01);
iter alpha f(alpha)  norm(c)
      0 0.000   1.0000   2.0000
      1 0.081   0.7711   5.2020
      2 0.010   0.6238   7.5242
      3 0.005   0.4716   9.2067
      4 0.005   0.2829   9.5195
      5 0.009   0.0315   3.2433
      6 0.002   0.0232   0.3595
      7 0.002   0.0231   0.1973
      8 0.038   0.0224   0.9459
      9 0.019   0.0117   1.9848
     10 0.010   0.0007   1.1772
     11 0.001   0.0001   0.0650
     12 0.001   0.0001   0.0096
xmin =
    1.0106
    1.0214
fmin =
   1.1304e-04
5
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];
xcorrect = [1;1];
fcorrect = 0;
[xmin,fmin]=ConjGrad(F,gradF,x0,1e-3,200)
assert(isequal(round(xmin),xcorrect))
assert(isequal(round(fmin),fcorrect))
iter alpha f(alpha)  norm(c)
      0 0.000 267.6200 1270.8691
      1 0.000   6.0719  14.1065
      2 0.001   6.0180   1.6073
      3 0.467   5.0790  28.7048
      4 0.001   4.7487  36.3853
      5 0.000   4.5322  39.3467
      6 0.000   4.3651  40.8513
      7 0.000   4.2603  41.1770
      8 0.000   4.1454  40.8985
      9 0.000   4.0649  40.3490
     10 0.000   3.9569  39.4307
     11 0.000   3.9243  39.0540
     12 0.000   3.8506  38.0580
     13 0.000   3.8031  37.3569
     14 0.000   3.7322  36.1958
     15 0.000   3.7061  35.7572
     16 0.000   3.6371  34.5433
     17 0.000   3.6165  34.1496
     18 0.000   3.5793  33.6041
     19 0.000   3.5026  32.3773
     20 0.000   3.4864  32.0627
     21 0.000   3.4583  31.6645
     22 0.000   3.4200  31.2084
     23 0.000   3.3733  30.7201
     24 0.000   3.3026  30.0428
     25 0.000   3.2724  29.8003
     26 0.000   3.2488  29.6469
     27 0.000   3.1981  29.4712
     28 0.000   3.1867  29.5107
     29 0.000   3.1661  29.5285
     30 0.000   3.1368  29.5338
     31 0.000   3.0992  29.5357
     32 0.000   3.0365  30.0690
     33 0.000   3.0296  30.2637
     34 0.000   3.0059  30.5577
     35 0.000   2.9743  30.8710
     36 0.000   2.9352  31.2116
     37 0.000   2.8889  31.5880
     38 0.000   2.8359  32.0087
     39 0.000   2.7596  33.1095
     40 0.000   2.7353  33.6059
     41 0.000   2.6831  35.3667
     42 0.000   2.6740  35.7881
     43 0.000   2.6404  36.3882
     44 0.000   2.5662  38.4548
     45 0.000   2.5525  38.9412
     46 0.000   2.4929  40.1522
     47 0.000   2.4466  40.8635
     48 0.000   2.3397  43.1902
     49 0.000   2.3092  44.2166
     50 0.000   2.2554  45.0702
     51 0.000   2.1417  47.8095
     52 0.000   2.1117  49.0280
     53 0.000   2.0518  49.9447
     54 0.000   1.9273  51.1283
     55 0.000   1.8029  52.2448
     56 0.000   1.6745  53.1668
     57 0.000   1.5354  53.6711
     58 0.000   1.3642  54.7053
     59 0.000   1.2910  54.5639
     60 0.000   1.0171  51.9367
     61 0.000   0.8405  48.8751
     62 0.000   0.6181  40.8166
     63 0.000   0.3748  19.3306
     64 0.000   0.3166   3.8108
     65 0.000   0.3143   0.6618
     66 0.000   0.3142   0.3446
     67 0.002   0.3141   0.8849
     68 0.013   0.3090   5.5826
     69 0.006   0.1355   7.3041
     70 0.002   0.0902   9.4756
     71 0.001   0.0581   9.1662
     72 0.001   0.0302   7.3843
     73 0.001   0.0073   3.8375
     74 0.001   0.0006   0.2997
     75 0.001   0.0005   0.0287
     76 0.002   0.0005   0.0277
     77 0.087   0.0005   0.2642
     78 0.014   0.0000   0.0495
     79 0.001   0.0000   0.0013
     80 0.001   0.0000   0.0005
xmin =
    1.0005
    1.0011
fmin =
   3.0282e-07