326 Appendix B
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code quadr.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% x -> independent variable and symbolic variable (later)
% y -> exp(-x)
% syms -> symbolic object (MATLAB function)
% taylor -> MATLAB function
% subs -> symbolic substitution (MATLAB function)
% xlabel, ylabel, legend, plot, hold on -> MATLAB functions
%
x = -2:0.01:2;
y = exp(-x);
plot(x,y)
hold on
% Linear approximation
syms x
f = taylor(exp(-x),2);
x = -2:0.01:2;
z = subs(f);
plot(x,z,'r--')
% Quadratic approximation
syms x
f = taylor(exp(-x),3);
x = -2:0.01:2;
z = subs(f);
plot(x,z,'g--')
legend('exp(-x)','linear','quadratic')
xlabel('x')
ylabel('f(x)')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code quadr_examp12.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% x1 -> radius of can
% x2 -> height of can
% area -> area of can
% pi -> MATLAB variable
% x,y,z -> array of design points
% vv -> user identified contour values
% xit, x2t -> identified point at which gradient required
% contour -> MATLAB function to generate contours
% syms -> symbolic object (MATLAB function)
% subs -> symbolic substitution (MATLAB function)
% gradient -> analytical value
% hessian -> analytical value
% xlabel, ylabel, legend, plot, hold on -> MATLAB functions
%
327Appendix B
clear all
clc
for x1 = 1:200
for x2 = 1:200
area = 2*pi*x1*x2 + 2*pi*x1*x1;
x(x1,x2) = x1;
y(x1,x2) = x2;
z(x1,x2) = area;
end
end
vv = [15000;50000;60000;70000;80000;90000;150000;200000];
[c, h] = contour(x,y,z,vv); clabel(c, h);
hold on
syms x1p x2p
gradient = [2*pi*x2p+4*pi*x1p;2*pi*x1p];
hessian = [4*pi 2*pi; 2*pi 0];
% Linear approximation
x1p = 60;
x2p = 72.629;
gf = subs(gradient);
for delx1 = 1:60
for delx2 = 1:60
x1 = x1p + delx1;
x2 = x2p + delx2;
area = 2*pi*x1p*x2p + 2*pi*x1p*x1p + gf’*[delx1;delx2]
+ 0.5*[delx1 delx2]*(hessian*[delx1;delx2]);
[x1 x2 area]
xx(delx1,delx2) = x1;
yy(delx1,delx2) = x2;
zz(delx1,delx2) = area;
end
end
vv1 = [50000;60000;70000;80000;90000];
[c, h] = contour(xx,yy,zz,vv1,'rd','LineWidth',3);
xlabel('x_1, mm')
ylabel('x_2, mm')
legend('Objective Function','Quadratic approx.')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
328 Appendix B
Chapter 2
Code Name Details
exhaustive.m Exhaustive search to locate the minimum of the test problem
bisection.m Bisection method
func.m Objective function to be coded here
newtonraphson.m Newton–Raphson method
secant.m Secant method
cubic.m Cubic polynomial t
golden.m Golden section method
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code exhaustive.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% delta -> step size for search
% T -> independent variable, temperature
% U -> cost function
% uvec -> vector of cost function evaluated at
% different temperatures
% minu -> minimum of cost function
% min -> MATLAB function
%
clear all
clc
uvec=[];
delta = 0.01;
for T = 40:delta:90
U = 204165.5/(330-2*T) + 10400/(T-20);
uvec = [uvec U];
plot(T,U)
hold on
end
xlabel('T');ylabel('U');
[minu,i]= min(uvec);
fprintf('Minimum Cost = %6.2f\n ',minu)
fprintf('occurs at T = %6.2f\n ',40+(i-1)*delta)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code bisection.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% a -> lower bound of the design variable
% b -> upper bound of the design variable
329Appendix B
% 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
%
clear all
clc
a = 40;
b = 90;
epsilon = 0.01;
delx = 0.01;
fprintf(' a b \n')
fprintf('-------------------------\n')
for i= 1:100
fprintf(' %7.3f %8.3f \n',a,b)
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;
else
a = alpha;
end
if abs(a-b) < epsilon
break;
end
end
fprintf('-------------------------\n')
fprintf('x* = %7.3f Minimum = %8.3f\n',a,func(a))
fprintf('Number of function calls = %3d\n',4*i)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code func.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% x -> input variable to the function
% fx -> output from the function
%
function fx = func(x)
fx = 204165.5/(330-2*x) + 10400/(x-20);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
330 Appendix B
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code newtonraphson.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% x -> initial guess of design variable
% delx -> ?x for central difference method
% derivative -> derivative using central difference method
% sec_derivative -> second derivative
% epsilon -> constant used to terminate the program
% xprev -> value of x stored from previous iteration
%
clear all
clc
x = 45;
delx = 0.01;
epsilon = 0.01;
fprintf(' x f(x) Deriv. Second deriv.\n')
fprintf('-----------------------------------------\n')
for i = 1:100
derivative = (func(x+delx) - func(x-delx))/(2*delx);
sec_derivative =(func(x+delx)+func(x-delx)-2*func(x))/
(delx*delx);
fprintf('%8.3f %8.3f %8.3f %8.3f\n',x,func(x),derivative,
sec_derivative)
xprev = x;
x = x- derivative/sec_derivative;
if abs(x-xprev) < epsilon
break;
end
end
fprintf('-----------------------------------------\n')
fprintf('Number of function calls = %3d\n',5*i)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB code secant.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
%
clear all
clc
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.