%%
% Author: Paola Gervasio 09/08/2020
% paola-gervasio.unibs.it
%
% rappresentazione grafico di un problema di punto sella
%
%
clear
close all
A=[3,1;1,3]/10;
f=[2;3]/10;
xsol=A\f;
phi=@(x)0.5*x'*A*x-f'*x;

b=[0.1;0.2]; g=1;
vincolo=@(x)b'*x-g;
lagrangiana=@(x,p)phi(x)+p*vincolo(x);

x1=(-10:.5:10); y1=(-10:.5:10);
n=length(y1); m=length(x1);z=zeros(n,m);

p1=linspace(-20,20,21);
for p=p1
for i=1:n
    for j=1:m
        z(i,j)=lagrangiana([x1(j);y1(i)],p);
    end
end
figure(1); clf

mesh(x1,y1,z)
hold on
plot3(xsol(1),xsol(2),0,'o','Markersize',8,'markerfacecolor','r')
contour(x1,y1,z,30)
% axis([-5,5,-5,5,-3,10])
xlabel('x_1'); ylabel('x_2');
title(['funzione L(x,p) con p=',num2str(p)])
view([30,0])
% figure(2); clf
% contour(x1,y1,z,40)
% hold on
% plot(xsol(1),xsol(2),'o','Markersize',8,'markerfacecolor','r'); grid on
% xlabel('x_1'); ylabel('x_2');
% title(['linee di livello di L(x,p) con p=',num2str(p)])

pause 
end