clear all
warning off
format compact
H = zeros(4);
H(1,1) = 0.2;
f = [0.1 0.2 0.12 0.185]';
A = [-99 -105 -95 -99;
       0    0   0   2;
      -1    0   0   0;
       0   -1   0   0;
       0    0  -1   0;
       0    0   0  -1;
       1    0   0   0];
b = [-98;
       1;
       0;
       0;
       0;
       0;
       0.4];
Aeq = [1 1 1 1];
beq = 1;
disp('Nominal optimum')
disp('----------------------------------')
m_nom=quadprog(H,f,A,b,Aeq,beq)
J_nom = 0.5*m_nom'*H*m_nom + f'*m_nom

disp('Re-optimized with octane of stream 3 = 97')
disp('----------------------------------')
A(1, 3) = -97;
m_d=quadprog(H,f,A,b,Aeq,beq)
J_d = 0.5*m_d'*H*m_d + f'*m_d

disp('Loss calculations')
for i = 1:4
  A = [ 1   1   1   1;
       99 105  97  99;
       0   0   0   1;
       0   0   0   0];
 
  b = [ 1, 98, 0, 0]';
  if i == 1
    disp('m1 constant')
    A(end, 1) = 1;
    b(end) = m_nom(1);
  elseif i == 2
    disp('m2 constant')
    A(end, 2) = 1;
    b(end) = m_nom(2);
  elseif i == 3
    disp('m3 constant')
    A(end, 3) = 1;
    b(end) = m_nom(3);
  elseif i == 4
    disp('m1-0.5m3 constant')
    A(end, 1) = 1;
    A(end, 3) = -1.5;
    b(end) = m_nom(1)-1.5*m_nom(3);
  end

  disp('----------------------------------')
  m = inv(A)*b
  J = 0.5*m'*H*m + f'*m
  Delta_J = J - J_d
  if any( sign(m) < 0 )
    disp('Infeasible')
  end
end
