Page 254 - Numerical Methods for Chemical Engineering
P. 254
Constrained minimizer fmincon in MATLAB 243
subject to the two equality constraints
2
2
a 1 (x 1 − x c1 ) + b 1 (y 1 − y c1 ) = 1
(5.126)
2
2
a 2 (x 2 − x c2 ) + b 2 (y 2 − y c2 ) = 1
where
x c1 = 0 y c1 = 0 a 1 = 0.5 b 1 = 0.3
(5.127)
x c2 = 5 y c2 = 5 a 2 = 0.2 b 2 = 0.4
Here, we use fmincon to find the closest points,
% specify ellipse data
ELL(1).xc = 0; ELL(1).yc = 0; ELL(1).a = 0.5; ELL(1).b = 0.3;
ELL(2).xc = 5; ELL(2).yc = 5; ELL(2).a = 0.2; ELL(2).b = 0.4;
% set initial guesses to ellipse centers with random offset
theta0 = [ELL(1).xc; ELL(1).yc; ELL(2).xc; ELL(2).yc];
theta0 = theta0 + 0.1 randn(size(theta0));
∗
% call fmincon to find closest points
Options = optimset(‘LargeScale’, ‘off’);
[theta, dist sq, exitflag, output] = fmincon (@ell cost fcn, theta0,. . .
[], [], [], [], [], [],@ell nonlcon, Options, ELL);
x1 = theta(1); y1 = theta(2); x2 = theta(3); y2 = theta(4);
The following routine returns the cost function:
function F = ell cost fcn(theta,ELL);
x1 = theta(1); y1 = theta(2); x2 = theta(3); y2 = theta(4);
∧
∧
F = (x1-x2) 2 + (y1-y2) 2;
return;
The equality constraint functions are returned by the routine
function [c,c eq] = ell nonlcon(theta, ELL);
c=0; % no inequality constraints
c eq = zeros(2,1); % two equality constraints
% extract positions
x1 = theta(1); y1 = theta(2); x2 = theta(3); y2 = theta(4);
% constraint function that states that (x1, y1) is on first ellipse
dx = x1 - ELL(1).xc; dy = y1 - ELL(1).yc;
∧
∗
c eq(1) = ELL(1).a dx 2+ELL(1).b dy 2-1;
∗
∧
% similiarly for the second ellipse
dx = x2 - ELL(2).xc; dy = y2 - ELL(2).yc;
∧
c eq(2) = ELL(2).a dx 2 + ELL(2).b dy 2-1;
∧
∗
∗
return;
fmincon reports the two closest points to be
(x 1 , y 1 ) = (0.83, 1.48) (x 2 , y 2 ) = (3.22, 4.04) (5.128)
in agreement with the results shown in Figure 5.12.