Cody

# Problem 485. Fletcher-Reeves Conjugate Gradient Method

Solution 2576259

Submitted on 18 Jun 2020 by Rafael S.T. Vieira
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
% 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 x1 x1 f(alpha) norm(c) 0 -1.900 2.000 267.6200 1270.8691 1 -1.451 2.118 6.0229 3.6089 xmin = -1.4508 2.1177 fmin = 6.0229

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 x1 x1 f(alpha) norm(c) 0 0.000 0.000 1.0000 2.0000 2 0.297 0.050 0.6384 8.1913 xmin = 0.2967 0.0501 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 x1 x1 f(alpha) norm(c) 0 1.100 0.900 9.6200 150.0119 10 1.004 1.009 0.0000 0.0079 xmin = 1.0044 1.0088 fmin = 1.9291e-05

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 x1 x1 f(alpha) norm(c) 0 0.000 0.000 1.0000 2.0000 23 1.005 1.010 0.0000 0.0071 xmin = 1.0049 1.0097 fmin = 2.3616e-05

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 x1 x1 f(alpha) norm(c) 0 -1.900 2.000 267.6200 1270.8691 74 1.000 1.000 0.0000 0.0005 xmin = 1.0002 1.0004 fmin = 4.7202e-08

### Community Treasure Hunt

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

Start Hunting!