% Minimization of Peron-Frobenious norm example
% Section 4.5.4 example in Boyd & Vandenberghe "Convex Optimization"
% (see page 165-167 for more details)
%
% The goal is to minimize the spectral radius of a square matrix A
% which is elementwise nonnegative, Aij >= 0 for all i,j. In this
% case A has a positive real eigenvalue lambda_pf (the Perron-Frobenius
% eigenvalue) which is equal to the spectral radius, and thus gives
% the fastest decay rate or slowest growth rate.
% The problem of minimizing the Perron-Frobenius eigenvalue of A,
% possibly subject to posynomial inequalities in some underlying
% variable x can be posed as a GP (for example):
%
%   minimize   lambda_pf( A(x) )
%       s.t.   f_i(x) <= 1   for i = 1,...,p
%
% where matrix A entries are some posynomial functions of variable x,
% and f_i are posynomials.
%
% We consider a specific example in which we want to find the fastest
% decay or slowest growth rate for the bacteria population governed
% by a simple dynamic model (see page 166). The problem is a GP:
%   minimize   lambda
%       s.t.   b1*v1 + b2*v2 + b3*v3 + b4*v4 <= lambda*v1
%              s1*v1 <= lambda*v2
%              s2*v2 <= lambda*v3
%              s3*v3 <= lambda*v4
%              1/2 <= ci <= 2
%              bi == bi^{nom}*(c1/c1^{nom})^alpha_i*(c2/c2^{nom})^beta_i
%              si == si^{nom}*(c1/c1^{nom})^gamma_i*(c2/c2^{nom})^delta_i
%
% with variables bi, si, ci, vi, lambda.
%
% Almir Mutapcic 10/05

% GP variables
gpvar lambda b(4) s(3) v(4) c(2)

% constants
c_nom = [1 1]';
b_nom = [2 3 2 1]';
alpha = [1 1 1 1]'; beta  = [1 1 1 1]';
s_nom = [1 1 3]';
gamma = [1 1 1]'; delta = [1 1 1]';

% objective is the Perron-Frobenius eigenvalue
obj = lambda;

% constraints
constr = [...
  % inequalities
  b'*v      <= lambda*v(1);
  s(1)*v(1) <= lambda*v(2);
  s(2)*v(2) <= lambda*v(3);
  s(3)*v(3) <= lambda*v(4);
  [0.5; 0.5] <= c; c <= [2; 2];
  % equalities
  b == b_nom.*((ones(4,1)*(c(1)/c_nom(1))).^alpha).*...
              ((ones(4,1)*(c(2)/c_nom(2))).^beta);
  s == s_nom.*((ones(3,1)*(c(1)/c_nom(1))).^gamma).*...
              ((ones(3,1)*(c(2)/c_nom(2))).^delta);
];

% find the optimal eigenvalue
[opt_lambda solution status] = gpsolve(obj,constr);
assign(solution);

% displaying results
disp(' ')
if lambda < 1
  fprintf(1,'The fastest decay rate of the bacteria population is %3.2f.\n', lambda);
else
  fprintf(1,'The slowest gr0wth rate of the bacteria population is %3.2f.\n', lambda);
end

disp(' ')
fprintf(1,'The concentration of chemical 1 achieving this result is %3.2f.\n', c(1));
fprintf(1,'The concentration of chemical 2 achieving this result is %3.2f.\n', c(2));
disp(' ')

% construct matrix A
A = zeros(4,4);
A(1,:) = b';
A(2,1) = s(1);
A(3,2) = s(2);
A(4,3) = s(3);

% eigenvalues of matrix A
disp('Eigenvalues of matrix A are: ')
eigA = eig(A)

% >> eig(A) (answer checks)
%
%    0.8041
%   -0.2841
%   -0.0100 + 0.2263i
%   -0.0100 - 0.2263i
 
