Professional Documents
Culture Documents
X=[X;x1];
Y=[Y;x2];
else
a=x1;
x1=x2;
fx1=fx2;
x2=(1-c)*a +c*b;
fx2=feval(f,x2);
X=[X;x1];
Y=[Y;x2];
end
fprintf('%.4e %.4e %.4e %.4e %.4e\n', x1, x2, fx1, fx2, b-a);
if (abs(b-a)<eps)
%to check if tolerance is met
fprintf('succeeded after %d steps \n', i);
X
% prints the array of x1 and x2
Y
return;
end
end
fprintf('failed requirements after %d steps \n',N);
X2
1.7129
1.3704
1.5012
1.3704
1.4204
1.4512
1.4204
1.4322
1.4395
1.4322
1.4277
1.4294
1.4304
1.4294
1.4287
1.429
1.4287
F(x1)
7.738
8.2239
7.738
7.8496
7.738
7.7148
7.7195
7.7148
7.7144
7.7143
7.7144
7.7143
7.7143
7.7143
7.7143
7.7143
7.7143
F(x2)
8.2801
7.738
7.7512
7.738
7.7148
7.7179
7.7148
7.7144
7,711
7.7144
7.7143
7.7143
7.7143
7.7143
7.7143
7.7143
7.7143
Interval size(b-a)
1.4507
0.8966
0.5541
0.34247
0.21166
0.13081
0.00808
0.00499
0.03088
0.01908
0.01795
0.00729
0.00451
0.00278
0.00172
0.00106
0.00066
Interval size
Interval size
10
11
12
13
14
15
16
17
Iteration number
Observations:
The number of iterations taken to reach optimal solution for an epsilon<0.001 is 17, which
seems to be a lot for an asymptotic problem. This method may not work with multivariable
unbound problems.
% gradient function
descent=-grad;
%descent direction
D=descent;
% stopping criteria using norm
if norm(grad)<e
b=1;
else b=0;
end
counter = 1;
Epsilon=[norm(grad);e];
falpha=@(a)25*(x1 + descent(1)*a)^2 + 20*(x2 + descent(2)*a)^2 - 2*(x1 +
descent(1)*a) - (x2 + descent(2)*a);
a0=[0];
[a,fv1] = fminunc(falpha,a0); %minimising f in terms of alpha
Alpha=a;
%Calculating updated values for x1 and x2
x=[(x1 + descent(1)*a); (x2 + descent(2)*a)];
%updating the list of x1 and x2
X=[X;x];
f = 25*x1^2 + 20*x2^2 - 2*x1 - x2;
%updating the list of objective function
Y=[Y;f];
while b < 1;
counter = counter + 1;
grad = [50*x1-2,40*x2-1];
gradf=[gradf;grad]
%updating the list of gradients
if norm(grad) < e
b=1;
break
else b=0;
end
Epsilon=[norm(grad);e];
descent=-grad
%Calculating new gradient
D=[D;descent]; %updating the list of descent directions
%User defined function
falpha = @(a) 25*(x1 + descent(1)*a)^2 + 20*(x2 + descent(2)*a)^2 - 2*(x1 +
descent(1)*a) - (x2 + descent(2)*a);
a0=[0];
%initial starting value for alpha
[a,fv1] = fminunc(falpha,a0);
Alpha=[Alpha; a];
%Updating new x1 and x2 values and appending to list
x=[(x1 + descent(1)*a);
X=[X;x];
(x2 + descent(2)*a)];
COMMAND WINDOW:
Epsilon
Alpha
153.0523
7.6418
0.0203
0.4632
0.0246
0.0231
0.0203
0.0014
0.0246
0.00000
0.0145
f
238
0.668
-0.0503
-0.0525
-0.0525
-0.0525
3.5
Change of x1
Change in x2
3
2.5
2
1.5
1
0.5
0
1
Iteration number
238
240
190
140
90
40
-10
0.668
-0.0503
-0.0525
-0.0525
-0.0525
Iteration number
Observations:
Steepest descent algorithm takes 6 iterations to reach convergence and final solution with the
descent direction orthogonally changing with every iteration. But there are better methods
such as Conjugate Gradient method as we shall see in the next problem.
% gradient function
descent=-grad;
%descent direction
D=descent;
% stopping criteria using norm
if norm(grad)<e
b=1;
else b=0;
end
counter = 1;
Epsilon=[norm(grad);e];
falpha=@(a)25*(x1 + descent(1)*a)^2 + 20*(x2 + descent(2)*a)^2 - 2*(x1 +
descent(1)*a) - (x2 + descent(2)*a);
a0=[0];
[a,fv1] = fminunc(falpha,a0); %minimising f in terms of alpha
Alpha=a;
%Calculating updated values for x1 and x2
x=[(x1 + descent(1)*a); (x2 + descent(2)*a)];
%updating the list of x1 and x2
X=[X;x];
f = 25*x1^2 + 20*x2^2 - 2*x1 - x2;
%updating the list of objective function
Y=[Y;f];
while b < 1;
counter = counter + 1;
grad = [50*x1-2,40*x2-1];
gradf=[gradf;grad]
%updating the list of gradients
if norm(grad) < e
b=1;
break
else b=0;
end
Epsilon=[norm(grad);e];
(x2 + descent(2)*a)];
f
238
0.668
-0.0525
-0.0525
-0.0525
3.5
x1 and x2 values
COMMAND WINDOW:
Epsilon
Alpha
153.0523
7.6418
.0203
0.0662
.0239
0.0025
.0201
.0000
.0241
Change in x1
Change in x2
3
2.5
2
1.5
1
0.5
0
1
Iteration number
238
0.668
-0.0525
-0.0525
-0.0525
Iteration
Observations:
We see that with conjugate gradient method the solution convergences in 5 iterations
which is only slightly quicker than steepest descent method in this case. This is because the
descent direction is updated more accurately with the calculation of the beta variable.