376 Appendix B
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code func_val1.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% computes function value
%
function y = func_val1(x)
y = (x(1)-1)^2 + (x(2)-2)^2;
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code eqconstr_val.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% computes value of equality constraint
%
function h = eqconstr_val(x)
h(1) = 2*x(1)-x(2);
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code ineqconstr_val.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% computes value of inequality constraint
%
function g = ineqconstr_val(x)
g(1) = x(1)-5;
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code grad_vec_f.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% computes gradient vector (obj. function)
%
function deriv = grad_vec_f(x,delx,n_of_var,scale_factor)
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_val1(xvec) - func_val1(xvec1))/(2*delx);
end
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
377Appendix B
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code grad_vec_eqcon.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% computes gradient vector (eq. constraint)
%
function deriv = grad_vec_eqcon(x,delx,n_of_eqcons)
xvec = x;
xvec1 = x;
for j = 1:n_of_eqcons
for i = 1:length(x)
xvec = x;
xvec1 = x;
xvec(i) = x(i) + delx;
xvec1(i) = x(i) - delx;
h = eqconstr_val(xvec);
h1 = eqconstr_val(xvec1);
deriv(j,i) = (h(j) - h1(j))/(2*delx);
end
end
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code grad_vec_ineqcon.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% computes gradient vector (ineq. constraint)
%
function deriv = grad_vec_ineqcon(x,delx,n_of_iqcons)
xvec = x;
xvec1 = x;
for j = 1:n_of_iqcons
for i = 1:length(x)
xvec = x;
xvec1 = x;
xvec(i) = x(i) + delx;
xvec1(i) = x(i) - delx;
g = ineqconstr_val(xvec);
g1 = ineqconstr_val(xvec1);
deriv(j,i) = (g(j) - g1(j))/(2*delx);
end
end
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
378 Appendix B
Chapter 7
Code Name Details
sqp.m (main program) Sequential quadratic programming (SQP) method modied for
weighted sum approach
func_val.m (function) Computes augmented Lagrangian function value
func_val1.m (function) Computes function value
sqp.m (main program) SQP method modied for solving multiobjective problems using
ε-constraint technique
func_val.m (function) Computes augmented Lagrangian function value
func_val1.m (function) Computes function value
ineqconstr_val.m Computes inequality constraint value
pso.m (main program) Particle swarm optimization (PSO) method with dynamic
weights
func1.m (function) Computes value of objective function
sqp.m (main program) Main program for solving reentry problem
dynamics.m (function) Computes area, volume, and X
cp
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code sqp.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% n_of_var -> number of design variables
% x = [0.1 0.1] -> starting value of x
% epsilon1 -> constants used for terminating the algorithm
% delx -> required for gradient computation
% falpha_prev -> function value at first/previous iteration
% deriv -> gradient vector
% quadprog -> MATLAB function to solve quadratic programming
% LAMBDA -> Lagrange multipliers
clear all
clc
warning off
n_of_var = 2;
n_of_eqcons = 1;
n_of_iqcons = 1;
scale_factor = 1;
global LAMBDA RK BETA EQCONSTR ICONSTR FVALUE W1 W2
LAMBDA = zeros(1,n_of_eqcons);
BETA = zeros(1,n_of_iqcons);
X = [0.1 0.1];
RK = 1;
A = eye(length(X));
epsilon1 = 1e-6;
delx = 1e-3;
for kk = 1:101
X = [0.1 0.1];
W1 = (kk-1)/100;
W2 = 1 - W1;
379Appendix B
checkconstr = zeros(1,n_of_iqcons);
fprintf(' No. x-vector f(x) |Cons.| \n')
fprintf('___________________________________________________\n')
checkconstr = zeros(1,n_of_iqcons);
for i = 1:10
deriv_f = grad_vec_f(X,delx,n_of_var,scale_factor);
sec_deriv_f = hessian(X,delx);
deriv_eqcon = grad_vec_eqcon(X,delx,n_of_eqcons);
deriv_ineqcon = grad_vec_ineqcon(X,delx,n_of_iqcons);
options = optimset('Display','off');
x = quadprog(sec_deriv_f,deriv_f,deriv_ineqcon,-ineqconstr_val(X),
deriv_eqcon,-eqconstr_val(X),[],[],X,options);
fprev = func_val(X);
X = X+x';
yyy = func_val(X);
LAMBDA = LAMBDA + 2*RK*EQCONSTR;
BETA = BETA + 2*RK*(max([ICONSTR; -BETA./(2*RK)]));
fnew = func_val(X);
checkconstr1 = max([ineqconstr_val(X);checkconstr]);
disp([i X FVALUE norm([checkconstr1 eqconstr_val(X)])]);
if abs(fnew-fprev) < epsilon1
break
end
end
fprintf('___________________________________________________\n')
plot(0.5*(X(1)^2+X(2)^2) , 0.5*((X(1)-1)^2 + (X(2)-3)^2),'r*')
hold on
end
xlabel('f1');
ylabel('f2');
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code func_val.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% computes augmented Lagrangian function value
%
function y = func_val(x)
global LAMBDA RK BETA EQCONSTR ICONSTR FVALUE W1 W2
y = W1*0.5*(x(1)^2+x(2)^2) + W2*0.5*((x(1)-1)^2 + (x(2)-3)^2);
g = ineqconstr_val(x);
h = eqconstr_val(x);
EQCONSTR = h;
ICONSTR = g;
FVALUE = y;
y = y + LAMBDA*EQCONSTR' + RK*EQCONSTR*EQCONSTR' + sum(BETA.*
max([ICONSTR; -BETA./(2*RK)])) + sum(RK*(max([ICONSTR;
-BETA./(2*RK)])).^2);
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
380 Appendix B
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code func_val1.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% computes function value
%
function y = func_val1(x)
global W1 W2
y = W1*0.5*(x(1)^2+x(2)^2) + W2*0.5*((x(1)-1)^2 + (x(2)-3)^2);
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code sqp.m modified for eps-constraints method
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% n_of_var -> number of design variables
% x = [-1.5 1.5] -> starting value of x
% epsilon1 -> constants used for terminating the algorithm
% delx -> required for gradient computation
% falpha_prev -> function value at first/previous iteration
% deriv -> gradient vector
% quadprog -> MATLAB function to solve quadratic programming
% LAMBDA -> Lagrange multipliers
clear all
clc
warning off
n_of_var = 1;
n_of_eqcons = 1;
n_of_iqcons = 5;
scale_factor = 1;
global LAMBDA RK BETA EQCONSTR ICONSTR FVALUE W1 W2
LAMBDA = zeros(1,n_of_eqcons);
BETA = zeros(1,n_of_iqcons);
X = [1 1];
RK = 1;
A = eye(length(X));
epsilon1 = 1e-6;
delx = 1e-3;
for kk = 1:100
W1 = (kk-1)/100;
W2 = 1 - W1;
checkconstr = zeros(1,n_of_iqcons);
fprintf(' No. x-vector f(x) |Cons.| \n')
fprintf('___________________________________________________\n')
checkconstr = zeros(1,n_of_iqcons);
for i = 1:10
deriv_f = grad_vec_f(X,delx,n_of_var,scale_factor);
sec_deriv_f = hessian(X,delx);
deriv_eqcon = grad_vec_eqcon(X,delx,n_of_eqcons);
deriv_ineqcon = grad_vec_ineqcon(X,delx,n_of_iqcons);
options = optimset('Display','off');
x = quad prog(sec_deriv_f,deriv_f,deriv_ineqcon,-ineqconstr_val(X),
deriv_eqcon,-eqconstr_val(X),[],[],X,options);
fprev = func_val(X);
X = X+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.