Iteration     primal obj.         gap        dual residual     previous step.
 
   1         2.32510e+00       3.60000e+01       1.24e+02               Inf
   2         1.77783e+00       1.93739e+01       2.81e+00       8.49937e-01
   3         9.18051e-01       1.28939e+01       3.41e-01       6.53253e-01
   4         2.14855e-01       7.39203e+00       1.36e-02       8.00541e-01
   5         1.32055e-02       5.69457e+00       4.80e-03       4.05659e-01
   6        -2.73043e-01       3.39796e+00       1.67e-06       9.81358e-01
 
Iteration     primal obj.         gap        dual residual     previous step.
 
   1         7.32611e+01       3.60000e+01       1.06e+02               Inf
   2         4.06769e+01       3.49894e+01       1.02e+02       2.02930e-02
   3         2.00501e+01       3.35290e+01       9.55e+01       3.17566e-02
   4         1.21747e+01       3.21799e+01       8.55e+01       5.36207e-02
   5         4.96235e+00       2.96083e+01       6.55e+01       1.25000e-01
   6         2.95813e+00       2.63499e+01       3.68e+01       2.50000e-01
   7         2.00648e+00       1.74821e+01       1.58e-02       1.00000e+00
   8         1.00893e+00       8.71360e+00       2.16e-04       1.00000e+00
   9         4.29754e-01       4.36798e+00       2.28e-05       1.00000e+00
  10         1.14019e-01       2.19242e+00       1.22e-05       1.00000e+00
  11        -4.64937e-02       1.10091e+00       1.17e-05       1.00000e+00
  12        -1.29646e-01       5.52664e-01       5.74e-06       1.00000e+00
  13        -1.72832e-01       2.77254e-01       1.63e-06       1.00000e+00
  14        -1.95127e-01       1.38966e-01       2.77e-07       1.00000e+00
  15        -2.06516e-01       6.95922e-02       2.93e-08       1.00000e+00
  16        -2.12276e-01       3.48275e-02       2.19e-09       1.00000e+00
  17        -2.15170e-01       1.74221e-02       1.39e-10       1.00000e+00
  18        -2.16620e-01       8.71323e-03       8.52e-12       1.00000e+00
  19        -2.17346e-01       4.35716e-03       5.24e-13       1.00000e+00
  20        -2.17709e-01       2.17872e-03       3.25e-14       1.00000e+00
  21        -2.17891e-01       1.08939e-03       2.02e-15       1.00000e+00
  22        -2.17981e-01       5.44705e-04       1.26e-16       1.00000e+00
  23        -2.18027e-01       2.72354e-04       7.88e-18       1.00000e+00
  24        -2.18050e-01       1.36178e-04       4.92e-19       1.00000e+00
  25        -2.18061e-01       6.80890e-05       3.08e-20       1.00000e+00
  26        -2.18067e-01       3.40445e-05       1.92e-21       1.00000e+00
  27        -2.18069e-01       1.70223e-05       1.20e-22       1.00000e+00
  28        -2.18071e-01       8.51114e-06       7.51e-24       1.00000e+00
  29        -2.18071e-01       4.25557e-06       4.69e-25       1.00000e+00
  30        -2.18072e-01       2.12779e-06       2.94e-26       1.00000e+00
  31        -2.18072e-01       1.06389e-06       1.83e-27       1.00000e+00
  32        -2.18072e-01       5.31947e-07       1.18e-28       1.00000e+00
  33        -2.18072e-01       2.65973e-07       9.23e-30       1.00000e+00
  34        -2.18072e-01       1.32987e-07       4.87e-31       1.00000e+00
  35        -2.18072e-01       6.64933e-08       6.74e-31       1.00000e+00
  36        -2.18072e-01       3.32467e-08       3.07e-32       1.00000e+00
  37        -2.18072e-01       1.66233e-08       3.25e-31       1.00000e+00
  38        -2.18072e-01       8.31166e-09       2.29e-31       1.00000e+00
Solved
Problem succesfully solved.
 
The fastest decay rate of the bacteria population is 0.80.
 
The concentration of chemical 1 achieving this result is 0.50.
The concentration of chemical 2 achieving this result is 0.50.
 
Eigenvalues of matrix A are: 

eigA =

   0.8041          
  -0.2841          
  -0.0100 + 0.2263i
  -0.0100 - 0.2263i