346 Appendix B
% delx -> required for gradient computation
% falpha_prev -> function value at first/previous iteration
% deriv -> gradient vector
% deltag -> difference in gradient vector (over previous
% iteration)
% A -> approximation of the hessian matrix
% search -> search direction
%
clear all
clc
n_of_var = 2;
x = [-3 2];
delx = 0.001;
A = eye(length(x));
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:50
if i==1
deriv_prev = grad_vec(x,delx,n_of_var);
search = -deriv_prev;
[alpha,falpha] = golden_funct1(x,search);
if abs(falpha-falpha_prev)<0.001
break;
end
falpha_prev = falpha;
x = x + alpha*search;
fpri ntf('%3d %8.3f %8.3f % 8.3f %8.3f \n',i,x,falpha_
prev,norm(deriv_prev))
else
deltax = (alpha*search);
if i>2
deltax = deltax';
search = search';
end
deriv = grad_vec(x,delx,n_of_var);
deltag = deriv-deriv_prev;
term1 = (deltag'*deltag)/(deltag*deltax');
term2 = (deriv_prev'*deriv_prev)/(deriv_prev*search');
A = A + term1 + term2;
search = -inv(A)*deriv';
[alpha,falpha] = golden_funct1(x,search');
fpri ntf('%3d %8.3f %8.3f % 8.3f %8.3f \n',i,x+alpha*search',
falpha,norm(deriv))
if ab s(falpha-falpha_prev)<epsilon1 ||
norm(deriv)<epsilon2
347Appendix B
break;
end
falpha_prev = falpha;
deriv_prev = deriv;
x = x+alpha*search';
end
end
fprintf('__________________________________________\n')
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code powell.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% n_of_var -> number of design variables
% x = [-3 2] -> starting value of x
% epsilon -> constant used for terminating the algorithm
% term -> linearly independent search directions
% falpha_prev -> function value at first/previous iteration
% search -> search direction
%
clear all
clc
n_of_var = 2;
x = [-3 2];
epsilon = 1e-6;
falpha_prev = func_multivar(x);
fprintf('Initial function value = %7.4f\n ',falpha_prev)
fprintf(' No. x-vector f(x) \n')
fprintf('__________________________________________\n')
for i = 1:n_of_var
for j = 1:n_of_var+1
if (i==j)
term(i,j)=1;
else
term(i,j) = 0;
end
end
end
for i = 1: n_of_var
search{i} = (term(:,i))';
[alpha,falpha] = golden_funct1(x,search{i});
x = x + alpha*search{i};
end
search{i+1} = (term(:,i+1))';
for k = 1:200
xini = x;
i = 1;
while i<n_of_var+1
348 Appendix B
[alpha,falpha] = golden_funct1(x,search{i});
x = x + alpha*search{i};
i = i+1;
end
if abs(falpha-falpha_prev) < epsilon
break;
end
search{i} = (x-xini);
[alpha,falpha] = golden_funct1(x,search{i});
x = x + alpha*search{i};
temp = search;
for i = 1:n_of_var
search{i} = temp{i+1};
end
falpha_prev = falpha;
fprintf('%3d %8.3f %8.3f % 8.3f \n',k,x,falpha)
end
fprintf('__________________________________________\n')
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code neldermead.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% n_of_var -> number of design variables
% lb, ub -> lower/upper bound of variable
% (optional for generating initial feasible points randomly)
% ybest -> best value of the objective function in theiteration
% ysecondbest -> second best value of the objective function
% yworst -> worst value of the objective function in the
% iteration
% xworst -> corresponding value of the variable for yworst
% xc -> centroid of the polygon
% fcentroid -> function value at xc
% deviation -> sum square deviation of function values from
% centroid
% xr => reflected point
% freflec => function value at reflected point
% xe => expanded point
% fexp => function value at expanded point
% xcont => contracted point
% fcont => function value at contracted point
%
clear all
clc
n_of_var = 2;
epsilon = 1e-4;
alpha = 1;
gamma = 2;
349Appendix B
rho = -0.5;
lb = [-1 -1];
ub = [1 1];
fprintf(' Iteration Deviation f(x) \n')
fprintf('__________________________________________\n')
for JJ = 1:50
for i = 1:length(lb)
for j = 1:n_of_var+1
a(i,j) = lb(i) + (ub(i)-lb(i))*rand;
end
end
if JJ~=1
a = x';
end
for i = 1:n_of_var+1
for j = 1:n_of_var
x(i,j) = a(j,i);
end
fval(i) = func_multivar(x(i,:));
end
[yworst,I] = max(fval);
[ybest,II] = min(fval);
% compute centroid
for i = 1:length(lb)
sum(i) = 0;
for j = 1:n_of_var+1
if (j ~= I)
sum(i) = sum(i) + a(i,j);
else
xworst(:,i) = a(i,j);
end
end
end
xc = sum./n_of_var;
fcentroid = func_multivar(xc);
sum1 = 0;
for i = 1:n_of_var+1
sum1 = sum1 + (fcentroid-fval(i))^2;
end
deviation = sqrt(sum1/(n_of_var+1));
if deviation < epsilon
break;
end
fval(I) = [];
[ysecondworst,Isw] = max(fval);
xr = xc + alpha*(xc-xworst);
freflec = func_multivar(xr);
if freflec < ybest
%expansion
350 Appendix B
xe = xc + gamma*(xc-xworst);
fexp = func_multivar(xe);
if fexp < freflec
x(I,:) = xe;
else
x(I,:) = xr;
end
else
if freflec < ysecondworst
x(I,:) = xr;
else
xcont = xc + rho*(xc-xworst);
fcont = func_multivar(xcont);
if fcont < yworst
x(I,:) = xcont;
end
end
end
fprintf('%3d %15.4f %15.3f \n',JJ,deviation,ybest)
end
fprintf('__________________________________________\n')
xc
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code robotics_nominal_traj.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Generates nominal trajectory for the robotics arm problem
clear all
clc
% generate 100 points in t
t = -pi:.063:pi
px = 30*cos(t);
py = 100*sin(t);
pz = 10*t + 66.04;
plot3(px,py,pz,'b-','LineWidth',3)
xlabel('px')
ylabel('py')
zlabel('pz')
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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.