Você está na página 1de 6

A implementação em Matlab desta lista está disponível para download em https://goo.

gl/znCEkr

Item [a]
Uma função foi implementada em Matlab, com o intuito de chegar se determinada solução para um
problema satisfazem as condições de KKT, dados os valores de e a função satisfyKKT() retorna lógico
TRUE se todas as três condições forem satisfeitas. A solução com multiplicadores de
Lagrange satisfaz as condições de KKT visto que e

Desta forma, o ponto fornecido resulta em uma condição de estacionariedade. Para determinar se é ponto
de mínimo, devemos calcular a matriz Hessiana

Cujo autovalores são , logo a matriz Hessiana é positiva-definida, sendo portanto, um ponto de
mínimo.

Item [b]
A figura abaixo mostra as curvas de nível do problema além de evidenciar o domínio viável dado pelas
restrições. O ponto em vermelho é a solução do problema .
Item [c]
O resultado é mostrado na Tabela 1, na última página

% Solution to problem 1 list 7

clc
close all

% Add OMT folder to the path.


folder = fileparts(which(mfilename));
addpath(genpath(folder));

syms x_1 x_2

f = x_1^2 + x_2^2 - 16*x_1 - 2*x_2;


g = [sin(2*x_1)+(2*pi-x_1)/8 - x_2 >=0;
-sin(2*x_1)+(2*pi-x_1)/8 + x_2 >=0];

fvars =[x_1, x_2];

xlim = [0, 7, -3, 3, 0.3];


fighandle = figure;
% Plotting Surface
plotSurface(f, xlim, fighandle)

% Plotting Contour and constraint


nfig = figure;
xlim = [0, 7, -3, 3, 0.01];
plotGradient(f, xlim, fvars, nfig);
plotConstraint(g, xlim, fvars, nfig);

% Plotting optimal solution


xsol = [2*pi, 0];
lambda = [81-16*pi, 79-16*pi];
plotOptimSol(xsol, nfig);

% Check KKT conditions


isKKT = satisfyKKT(f, g, fvars, xsol, lambda);

%Check Hessian
H = hessian( lagrangian(f,g), fvars );

function [xsol, T] = slpopt(f, g, b, geq, beq, x0, delta)


% SLPOPT Successive Linear Programming optimization

syms x2 x1
f = 64 - 16*x2 - 32*x1;

g = [4*x1+2*x2-30;
x1+2*x2-6];

fvars = [x1, x2];

x0 = [0; 0];

delta = 2;

% Compute the gradient of the objective function and constraints


fgrad = gradient_f(f, fvars);

ggrad = gradient_f(g, fvars);

ffun = matlabFunction(f, fvars);


x_old = x0;
x_i = x0;

for i = 1 : 15
f_lin = linearize_fun(ffun, fgrad, fvars, x_i);

g_lin = linearize_fun(gfun, ggrad, fvars, x_i);

[fcoefs, ~] = coeffs_fun(f_lin, fvars);


[A, b] = coeffs_fun(g_lin, fvars);
% [Aeq, beq] = coeffs_fun(geq_lin, fvars);
Aeq = []; beq = [];

lb = x_i - ones(size(x_i)).*delta;
ub = x_i + ones(size(x_i)).*delta;

% Solve the linear optimization problem


[x_i,~,~,~,LAMBDA] = linprog(fcoefs, A, b, Aeq, beq, lb, ub);
lambda = LAMBDA.ineqlin;

% Computes the Lagrangian


Lval = numLagrangian(f, g, lambda);

% evaluate original objective function using the solution


fval = ffun(x_i);

% evaluate the constraints in the point


gval = gfun(x_i);

% check if x_i is a viable solution


if (ffun(x_i) < ffun(x_old)) && all(violated_g(x_i) > violated_glin(x_i))

flag = true; %accept

elseif (ffun(x_i) > ffun(x_old)) && all( violated_g(x_i) < violated_glin(x_i) )


flag = false; %reject

elseif ( L(x_i) < L(x_old) )


flag = true;

else
flag = false;
end

if ~flag % if rejected
delta = delta/2;
x_i = x_old;
end

end
end

%%
function f_lin = linearize_fun(fun, grad_fun, Xvars, x_i)
% Linearize nonlinear function fun at point x_i using the function's
% gradient (grad_fun)

fi = fun(x_i);
grad_fi = grad_fun(x_i);
f_lin = fi + transpose(grad_fi)*(Xvars - x_i);

end

%%
function grad = gradient_f(f, fvars)
grad = [];
for i = 1:length(f)
grad = [grad; gradient(f(i))];
end

grad = matlabFunction(grad, fvars);

end

%%
function L = numLagrangian(fval, gval, lambda)

L = fval + dot(-gval,lambda);
end
%%
function [cf, cr] = coeffs_fun(f, fvars)
% Return the coefficients of a symbolic function Example: if f = 3*x^2 +
% pi*y + z + 2 , the function will return A = [3, pi, 1] (Note that the
% function will try to preserve the order of the coefficients). The last
% coefficient (2) will not be inserted in the output since it is not
% multiplied by any variable. If f is an array of symbolic expressions, the
% output will be a matriz, in wich each row will correspond to the
% coefficients of an expression.

% get variables from expression if fvars is empty


if isempty(fvars), fvars = symvar(f); end

cf = []; cr = [];
for i = 1 : length(f)
children_f = children(f(i));
cf_i = [];
cr_i = [];
for j = 1: length(children_f)
coef_f = coeffs(children_f(j), fvars);

if isempty(symvar(children_f(j)))
cr_i = [cr_i, -coef_f];
elseif ~isempty(symvar(children_f(j)))
cf_i = [cf_i, coef_f];
end
end

cf = [cf; double(cf_i)];
cr = [cr; double(cr_i)];

end
end

Item [d]

A tabela abaixo compara os resultados obtidos nos itens e .

Você também pode gostar