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

### Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

# Solution 59842

Submitted on 12 Mar 2012 by Robert Canfield

Correct

352Size
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 0.454   0.0004   0.3891
4 0.005   0.0000   0.0946
5 0.001   0.0000   0.0068
xmin =
0.9985
0.9970
fmin =
2.2080e-06
```
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.004   0.5157   1.2645
4 0.133   0.3694   4.8332
5 0.011   0.2309   6.7510
6 0.002   0.1825   0.5629
7 0.296   0.1208   4.3389
8 0.007   0.0558   5.1551
9 0.001   0.0375   0.2040
10 0.891   0.0140   2.4865
11 0.007   0.0012   1.5212
12 0.001   0.0000   0.0010
xmin =
1.0012
1.0023
fmin =
1.3469e-06
```
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.045   5.9617  14.8396
4 0.006   4.7793  24.0769
5 0.001   4.4484  32.7903
6 0.001   3.9147   1.8018
7 0.439   2.5297  11.7304
8 0.002   2.3501  15.6329
9 0.003   2.0035   2.2072
10 0.107   1.6130   8.1732
11 0.004   1.4631  10.1721
12 0.005   1.2066   2.2251
13 0.071   0.9522   5.4287
14 0.009   0.8096   7.6632
15 0.005   0.6750   1.5763
16 0.101   0.4987   4.8488
17 0.012   0.3464   7.0891
18 0.003   0.2807   0.7749
19 0.223   0.1909   4.6337
20 0.008   0.0990   5.8518
21 0.002   0.0721   0.3033
22 0.475   0.0444   3.4703
23 0.005   0.0137   3.4506
24 0.001   0.0069   0.0795
25 0.980   0.0033   1.4821
26 0.004   0.0000   0.0149
27 0.001   0.0000   0.0002
xmin =
1.0003
1.0005
fmin =
7.4849e-08
```