331Appendix B
a = 40;
b = 90;
epsilon = 0.001;
delx = 0.01;
flag = 0;
fprintf(' Alpha Deriv. \n')
fprintf('-------------------------\n')
for i = 1:100
alpha = (a+b)/2;
derivative = (func(a+delx) - func(a-delx))/(2*delx);
derivative_alpha = (func(alpha+delx)-func(alpha-delx))/
(2*delx);
if (derivative*derivative_alpha) < 0
b = alpha;
flag = 1;
else
a = alpha;
end
if flag == 1
break;
end
end
for j = 1:100
fprintf(' %7.3f %8.3f \n',alpha,derivative_alpha)
derivative_a = (func(a+delx) - func(a-delx))/(2*delx);
derivative_b = (func(b+delx) - func(b-delx))/(2*delx);
alpha = b - derivative_b*(b-a)/(derivative_b-derivative_a);
derivative_alpha = (func(alpha+delx) - func(alpha-delx))/
(2*delx);
if derivative_alpha > 0
b = alpha;
else
a = alpha;
end
if abs(derivative_alpha) < epsilon
break;
end
end
fprintf('-------------------------\n')
fprintf('x* = %7.3f Minimum = %8.3f\n',alpha,func(alpha))
fprintf('Number of function calls = %3d\n',4*i+6*j)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
332 Appendix B
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code cubic.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% a -> lower bound of the design variable
% b -> upper bound of the design variable
% alpha -> midpoint of a and b
% delx -> ?x for central difference method
% derivative -> derivative using central difference method
% derivative_alpha -> derivative at x = alpha
% abs -> absolute of a number, MATLAB function
% flag -> set the flag when minimum is bracketed
% derivative_a -> derivative at point a
% derivative_b -> derivative at b
%
a = 40;
b = 90;
delx = 0.01;
flag = 0;
epsilon= 0.001;
fprintf(' a b \n')
fprintf('-------------------------\n')
for i = 1:100
alpha = (a+b)/2;
derivative = (func(a+delx) - func(a-delx))/(2*delx);
derivative_alpha = (func(alpha+delx)-func(alpha-delx))/
(2*delx);
if (derivative*derivative_alpha) < 0
b = alpha;
flag = 1;
else
a = alpha;
end
if flag == 1
break;
end
end
for j = 1:100
fprintf(' %7.3f %8.3f \n',a,b)
derivative_a = (func(a+delx) - func(a-delx))/(2*delx);
derivative_b = (func(b+delx) - func(b-delx))/(2*delx);
z = 3*(func(a)-func(b))/(b-a) + derivative_a + derivative_b;
w = ((b-a)/abs(b-a))*sqrt(z*z-derivative_a*derivative_b);
mew = (derivative_b+w-z)/(derivative_b-derivative_a+2*w);
if mew <= 1
x_opt = b - mew*(b-a);
else
x_opt = a;
end
333Appendix B
alpha1 = (func(x_opt+delx) - func(x_opt-delx) )/(2*delx);
if abs(alpha1) < epsilon
break;
else
if (derivative_a*alpha1) < 0
b = x_opt;
else
a = x_opt;
end
end
end
fprintf('-------------------------\n')
fprintf('x* = %7.3f Minimum = %8.3f\n',x_opt,func
(x_opt))
fprintf('Number of function calls = %3d\n',4*i+8*j)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code golden.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% a -> lower bound of the design variable
% b -> upper bound of the design variable
% alpha -> midpoint of a and b
% falpha1 -> function value at x = alpha1
% falpha2 -> function value at x = alpha2
% epsilon -> constant used to terminate the algorithm
% abs -> absolute of a number, MATLAB function
% tau -> 2-golden number
%
clear all
clc
a = 40;
b = 90;
epsilon = 0.00001;
tau = 0.381967;
alpha1 = a*(1-tau) + b*tau;
alpha2 = a*tau + b*(1-tau);
falpha1 = func(alpha1);
falpha2 = func(alpha2);
fprintf(' a b \n')
fprintf('-------------------------\n')
for i = 1:100
fprintf(' %7.3f %8.3f \n',a,b)
if falpha1 > falpha2
a = alpha1;
alpha1 = alpha2;
falpha1 = falpha2;
alpha2 = tau*a + (1-tau)*b;
falpha2 = func(alpha2);
else
b = alpha2;
334 Appendix B
alpha2 = alpha1;
falpha2 = falpha1;
alpha1 = tau*b + (1-tau)*a;
falpha1 = func(alpha1);
end
if abs(func(alpha1)-func(alpha2)) < epsilon
break;
end
end
fprintf('-------------------------\n')
fprintf('x* = %7.3f Minimum = %8.3f\n',alpha1,func(alpha1))
fprintf('Number of function calls = %3d\n',2+i)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code func.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% objective function to be coded here
% different test functions
%
function fx = func(x)
% fx = 204165.5/(330-2*x) + 10400/(x-20);
% fx = 3*x^4+(x-1)^2;
% fx = -4*x*sin(x);
% fx = 2*(x-3)^2+exp(0.5*x*x);
fx = 3*(x)^2+12/(x^3)-5;
% fx = 2*x*x+16/x;
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
335Appendix B
Chapter 3
Code Name Details
golden_funct1.m Golden section method for a multivariable function
func_multivar.m Objective function to be coded here
rosenbrock.m Plot of Rosenbrock function
springsystem.m Finds minimum of the spring system problem
steep_des.m Steepest descent method
grad_vec.m Gradient vector computation
contour_testproblem.m Plots contour of the test problem function
newton.m Newton’s method
hessian.m Computes Hessian matrix
modied_newton.m Modied Newton’s method
levenbergmarquardt.m Levenberg–Marquardt’s method
conjugate.m Conjugate gradient method
DFP.m Davidon–Fletcher–Powell (DFP) method
BFGS.m Broyden–Fletcher–Goldfarb–Shanno (BFGS) method
powell.m Powell’s conjugate direction method
neldermead.m Nelder–Mead algorithm
\Robotics\ Directory containing codes for problems in robotics
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code golden_funct1.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% a -> lower bound of the design variable
% b -> upper bound of the design variable
% falpha1 -> function value at x = alpha1
% falpha2 -> function value at x = alpha2
% epsilon -> constant used to terminate the algorithm
% abs -> absolute of a number, MATLAB function
% tau -> 2-golden number
% func_multivar -> returns the value of a multivariable
% function
%
function [alpha1,falpha1] = golden_funct1(x,search)
a = -5;
b = 5;
tau = 0.381967;
epsilon = 1e-5;
alpha1 = a*(1-tau) + b*tau;
alpha2 = a*tau + b*(1-tau);
falpha1 = func_multivar(x+alpha1*search);

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.