% Approximating quadratic monomials 
% Carrilho - 22 Jan 2015
% 

% clear
clear, clc, close all

% limites para x 
% x1L = -1.0;
% x1U = 2.0;
x1L = -5.0;
x1U = 5.0;


% limites para y
% x2L = -3.0;
% x2U = 4.0;
x2L = -4.0;
x2U = 4.0;

% cria grid
[x1, x2] = meshgrid(x1L:(x1U-x1L)/50:x1U, x2L:(x2U-x2L)/50:x2U); 

% calcula f
w12 = x1.*x2;

% sets a standard viewpoint for all figures
% az = 17.5000;
az = 18;
el = 14;

% plot funcion
figure(1)
surf(x1,x2,w12)
hidden off 
view([az el])
xlabel('x_1')

% break 

% Mccormick approximations - MC1 
w12L_MC1 = zeros(size(w12));
w12U_MC1 = zeros(size(w12));
for i=1:length(w12(1,:)), 
    for j=1:length(w12(1,:)), 
        [w12L_MC1(i,j), w12U_MC1(i,j)] = MC1(x1(i,j),x2(i,j),x1L,x1U,x2L,x2U);
    end  
end

% plot surfaces
figure(2)
surf(x1,x2,w12)
hold on 
mesh(x1,x2,w12L_MC1)
mesh(x1,x2,w12U_MC1)
hidden off 
hold off 
view([az el])

% break

% Mccormick approximations - MC4 
w12L_MC4 = zeros(size(w12));
w12U_MC4 = zeros(size(w12));
for i=1:length(w12(1,:)), 
    for j=1:length(w12(1,:)), 
        [w12L_MC4(i,j), w12U_MC4(i,j)] = MC4(x1(i,j),x2(i,j),x1L,x1U,x2L,x2U);
    end  
end

% plot surfaces
figure(3)
surf(x1,x2,w12)
hold on 
mesh(x1,x2,w12L_MC4)
mesh(x1,x2,w12U_MC4)
hidden off 
hold off 
view([az el])


% saves figures to files 
print -f1 -dpng -r200 quadratic_monomial.png
print -f2 -dpng -r200 quadratic_monomial_MC1.png
print -f3 -dpng -r200 quadratic_monomial_MC4.png

