# Section 4.5.4: Design of a cantilever beam: recursive formulation (GP)

    % Boyd & Vandenberghe "Convex Optimization"
    % (a figure is generated)
    % Almir Mutapcic 02/08/06
    %
    % We have a segmented cantilever beam with N segments. Each segment
    % has a unit length and variable width and height (rectangular profile).
    % The goal is minimize the total volume of the beam, over all segment
    % widths w_i and heights h_i, subject to constraints on aspect ratios,
    % maximum allowable stress in the material, vertical deflection y, etc.
    %
    % The problem can be posed as a geometric program (posynomial form)
    %     minimize   sum( w_i* h_i)
    %         s.t.   w_min <= w_i <= w_max,       for all i = 1,...,N
    %                h_min <= h_i <= h_max
    %                S_min <= h_i/w_i <= S_max
    %                6*i*F/(w_i*h_i^2) <= sigma_max
    %                y_1 <= y_max
    %
    % with variables w_i and h_i (i = 1,...,N).
    % For other definitions consult the book.
    % (See exercise 4.31 for a non-recursive formulation.)

    % optimization variables
    N = 8;

    % constants
    wmin = .1; wmax = 100;
    hmin = .1; hmax = 6;
    Smin = 1/5; Smax = 5;
    sigma_max = 1;
    ymax = 10;
    E = 1; F = 1;

    cvx_begin gp
      % optimization variables
      variables w(N) h(N)

      % setting up variables relations
      % (recursive formulation)
      v = cvx( zeros(N+1,1) );
      y = cvx( zeros(N+1,1) );
      for i = N:-1:1
        fprintf(1,'Building recursive relations for index: %d\n',i);
        v(i) = 12*(i-1/2)*F/(E*w(i)*h(i)^3) + v(i+1);
        y(i) = 6*(i-1/3)*F/(E*w(i)*h(i)^3)  + v(i+1) + y(i+1);
      end

      % objective is the total volume of the beam
      % obj = sum of (widths*heights*lengths) over each section
      % (recall that the length of each segment is set to be 1)
      minimize( w'*h )
      subject to
        % constraint set
        wmin <= w; w <= wmax;
        hmin <= h; h <= hmax;
        Smin <= h./w; h./w <= Smax;
        6*F*[1:N]'./(w.*(h.^2)) <= sigma_max;
        y(1) <= ymax;
    cvx_end

    % display results
    disp('The optimal widths and heights are: ');
    w, h
    fprintf(1,'The optimal minimum volume of the beam is %3.4f.\n', sum(w.*h))

    % plot the 3D model of the optimal cantilever beam
    figure, clf
    cantilever_beam_plot([h; w])

    Building recursive relations for index: 8
    Building recursive relations for index: 7
    Building recursive relations for index: 6
    Building recursive relations for index: 5
    Building recursive relations for index: 4
    Building recursive relations for index: 3
    Building recursive relations for index: 2
    Building recursive relations for index: 1

    Calling SeDuMi: 595 variables (19 free), 353 equality constraints
    ------------------------------------------------------------------------
    SeDuMi 1.1 by AdvOL, 2005 and Jos F. Sturm, 1998, 2001-2003.
    Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
    Split 19 free variables
    eqs m = 353, order n = 285, dim = 945, blocks = 23
    nnz(A) = 1181 + 0, nnz(ADA) = 4577, nnz(L) = 2548
     it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
      0 :            8.22E-001 0.000
      1 :  2.60E+001 3.22E-001 0.000 0.3925 0.9000 0.9000   4.99  1  1  1.1E+000
      2 :  9.65E+000 9.72E-002 0.000 0.3015 0.9000 0.9000   2.09  1  1  2.1E-001
      3 :  6.13E+000 3.45E-002 0.000 0.3550 0.9000 0.9000   1.54  1  1  6.2E-002
      4 :  4.54E+000 1.24E-002 0.000 0.3582 0.9000 0.9000   1.27  1  1  2.0E-002
      5 :  4.07E+000 5.30E-003 0.000 0.4287 0.9000 0.9000   1.14  1  1  8.5E-003
      6 :  3.89E+000 2.28E-003 0.000 0.4295 0.9000 0.9000   1.12  1  1  3.7E-003
      7 :  3.80E+000 8.25E-004 0.000 0.3624 0.9000 0.9000   1.09  1  1  1.4E-003
      8 :  3.76E+000 2.11E-004 0.000 0.2558 0.9000 0.9000   1.06  1  1  3.5E-004
      9 :  3.75E+000 1.39E-005 0.000 0.0661 0.9900 0.9900   1.03  1  1  2.3E-005
     10 :  3.75E+000 5.13E-007 0.000 0.0368 0.9900 0.9900   1.02  1  1  8.5E-007
     11 :  3.75E+000 2.38E-008 0.189 0.0463 0.9900 0.9900   1.00  2  2  4.0E-008
     12 :  3.75E+000 2.24E-009 0.034 0.0943 0.9900 0.9900   1.00  4  5  3.8E-009

    iter seconds digits       c*x               b*y
     12      0.3   8.1  3.7475131070e+000  3.7475130804e+000
    |Ax-b| =  7.8e-008, [Ay-c]_+ =  8.3E-010, |x|= 4.1e+001, |y|= 3.4e+000

    Detailed timing (sec)
       Pre          IPM          Post
    3.004E-002    2.604E-001    0.000E+000
    Max-norms: ||b||=1.422363e+001, ||c|| = 2.202759e+000,
    Cholesky |add|=0, |skip| = 0, ||L.L|| = 47.8685.
    ------------------------------------------------------------------------
    Status: Solved
    Optimal value (cvx_optval): +42.4155
    The optimal widths and heights are:

    w =

        0.6214
        0.7830
        0.9063
        1.0130
        1.1003
        1.1766
        1.2000
        1.3333

    h =

        3.1072
        3.9149
        4.5313
        5.0648
        5.5014
        5.8828
        6.0000
        6.0000

    The optimal minimum volume of the beam is 42.4078.