Sie sind auf Seite 1von 12

Escola Politécnica da Universidade de São Paulo

Diego Varalda de Almeida, 10580379

Resolução da 5a lista de exercícios

PMR5215 - Otimização aplicada ao projeto


de sistemas mecânicos

Orientadores: Prof. Dr. Emílio Carlos Nelli


Silva ; Prof. Dr. Thiago Martins

Universidade de São Paulo


Programa de Pós-graduação

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)

% Author(s): Diego Varalda de Almeida


% Copyright (c) 2018, Diego ALMEIDA
% <diego.vda@usp.br>

% 12-Apr-2018 First version of EXE1.


close all
clc

syms x_1 x_2


f = 8*x_1^4+5*x_2^4+2*x_2^2-36*x_1^2;

fvars = [x_1, x_2];

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);

%% compute the gradient


gf = gradient(f);
latex(gf);

%% Solving the optimization problem numerically

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

% -> Plot contour


contour(X_1, X_2, Z, 'ShowText', 'on');
pbaspect([1 1 1]);
end
A solução exibida no command window é como segue

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

O ponto estacionário é calculado fazendo , desta forma encontramos . A matriz


Hessiana é constante e positiva definida, logo o ponto calculado é de mínimo.

[c]
 

[d]

[e]
 

[f]

[g]

O código a seguir foi implementado em Matlab e pode ser baixado em https://goo.gl/znCEkr

function exe2
%EXE1 Solution to exercise 2 (list 5)

% Author(s): Diego Varalda de Almeida


% Copyright (c) 2018, Diego ALMEIDA
% <diego.vda@usp.br>
% 14-Apr-2018 First version of EXE1.
close all
clc

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)

%% solving the gradient


x_sol = solve(g, fvars);
disp(x_sol.x);
disp(x_sol.y);
solPlot = figure(gradplot);
plotPoint(solPlot, x_sol.x, x_sol.y);

%% 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

% -> Plot contour


contour(X_1, X_2, Z, 'ShowText', 'on');
pbaspect([1 1 1]);
end

%%
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));

% % find the expression for alpha


% syms alpha
% psi = f;
% for i=1:length(g)
% psi = subs(psi, fvars(i), (fvars(i)+g(i)*alpha))
% apha_exp = solve(

% x = transpose({x0(1), x0(2)});
x = x0(1);
y = x0(2);
for i = 1 : iter
% compute the objective function
fval = ffun(x, y);

% compute the gradient


grad_f = transpose( g(x, y) );

% s
s_x = -grad_f(1);
s_y = -grad_f(2);

% compute the step


alpha = ( 2*s_y*(6*x-7*y+15) - 3*s_x*(7*x-4*y+5) ) / ...
(-24*s_x*s_y+21*s_x^2+14*s_y^2);

% updates the table


T(i, :) = [x, y, fval, grad_f, s_x, s_y, alpha];

% updates x values (design variables)


x = x + alpha*s_x;
y = y + alpha*s_y;

end
end

%%
function T = conjugateGradient(f, x0, iter)
% CONJUGATEGRADIENT implements the optimization method conjugate gradient
% 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 = x0(1);
y = x0(2);

for i = 1 : iter
% compute the objective function
fval = ffun(x, y);

% compute the gradient


g_i = transpose( g(x, y) );

% Computing beta and s_i


if i == 1
beta_i = 0;
s_i = -g_i;
else
beta_i = norm(g_i)^2 / norm(old_g_i)^2;
s_i = beta_i * old_s_i - g_i;
end

old_g_i = g_i; % updates g_i - 1


old_s_i = s_i; % updates s_i - 1

s_x = s_i(1);
s_y = s_i(2);

% compute the step alpha


alpha = ( 2*s_y*(6*x-7*y+15) - 3*s_x*(7*x-4*y+5) ) / ...
(-24*s_x*s_y+21*s_x^2+14*s_y^2);

% updates the table


T(i, :) = [x, y, fval, g_i, beta_i, s_i, alpha];

% updates x values (design variables)


x = x + alpha*s_x;
y = y + alpha*s_y;

end
end

Das könnte Ihnen auch gefallen