336 Appendix B
falpha2 = func_multivar(x+alpha2*search);
for i= 1:1000
if falpha1 > falpha2
a = alpha1;
alpha1 = alpha2;
falpha1 = falpha2;
alpha2 = tau*a + (1-tau)*b;
falpha2 = func_multivar(x+alpha2*search);
else
b = alpha2;
alpha2 = alpha1;
falpha2 = falpha1;
alpha1 = tau*b + (1-tau)*a;
falpha1 = func_multivar(x+alpha1*search);
end
if abs(func_multivar(x+alpha1*search)-
func_multivar(x+alpha2*search)) < epsilon
break;
end
end
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code func_multivar.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
function fx = func_multivar(x)
fx = 100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code rosenbrock.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% plots the Rosenbrock’s function
%
clear all
clc
[x1,x2] = meshgrid(-2:.03:2,-2:.03:2);
z = 100*(x2-x1.^2).^2+(1-x1).^2
surf(x1,x2,z)
shading interp
view (170,20)
xlabel('x1')
337Appendix B
ylabel('x2')
zlabel('f(x1,x2)')
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code springsystem.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
clear all
clc
zprev = inf;
i = 0;
j = 0;
for x = -1:0.01:1
i = i+1;
for y = -1:0.01:1
j = j+1;
z = 100*(sqrt(x^2+(y+1)^2)-1)^2 + 90*(sqrt(x^2+(y-
1)^2)-1)^2 -(20*x+40*y);
if z < zprev
zprev = z;
xbest = x;
ybest = y;
end
end
end
fprintf('Minimum Potential = %7.4f\n ',zprev)
fprintf('occurs at x1,x2 = %10.4f %10.4f\n',xbest,ybest)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code steep_des.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% n_of_var -> number of design variables
% x = [-1.5 1.5] -> starting value of x
% epsilon1,epsilon2 -> constants used for terminating the
% algorithm
% delx -> required for gradient computation
% falpha_prev -> function value at first/previous iteration
% deriv -> gradient vector
% search -> search direction (set to negative of gradient)
%
clear all
clc
n_of_var = 2;
x = [-3 2];
338 Appendix B
epsilon1 = 1e-6;
epsilon2 = 1e-6;
delx = 1e-3;
falpha_prev = func_multivar(x);
fprintf('Initial function value = %7.4f\n ',falpha_prev)
fprintf(' No. x-vector f(x) Deriv \n')
fprintf('__________________________________________\n')
for i = 1:3000
deriv = grad_vec(x,delx,n_of_var);
search = -deriv;
[alpha,falpha] = golden_funct1(x,search);
if abs(falpha-falpha_prev)<epsilon1 || norm(deriv)<epsilon2
break;
end
falpha_prev = falpha;
x = x + alpha*search;
fprintf('%3d %8.3f %8.3f % 8.3f %8.3f
\n',i,x,falpha,norm(deriv))
end
fprintf('__________________________________________\n')
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code grad_vec.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% compute gradient vector using central difference method
% xvec, xvec1 -> vector of design variables
% deriv(i) -> derivative w.r.t. ith variable
%
function deriv = grad_vec(x,delx,n_of_var)
xvec = x;
xvec1 = x;
for i = 1:length(x)
xvec = x;
xvec1 = x;
xvec(i) = x(i) + delx;
xvec1(i) = x(i) - delx;
deriv(i) = (func_multivar(xvec) - func_multivar(xvec1))/
(2*delx);
end
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
339Appendix B
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code contour_testproblem.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% plots contour of the test problem
% surfc -> Matlab function
clear all
clc
i = 0;
j = 0;
for x = -5:.02:5
i = i+1;
for y = -5:.02:5
j = j+1;
z(i,j) = 100*(sqrt(x^2+(y+1)^2)-1)^2 + 90*(sqrt(x^2+
(y - 1)^2)-1 )^2 -(20*x+40*y);
t1(i,j) = x;
t2(i,j) = y;
end
j = 0;
end
surfc(t1,t2,z)
shading interp
xlabel('x1')
ylabel('x2')
zlabel('f(x1,x2)')
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code newton.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% n_of_var -> number of design variables
% x = [-3 2] -> starting value of x
% epsilon1, epsilon2 -> constant used for terminating
% the algorithm
% delx -> required for gradient computation
% f_prev -> function value at first/previous iteration
% deriv -> gradient vector
% sec_deriv -> hessian matrix
%
clear all
clc
n_of_var = 2;
x = [-3 2];
epsilon1 = 1e-7;
epsilon2 = 1e-7;
delx = 1e-3;
340 Appendix B
f_prev = func_multivar(x);
fprintf('Initial function value = %7.4f\n',f_prev)
fprintf('No. x-vector f(x) Deriv \n')
fprintf('__________________________________________\n')
for i = 1:50
f_prev = func_multivar(x);
deriv = grad_vec(x,delx,n_of_var);
sec_deriv = hessian(x,delx,n_of_var);
x = (x' - inv(sec_deriv)*deriv')';
f = func_multivar(x);
if abs(f-f_prev)<epsilon1 || norm(deriv)<epsilon2
break;
end
fprintf('%3d %8.3f %8.3f % 8.3f %8.3f
\n',i,x,f,norm(deriv))
end
fprintf('%3d %8.3f %8.3f % 8.3f %8.3f \n',i,x,f,norm(deriv))
fprintf('__________________________________________\n')
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code hessian.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%compute hessian matrix
% sec_deriv -> second derivative matrix
% func_multivar() -> multivariable function
% temp -> temporary variable
% Note that n_of_var = length(x)
%
function sec_deriv = hessian(x,delx,n_of_var)
for i = 1:length(x)
for j = 1:length(x)
if i == j
temp = x;
temp(i) = x(i) + delx;
term1 = func_multivar(temp);
temp(i) = x(i) - delx;
term2 = func_multivar(temp);
term3 = func_multivar(x);
sec_deriv(i,j) = (term1-2*term3+term2)/(delx^2);
else
temp = x;
temp(i) = x(i) + delx;
temp(j) = x(j) + delx;
term1 = func_multivar(temp);
temp = x;
Get Optimization now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.