Bi7740: Scientific computing Optimization: a brief summary - Exercise Vlad Popovici popovici@iba.muni.cz Institute of Biostatistics and Analyses Masaryk University, Brno Vlad Bi7740: Scientific computing The beer can problem variables: x1 the diameter, x2 the height of the can Problem: let x = [x1, x2]T minimize f(x) = πx2 1 2 + πx1x2 with respect to x1, x2 subject to −2x1 + x2 ≤ 0 ceq(x) = 333 − πx2 1 4 x2 = 0 Vlad Bi7740: Scientific computing The differentials: the gradient of the objective function: f(x) = ∂f ∂x1 , ∂f ∂x2 T = [π(x1 + x2), πx1]T Vlad Bi7740: Scientific computing The differentials: the gradient of the objective function: f(x) = ∂f ∂x1 , ∂f ∂x2 T = [π(x1 + x2), πx1]T the Jacobian of the nonlinear constraint function (which is a scalar function, so it’s a gradient finally): Jceq = ∂ceq ∂x1 , ∂ceq ∂x2 = − π 2 x1x2, − π 4 x2 1 Vlad Bi7740: Scientific computing Matlab implementation Constraint function implementation (save it as beer_constraint.m file): function [c ceq c_j ceq_j] = beer_constraint(x, vol) % Evaluate the constraint for the beer can. % % x(1) − diameter % x(2) − height c = []; % no nonlinear inequalities ceq = vol − 0.25*pi*x(1)^2*x(2); % Jacobian: if nargout > 2 c_j = []; ceq_j = [−0.5*pi*x(1)*x(2), −0.25*pi*x(1)^2]; end return Vlad Bi7740: Scientific computing Objective function implementation (save it as beer_area.m file): function [f nabla_f] = beer_area(x) % Compute the area of a beer can. % x(1) − diameter % x(2) − height f = pi*x(1)*x(2) + 0.5*pi*x(1)^2; % gradient: if nargout > 1 nabla_f = [pi*x(1)+pi*x(2); pi*x(1)]; % column ... vector! end return Vlad Bi7740: Scientific computing Matlab session: try also optimtool! vol = 333; % cm^3 % to bound the parameter: h_constraint = @(x) (beer_constraint(x, vol)); A = [−2 1]; b = 0; lb = [4; 5]; ub = [8; 15]; x0 = [6; 10]; % set the options: options = optimoptions('fmincon'); options = optimoptions(options,'Display', 'off'); options = optimoptions(options,'Algorithm', ... 'active−set'); x = fmincon(@beer_area, x0, A, b, [], [], lb, ub, ... h_constraint, options); Vlad Bi7740: Scientific computing