Beruflich Dokumente
Kultur Dokumente
São Paulo
2018
A resolução desta e das demais listas (implementação em Matlab) podem ser baixadas em
https://goo.gl/znCEkr
Exercício 1
[a]
[b]
Seja , o gradiente é calculado como
[c]
O código para calcular a solução do problema proposto foi implementado em Matlab e é mostrado a seguir
function exe1()
%EXE1 Solution to exercise 1 (list 5)
ffun = matlabFunction(f);
[X1, X2] = meshgrid(-2:0.1:2);
Z = ffun(X1, X2);
%Plotting surface
surf(X1, X2, Z);
xlabel(strcat('$', char(fvars(1)), '$'), 'Interpreter','latex', 'FontSize',17);
ylabel(strcat('$', char(fvars(2)), '$'), 'Interpreter','latex', 'FontSize',17);
zlabel(strcat('$', 'f(x_1, x_2)', '$'), 'Interpreter','latex', 'FontSize',17);
title('Surface plot');
pbaspect([1 1 1]);
set(gcf,'color','white');
gradplot = figure;
plotGradient(f, fvars, gradplot, X1, X2);
X0 = [1, 0
-2, 0];
for i = 1 : length(X0)
options = optimoptions('fminunc','Algorithm', 'trust-region',
'SpecifyObjectiveGradient',true);
[Xsol,FVAL,~,~,GRAD,HESSIAN] = fminunc(@optproblem,transpose(X0(i, :)), options);
% [Xsol,GRAD] = fminunc(@optproblem,transpose(X0(2, :)));
% [Xsol,FVAL,~,~,GRAD,HESSIAN]
disp('X_sol');
disp(Xsol);
disp('Gradient');
disp(GRAD);
disp('Function value at Xsol');
disp(FVAL);
disp('Hessian');
disp(HESSIAN)
end
end
%%
function [f, g] = optproblem(x)
f = x(1).^2.*-3.6e1+x(2).^2.*2.0+x(1).^4.*8.0+x(2).^4.*5.0;
g = [ 32.*x(1).^3 - 72.*x(1);
20.*x(2).^3 + 4.*x(2)];
end
%%
function plotGradient(f, fvars, fhandle, X_1, X_2)
ffun = matlabFunction(f);
% gradient
grad = matlabFunction(transpose(gradient(f, fvars)), 'Outputs',{'G1', 'G2'});
Z = ffun(X_1, X_2);
try
G = grad(X_1, X_2); %matrix with the gradient values
catch
[G1, G2] = gradient(Z, 100, 100); %numerical gradient
G = [G1, G2];
end
set(fhandle,'color','white');
% -> plot vector field
quiver(X_1, X_2, G(:, 1:size(G, 2)/2), G(:,size(G, 2)/2 + 1:size(G, 2)));
xlabel(strcat('$', char(fvars(1)), '$'), 'Interpreter','latex', 'FontSize',17);
ylabel(strcat('$', char(fvars(2)), '$'), 'Interpreter','latex', 'FontSize',17);
title('Gradient and Contour Plot');
hold on
X_sol (1, 0)
1.5000
0
Gradient
1.0e-06 *
0.5700
0
Function value at Xsol
-40.5000
Hessian
(1,1) 144.0000
(2,2) 4.0000
%---------------------
X_sol (-1, 0)
-1.5000
0
Gradient
1.0e-05 *
-0.9702
0
Function value at Xsol
-40.5000
Hessian
(1,1) 144.0000
(2,2) 4.0000
[d]
No item c, calculando numericamente o problema sem restrição partindo dos pontos indicados
e , obtemos dois pontos distintos, e , em ambos os casos o
gradiente é o vetor nulo , o que era esperado uma vez que o problema não possui restrições. O
problema é convexo uma vez que a matriz Hessiana é constante e todos os seus autovalores são positivos.
Exercício 2
[a]
[b]
Gradiente
A matriz Hessiana
[c]
[d]
[e]
[f]
[g]
function exe2
%EXE1 Solution to exercise 2 (list 5)
syms x y
f = 21*x^2 - 24*x*y + 30*x + 14*y^2 - 60*y;
fvars = symvar(f);
[X, Y] = meshgrid(-2:0.5:6);
ffun = matlabFunction(f);
Z = ffun(X, Y);
plotSurface(X, Y, Z, fvars);
gradplot = figure;
plotGradient(f, fvars, gradplot, X, Y, Z);
%% Compute Gradient
g = gradient(f);
latex(g)
%% Hessian Matrix
H = hessian(f);
latex(H)
all(eig(H) > eps)
%% Steepest descent
x0 = [0, 0];
T = steepestDescent(f, x0, 10);
dg = digits();
digits(4); %reduce number of digits to 4 (variable precision)
latex(sym(vpa(T, 4)))
plotPoint(gradplot, T(:,1), T(:, 2));
plotLine(gradplot, T(:,1), T(:, 2));
%% Conjugate Gradient
T = conjugateGradient(f, x0, 4);
latex(sym(vpa(T, 4)))
plotPoint(gradplot, T(:,1), T(:, 2));
plotLine(gradplot, T(:,1), T(:, 2));
digits(dg); %restore digits value
%% item G
s0 = T(1, 7:8);
s1 = T(2, 7:8);
disp('item g');
disp(s1 * H * transpose(s0));
end
%%
function plotGradient(f, fvars, fhandle, X_1, X_2, Z)
% gradient
grad = matlabFunction(transpose(gradient(f, fvars)), 'Outputs',{'G1', 'G2'});
try
G = grad(X_1, X_2); %matrix with the gradient values
catch
[G1, G2] = gradient(Z, 100, 100); %numerical gradient
G = [G1, G2];
end
set(fhandle,'color','white');
% -> plot vector field
quiver(X_1, X_2, G(:, 1:size(G, 2)/2), G(:,size(G, 2)/2 + 1:size(G, 2)));
xlabel(strcat('$', char(fvars(1)), '$'), 'Interpreter','latex', 'FontSize',17);
ylabel(strcat('$', char(fvars(2)), '$'), 'Interpreter','latex', 'FontSize',17);
title('Gradient and Contour Plot');
hold on
%%
function plotSurface(X1, X2, Z, fvars)
%Plotting surface
surf(X1, X2, Z);
xlabel(strcat('$', char(fvars(1)), '$'), 'Interpreter','latex', 'FontSize',17);
ylabel(strcat('$', char(fvars(2)), '$'), 'Interpreter','latex', 'FontSize',17);
zlabel(strcat('$', 'f(x_1, x_2)', '$'), 'Interpreter','latex', 'FontSize',17);
title('Surface plot');
pbaspect([1 1 1]);
set(gcf,'color','white');
end
%%
function plotPoint(figureHandle, x, y)
set(0, 'CurrentFigure', figureHandle);
scatter(x, y, 30, 'r', 'filled');
end
function plotLine(figureHandle, x, y)
set(0, 'CurrentFigure', figureHandle);
hold on;
plot(x, y, 'r');
end
%%
function T = steepestDescent(f, x0, iter)
% STEEPESTDESCENT implements the optimization method steepest descent
% f is the objective function (symbolic function)
% x0 are the initial guess for the n design variables
% iter is the number of iterations
fvars = symvar(f);
ffun = matlabFunction(f);
g = matlabFunction(gradient(f, fvars));
% x = transpose({x0(1), x0(2)});
x = x0(1);
y = x0(2);
for i = 1 : iter
% compute the objective function
fval = ffun(x, y);
% s
s_x = -grad_f(1);
s_y = -grad_f(2);
end
end
%%
function T = conjugateGradient(f, x0, iter)
% CONJUGATEGRADIENT implements the optimization method conjugate gradient
% f is the objective function (symbolic function)
fvars = symvar(f);
ffun = matlabFunction(f);
g = matlabFunction(gradient(f, fvars));
x = x0(1);
y = x0(2);
for i = 1 : iter
% compute the objective function
fval = ffun(x, y);
s_x = s_i(1);
s_y = s_i(2);
end
end