This is a static copy of a profile report

Home

Function details for con_klThis is a static copy of a profile report

Home

con_kl (Calls: 1, Time: 0.343 s)
Generated 01-May-2020 08:41:16 using performance time.
function in file /Users/sidhantkhanna/Documents/GitHub/BKS modified/code/Firms/con_kl.m
Copy to new window for comparing multiple runs

Parents (calling functions)

Function NameFunction TypeCalls
optiklfunction1
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
30
addpath(genpath('/Users/sidhan...
10.325 s94.6%
365
f_con_concave_diff = fl_phi.*(...
60.005 s1.4%
23
close all;
10.002 s0.7%
364
f_con_concave = fl_phi*((ar_kj...
60.001 s0.4%
366
d = ((1-fl_phi)*(1-fl_delta)+ ...
60.001 s0.4%
All other lines  0.009 s2.5%
Totals  0.343 s100% 
Children (called functions)

Function NameFunction TypeCallsTotal Time% TimeTime Plot
genpathfunction10.248 s72.2%
addpathfunction10.077 s22.3%
closefunction10.002 s0.6%
Self time (built-ins, overhead, etc.)  0.017 s4.9%
Totals  0.343 s100% 
Code Analyzer results
Line numberMessage
26This statement (and possibly following ones) cannot be reached.
35Best practice is to separate output variables with commas.
35The value assigned here to 'fl_zhi' appears to be unused. Consider replacing it by ~.
105The variable 'ar_k_con' appears to change size on every loop iteration. Consider preallocating for speed.
105The variable 'ar_l_con' appears to change size on every loop iteration. Consider preallocating for speed.
146The value assigned here to 'val' appears to be unused. Consider replacing it by ~.
156The value assigned here to 'idx2' appears to be unused. Consider replacing it by ~.
237IF might not be aligned with its matching END (line 325).
361The value assigned to variable 's1' might be unused.
385The value assigned to variable 'ar_l_con1' might be unused.
436This statement (and possibly following ones) cannot be reached.
Coverage results
Show coverage for parent directory
Total lines in function420
Non-code lines (comments, blank lines)182
Code lines (lines that can run)238
Code lines that did run59
Code lines that did not run179
Coverage (did run/can run)24.79 %
Function listing
time 
Calls 
 line
  21 
function [ar_k_con,ar_l_con]=con_kl(varargin)
  22 

  0.002 
      1 
  23
close all; 
< 0.001 
      1 
  24
bl_profile = false;      % Switch off profile if running a tester/calling from another function 
< 0.001 
      1 
  25
if(bl_profile) 
  26 
profile off;
  27 
profile on;
  28 
end
  29 

  0.325 
      1 
  30
addpath(genpath('/Users/sidhantkhanna/Documents/GitHub/BKS modified/')); 
  31 

< 0.001 
      1 
  32
if ~isempty(varargin) 
  33 
    
  0.002 
      1 
  34
    [ar_a_v,ar_z_v,ar_a,ar_z ... 
      1 
  35
        fl_ahi,fl_zhi, fl_phi,fl_theta,fl_alpha,fl_w,fl_r,fl_delta,... 
      1 
  36
        fl_kappa,it_agridno,it_zgridno, ... 
      1 
  37
        ] = varargin{:}; 
  38 
   % Display matrix and graphs of constrained k and l (for every combination of a and z)
  39 
    
  40 
    % if testing the index below, print out graph, and also show each
  41 
    % iteration of the first order taylor approximation. if bl_state_test
  42 
    % is true, then test specific state iteration to arrive at \bar{k}.
< 0.001 
      1 
  43
    bl_print           = false; 
< 0.001 
      1 
  44
    bl_state_test      = false; 
< 0.001 
      1 
  45
    it_test_a_idx      = 1;         
< 0.001 
      1 
  46
    it_test_z_idx      = 1; 
< 0.001 
      1 
  47
    bl_saveimg         = false;  % True if images are to be saved, false if you want to publish them 
< 0.001 
      1 
  48
    it_k_n             = 20000;                            % capital grid points to create capital grid and search for  
  49 

  50 
else
  51 
    
  52 
    fl_ahi             = 50;
  53 
    fl_zhi             = 7;
  54 
    it_agridno         = 5;
  55 
    it_zgridno         = 6;
  56 
    ar_a               = linspace(0,fl_ahi,it_agridno);    % Sample asset grid
  57 
    ar_z               = linspace(0.2,fl_zhi,it_zgridno);  % Sample entrepreneurial productivity grid
  58 
    %ar_a              = 50;                               % for testing single a value  
  59 
    %ar_z              = 1;                                % for testing single z value 
  60 
    [a_m,z_m]          = meshgrid(ar_a,ar_z);              % Meshed matrix of assets and entrepreneurial productivity
  61 
                                                           % grid (size for a_m = it_zgridnoxit_agridno, z_m =it_zgridnoxit_agridno)
  62 
   
  63 
    ar_a_v             = a_m(:);                           % Meshed-vectorised asset grid
  64 
    ar_z_v             = z_m(:);                           % Meshed-vectorised entrepreneurial productivity grid
  65 
    it_k_n             = 20000;                            % capital grid points to create capital grid and search for 
  66 
                                                           % constrained k   
  67 
    fl_phi             = 0.5;
  68 
    
  69 
    fl_alpha = 0.4;
  70 
    fl_theta = 0.79 - fl_alpha;
  71 
    fl_delta = 0.06;
  72 
    fl_kappa = 2;
  73 
    [fl_r,fl_w,fl_ahi] = ...
  74 
        deal(0.04,1.8,100);
  75 
        
  76 
    %% Testing Controls
  77 
   
  78 
    bl_print           = true;                             % Print matrix of optimal k with graphs
  79 
    bl_state_test      = true;                             % Plotting taylor method graphs for root finding
  80 
    it_test_a_idx      = 2;                                % a index for drawing graph or root search of k using TS approx
  81 
    it_test_z_idx      = 4;                                % z index for drawing graph or root search of k using TS approx
  82 
    bl_saveimg         = false; 
< 0.001 
      1 
  83
end 
  84 

  85 
%% Parameter controls
< 0.001 
      1 
  86
it_max_iter_taylor     = 7;                 % No of iterations for taylor series approximation for constraint function 
  87 

  88 
%% Initialize Matrices
< 0.001 
      1 
  89
fl_R              = fl_r + fl_delta; 
< 0.001 
      1 
  90
mt_k_con          = ones(it_agridno,it_zgridno);    % Matrix for storing constrained k , for each combination on a and z 
< 0.001 
      1 
  91
mt_l_con          = ones(it_agridno,it_zgridno); 
  92 

< 0.001 
      1 
  93
fl_version    = 3; 
  94 
% version 1 = numerical method, version 2 = vectorised, version 3 = taylor
  95 
% series approximation for loop - has both vectorised and unvectorised
  96 
% versions
< 0.001 
      1 
  97
fl_vectorised = true; % for version 3- loop or vectorised 
  98 

  99 

 100 
%% Version 1 - Numerical Method
< 0.001 
      1 
 101
if(fl_version == 1) 
 102 
    for i = 1:it_agridno*it_zgridno
 103 
        fl_a    = ar_a_v(i);
 104 
        fl_z    = ar_z_v(i);
 105 
        [ar_k_con(i),ar_l_con(i)] = conkl_numerical(fl_a,fl_z,fl_r,fl_w,fl_alpha,fl_theta,fl_kappa,fl_delta,fl_phi,fl_R);
 106 
    end
 107 
    
 108 
    ar_k_con = ar_k_con';                 % transposing to get same order as input vector (it_agridnoxit_zgridno x 1)
 109 
    ar_l_con = ar_l_con';                 % transposing to get same order as input vector (it_agridnoxit_zgridno x 1)
 110 
    
 111 
    mt_k_con = reshape(ar_k_con, [it_zgridno,it_agridno]);
 112 
    mt_l_con = reshape(ar_l_con, [it_zgridno,it_agridno]);
 113 
    
 114 
    mt_k_con = mt_k_con';                   % getting to matrix of dimension it_agridnoxit_zgridno
 115 
    mt_l_con = mt_l_con';                   % getting to matrix of dimension it_agridnoxit_zgridno
 116 
    
 117 
    
 118 
%% Version 2 - Vectorised solution  
< 0.001 
      1 
 119
elseif (fl_version == 2) 
 120 
    
 121 
    ar_ks1 = linspace(0, 5*fl_ahi, it_k_n);  % Grid of capital to search for roots of constraint equation (order = 1 X it_k_n) 
 122 
    ar_ks  = fliplr(ar_ks1);                   % flipping array to pick up the second root (non-zero root)
 123 
    
 124 
    f_lmax = @(z,k) (fl_w./((k.^fl_alpha).*fl_theta.*z)).^(1/(fl_theta - 1)); % max l for a given constrained k
 125 
    
 126 
    f_con_concave = @(a,z,k) fl_phi.*((k.^fl_alpha).*z.*(f_lmax(z,k)).^fl_theta - fl_w.*(f_lmax(z,k)))-(1+fl_r)*fl_kappa.*ones(numel(a),it_k_n)+(1+fl_r).*(a).*ones(1,it_k_n); 
 127 
    % concave part of the constraint function
 128 
    
 129 
    f_con_straight = @(a,z,k) (1-fl_phi).*(1-fl_delta).*ones(numel(a),1).*k+fl_R.*ones(numel(a),1).*k;
 130 
    % straight line part of the constraint function
 131 
    
 132 
    f_con = @(a,z,k) f_con_concave(a,z,k) - f_con_straight(a,z,k); % constraint function
 133 
    
 134 
    f1 = f_con(ar_a_v, ar_z_v, ar_ks); 
 135 
    % f1 evaulates the constraint for each combination of a,z and k. Order of f1 = ((it_agridnoxit_zgridno) X it_k_n)
 136 

 137 
    
 138 
    % for i=1:it_agridno*it_zgridno
 139 
    %    f1(i,it_k_n)=-1000;
 140 
    % end
 141 
    
 142 
    f1(:,it_k_n)  = -1000;  % assigning a large absolute value to the constraint at k=0 so as to
 143 
                           % remove the root k=0
 144 
    
 145 
    
 146 
    [val, idx]    = min(abs((f1)'));  
 147 
   % for each combi of a,z, root of the constraint equation is the k for 
 148 
   % which the value of the constraint function is closest to zero. 
 149 
   % min function on a matrix minimizes over each column, min (NXM) is of
 150 
   % order (1XM). As f1 is of the order ((it_agridnoxit_zgridno) X it_k_n), 
 151 
   % dimension of val and idx = (1 X (it_agridnoXit_zgridno)). Since we need to minimize f1 over
 152 
   % k for each combination of a and z, and dim f1=((it_agridnoxit_zgridno) X it_k_n),
 153 
   % dim f1' = (it_k_n x (it_agridnoxit_zgridno)), dim min(abs((f1)'))= 1 X (it_agridnoxit_zgridno), 
 154 
   % dim val , idx = 1 X (it_agridnoxit_zgridno)                                                                   
 155 
   
 156 
   [val2,idx2]    = max(((f1)'));     % store the max value of f1(k) for each combination of a,z
 157 
    ar_k_con      = ar_ks(idx);    %  storing constrained k values, dim = 1 X (it_agridnoxit_zgridno)   
 158 
    
 159 
    % Analysing cases where the only root of constraint equation is k=0 in
 160 
    % the range of k's we are checking
 161 
    % There are 2 subcases:
 162 
    
 163 
    % 1) k = 0 is the only root and max of f is obtained at k = 0 ( all other
 164 
    % f's are negative in the range of k's )
 165 
    
 166 
    % 2) k = 0 is not the only root, but only root in the given range of k's
 167 
    % we are checking. The actual constrained k is very high in this case,
 168 
    % so assuming the optimal would surely be less than the constrained k
 169 
    
 170 
    for i=1:it_agridno*it_zgridno 
 171 
        if (ar_k_con(i)==ar_ks1(2))
 172 
            if (val2(i)<=0)
 173 
                ar_k_con(i)=0;  % Case 1)
 174 
            else
 175 
                ar_k_con(i)=1000000; % Case 2)
 176 
            end
 177 
        end
 178 
    end
 179 
    
 180 
    % Limitation of this method is for case 2), if optimal k was greater than
 181 
    % constrained k, those cases you are not sure about (not sure if those
 182 
    % cases exist)
 183 
    
 184 
    ar_k_con = ar_k_con'; % transposing to get same order as input vector (it_agridnoxit_zgridno x 1)
 185 
    
 186 
    ar_l_con = (fl_w./(fl_theta.*ar_z_v.*ar_k_con.^(fl_alpha))).^(1/(fl_theta-1));
 187 
    
 188 
    mt_k_con = reshape(ar_k_con, [it_zgridno,it_agridno]);
 189 
    mt_l_con = reshape(ar_l_con, [it_zgridno,it_agridno]);
 190 
    
 191 
    mt_k_con = mt_k_con';           % getting to matrix of dimension it_agridnoxit_zgridno
 192 
    mt_l_con = mt_l_con';            % getting to matrix of dimension it_agridnoxit_zgridno
 193 
    
 194 
%% Version 3 - Taylor Series Method - For loop
< 0.001 
      1 
 195
elseif (fl_version == 3) 
< 0.001 
      1 
 196
    if(~fl_vectorised) 
 197 
        
 198 
        % Solving constraint k for each combination of a and z one by one.
 199 
        % The constraint function has a linear part and a curved part.
 200 
        % Starting at any point, the the curved part is approximated to a straight line with a taylor series expansion
 201 
        % The intersection of the first approximation and the linear part
 202 
        % is the first guess of k. At the first guess of k, approximate the
 203 
        % curve part again as a stright line, then find its intersection
 204 
        % with a straight line for the next guess of k. Continue doing that
 205 
        % until it_max_iter_taylor iterations (you converge to the correct k in less than those no. of iterations 
 206 
        
 207 
        
 208 
        syms k w alph theta kappa r phi delta R
 209 
        for i = 1:it_agridno
 210 
            for j = 1:it_zgridno
 211 
                
 212 
                % Defining the functions using symbols only
 213 
                
 214 
                f_lmax             = (w/((k^alph)*theta*ar_z(j)))^(1/(theta - 1));  % optimal l to constrained k
 215 
                f_con_concave      = phi*((k^alph)*ar_z(j)*(f_lmax)^theta - w*(f_lmax))-(1+r)*kappa+(1+r)*(ar_a(i));
 216 
                % Concave part of constraint function of k
 217 
                f_con_straight     = (1-phi)*(1-delta)*k+R*k;
 218 
                % Straight line part of constraint function of k
 219 
                
 220 
                % Substituting parameters in the constraint function
 221 
                f_con_straight_sub = subs(f_con_straight,{w,alph,theta,kappa,r,phi,delta,R},{fl_w,fl_alpha fl_theta,fl_kappa,fl_r,fl_phi,fl_delta, fl_R});
 222 
                f_con_concavej_k   = subs(f_con_concave, {w,alph,theta,kappa,r,phi,delta,R}, {fl_w,fl_alpha fl_theta,fl_kappa,fl_r,fl_phi,fl_delta, fl_R});
 223 
                
 224 
                m  = 1;   % no if iteration for guess of k            
 225 
                kj = 1;   % First guess of k
 226 
                
 227 
                % * a = constant term in taylor approximate of the concave part of constraint function
 228 
                % * b = coefficient of k in taylor approximate of the concave part of constraint function
 229 
                % * c = 0 (constant term of straight line part of constraint - staight line function chosen that way)
 230 
                % * d = coefficient of k of straight line part of constraint 
 231 
                
 232 
                if (bl_state_test)
 233 
                    ar_k_store = zeros(1, it_max_iter_taylor);
 234 
                    % Store all values of guesses of k
 235 
                end
 236 
                
 237 
                if (fl_phi == 0) % assuming kappa = 0, id a = 0 and phi = 0, opti k = 0
 238 
                mt_k_con(i,j) = (1+fl_r)*(ar_a(i)- fl_kappa);
 239 
                mt_l_con(i,j) = (fl_w/((mt_k_con(i,j)^fl_alpha)*fl_theta*ar_z(j)))^(1/(fl_theta - 1));
 240 
               
 241 
                    else
 242 
                % bl_solution_exits = false;
 243 
                s=0;
 244 

 245 
                while (m < it_max_iter_taylor)
 246 
                
 247 
                    f_con_concavej        = double(subs(f_con_concavej_k, {k}, {kj})); % substituting first guess of k
 248 
                    % computing coefficients a and b of taylor series approximate
 249 
                    f_con_concave_diff_k  = diff(f_con_concave,k);
 250 
                    f_con_concave_diff_kj = double(subs(f_con_concave_diff_k, {k,w,alph,theta,kappa,r,phi,delta,R}, {kj,fl_w,fl_alpha fl_theta,fl_kappa,fl_r,fl_phi,fl_delta, fl_R}));
 251 
            
 252 
                    d = (1-fl_phi)*(1-fl_delta)+ fl_R;
 253 
                    a = double(f_con_concavej-f_con_concave_diff_kj*kj); 
 254 
                    b = double(f_con_concave_diff_kj);
 255 
                    if (b>d) % Case where both straight lines intersect at negative k (slope of tangent to curve greater 
 256 
                        % than that of straight line), first guess needs to a large no.
 257 
                        k_jplus1=kj*100;
 258 
                        
 259 
                    elseif (a<0)
 260 
                        k_jplus1 = kj;
 261 
                        s=s+1;
 262 
                    else
 263 
                    k_jplus1 = (a/(d-b)); % intersection of taylor approximate and straight line part of the constraint 
 264 
                   
 265 
                    end
 266 
                    
 267 
              % Displaying coefficients of straight line and tangent to
 268 
              % curved part of the constraint, and each guess of k
 269 
              
 270 
                    if (i == it_test_a_idx && j == it_test_z_idx && bl_state_test)
 271 
                        st_test_iter_info_one = ['iter:', num2str(m), ', kj:', num2str(kj)];
 272 
                        st_test_iter_info_two = ['y-interp for lin approx of concave:', num2str(a)];
 273 
                        st_test_iter_info_thr = ['slope for lin approx of concave:', num2str(b)];
 274 
                        st_test_iter_info_fou = ['slope for origin linear line:', num2str(d)];
 275 
                        disp(st_test_iter_info_one);
 276 
                        disp(st_test_iter_info_two);
 277 
                        disp(st_test_iter_info_thr);    
 278 
                        disp(st_test_iter_info_fou);
 279 
                        ar_k_store(m) = kj; % storing all k's which were guessed until convergence to root
 280 
                    end  
 281 
                    
 282 
                    % updating
 283 
                    kj = k_jplus1; % next guess of k
 284 
                    m  = m+1; % updating iteration for next guess of k
 285 
                end
 286 
                
 287 
                %if(~bl_solution_exits)
 288 
                 %   display ('constraint equation has no positive solution for capital');
 289 
                %return;                    
 290 
                %else
 291 
                    
 292 
                mt_k_con(i,j) = kj; % updating constrained k for each a and z
 293 
                mt_l_con(i,j) = (fl_w/((kj^fl_alpha)*fl_theta*ar_z(j)))^(1/(fl_theta - 1));
 294 
                if (s>0)
 295 
                    mt_k_con(i,j)=0;
 296 
                    mt_l_con(i,j)=0;
 297 

 298 
                end  
 299 
                % updating optimal l to correponding constrained k for each a and z
 300 
                
 301 
                bl_state_test = false;
 302 
                %% Graph Curve and line Intersection
 303 
                if (i== it_test_a_idx && j == it_test_z_idx && bl_state_test)
 304 
                    figure(1);
 305 
                    hold on;
 306 
                    %fl_k_max = min(0.001, kj + kj/10);
 307 
                    fl_k_max = 120;
 308 
                    fplot(f_con_concavej_k, [0, fl_k_max]);
 309 
                    fplot(f_con_straight_sub, [0, fl_k_max]);
 310 
                    
 311 
                    for it_m = 1:1:it_max_iter_taylor
 312 
                        fl_kj_cur = ar_k_store(it_m);
 313 
                        line([real(fl_kj_cur) real(fl_kj_cur)],[0 fl_kj_cur+1], 'LineStyle', '--'); 
 314 
                        % -- lines are guesses of k
 315 
                    end
 316 
                    
 317 
                    line([real(kj) real(kj)],[0 kj+1]);
 318 
                    % final solution k    
 319 
                    grid on;
 320 
                    grid minor;
 321 
                    if(bl_saveimg)
 322 
                    saveas(gcf, '/Users/sidhantkhanna/Documents/GitHub/BKS modified/code/Firms/figures/con_kl/taylor.png')
 323 
                    end
 324 
                end                
 325 
            end
 326 
                
 327 
            end
 328 
               
 329 
        end
 330 
        
 331 
        mt_k_con1 = mt_k_con';
 332 
        mt_l_con1 = mt_l_con';
 333 

 334 
        ar_k_con = mt_k_con1(:);                    % matrix unlayered one column under another
 335 
        ar_l_con = mt_l_con1(:);                    % matrix unlayered one column under another
 336 

 337 

 338 
              
 339 
%% Version 3 - Taylor Series Method - Vectorised Version 
 340 

< 0.001 
      1 
 341
    elseif(fl_phi==0) 
 342 
         ar_k_con =(1+fl_r).*(ar_a_v- fl_kappa.*ones(numel(ar_a_v),1));
 343 
         ar_l_con =(fl_w./(fl_theta.*ar_z_v.*ar_k_con.^(fl_alpha))).^(1/(fl_theta-1));
 344 
         ar_k_con(ar_k_con<0) = 0;
 345 
         ar_l_con(ar_l_con<0) = 0;
 346 
         
 347 
          mt_k_con=reshape(ar_k_con, [it_zgridno,it_agridno]);
 348 
    mt_l_con=reshape(ar_l_con, [it_zgridno,it_agridno]);
 349 
    
 350 
    mt_k_con=mt_k_con';
 351 
    mt_l_con=mt_l_con';
 352 
    
 353 
       
 354 
                      
< 0.001 
      1 
 355
         else 
 356 
           
 357 
    
< 0.001 
      1 
 358
        ar_kj = ones(it_agridno*it_zgridno, 1); % initial guess of k 
< 0.001 
      1 
 359
        m     = 1; 
< 0.001 
      1 
 360
        s = zeros(it_agridno*it_zgridno, 1); 
< 0.001 
      1 
 361
        s1 = zeros(it_agridno*it_zgridno, 1); 
< 0.001 
      1 
 362
        while (m < it_max_iter_taylor) 
< 0.001 
      6 
 363
        f_lmax = (fl_w./((ar_kj.^fl_alpha)*fl_theta.*ar_z_v)).^(1/(fl_theta - 1)); 
  0.001 
      6 
 364
        f_con_concave = fl_phi*((ar_kj.^fl_alpha).*ar_z_v.*(f_lmax).^fl_theta - fl_w.*(f_lmax))-(1+fl_r)*fl_kappa.*ones(it_agridno*it_zgridno,1)+(1+fl_r).*(ar_a_v); 
  0.005 
      6 
 365
        f_con_concave_diff = fl_phi.*(fl_alpha.*ar_kj.^(fl_alpha - 1).*ar_z_v.*((fl_w./(ar_kj.^fl_alpha.*fl_theta.*ar_z_v)).^(1/(fl_theta - 1))).^fl_theta - (fl_alpha.*ar_kj.^fl_alpha.*fl_w.*((fl_w./(ar_kj.^fl_alpha.*fl_theta.*ar_z_v)).^(1/(fl_theta - 1))).^(fl_theta - 1).*(fl_w./(ar_kj.^fl_alpha.*fl_theta.*ar_z_v)).^(1/(fl_theta - 1) - 1))./(ar_kj.^(fl_alpha + 1).*(fl_theta - 1)) + (fl_alpha.*fl_w^2.*(fl_w./(ar_kj.^fl_alpha.*fl_theta.*ar_z_v)).^(1/(fl_theta - 1) - 1))./(ar_kj.^(fl_alpha + 1).*fl_theta.*ar_z_v.*(fl_theta - 1))); 
  0.001 
      6 
 366
        d = ((1-fl_phi)*(1-fl_delta)+ fl_R).*ones(it_agridno*it_zgridno, 1); 
< 0.001 
      6 
 367
        a = (f_con_concave-f_con_concave_diff.*ar_kj); 
< 0.001 
      6 
 368
        b = f_con_concave_diff; 
 369 
        
 370 
        % find k_{j+1} for everyone
< 0.001 
      6 
 371
        ar_k_jplus1 = (a./(d-b)); 
 372 
        % ar_k_jplus1_dup = ar_k_jplus1;
 373 
        
 374 
        % replace if lin approximate for slope is greater than linear slope
< 0.001 
      6 
 375
        ar_k_jplus1((b>d)) = ar_kj(b>d)*100; 
 376 
  %      s1(b>d) = s1(b>d)+1;
< 0.001 
      6 
 377
        ar_k_jplus1((a<0) & (b<d)) = ar_kj(a<0 & b<d); 
< 0.001 
      6 
 378
        s(a<0 & b<d) = s(a<0 & b<d)+1; 
< 0.001 
      6 
 379
        ar_kj = ar_k_jplus1; 
< 0.001 
      6 
 380
        m = m+1; 
< 0.001 
      6 
 381
        end 
 382 
        
< 0.001 
      1 
 383
        ar_k_con = ar_kj; 
< 0.001 
      1 
 384
        ar_l_con = (fl_w./(fl_theta.*ar_z_v.*ar_k_con.^(fl_alpha))).^(1/(fl_theta-1)); 
< 0.001 
      1 
 385
        ar_l_con1 = ar_l_con; 
< 0.001 
      1 
 386
        ar_k_con(s>0) = 0; 
< 0.001 
      1 
 387
        ar_l_con(s>0) = 0; 
 388 
 %      ar_k_con(s1>0) = ar_kj(s1>0);
 389 
 %      ar_l_con(s1>0) = ar_l_con1(s1>0);
 390 
    
< 0.001 
      1 
 391
    mt_k_con=reshape(ar_k_con, [it_zgridno,it_agridno]); 
< 0.001 
      1 
 392
    mt_l_con=reshape(ar_l_con, [it_zgridno,it_agridno]); 
 393 
    
< 0.001 
      1 
 394
    mt_k_con=mt_k_con'; 
< 0.001 
      1 
 395
    mt_l_con=mt_l_con'; 
 396 
    
 397 
                      
< 0.001 
      1 
 398
    end 
 399 
    
< 0.001 
      1 
 400
end 
 401 
    
 402 

 403 
%% Print matrix for constrained k and l
 404 

< 0.001 
      1 
 405
if(bl_print) 
 406 

 407 
    disp('mt_k_con');
 408 
    disp(mt_k_con);
 409 
    disp('mt_l_con');
 410 
    disp(mt_l_con);
 411 
    
 412 
figure (2)
 413 
surf(ar_z, ar_a, mt_k_con);
 414 
title('K constraint given a and z');
 415 
xlabel('a');
 416 
ylabel('z');
 417 
zlabel('K constraint');
 418 
view(125,35);
 419 
if(bl_saveimg)
 420 
saveas(gcf, '/Users/sidhantkhanna/Documents/GitHub/BKS modified/code/Firms/figures/con_kl/Kconst_az.png')
 421 
end
 422 

 423 
figure (3)
 424 
surf(ar_z, ar_a, mt_l_con);
 425 
title('L constraint given a and z');
 426 
xlabel('a');
 427 
ylabel('z');
 428 
zlabel('L with K constrained');
 429 
view(125,35);
 430 
if(bl_saveimg)
 431 
saveas(gcf, '/Users/sidhantkhanna/Documents/GitHub/BKS modified/code/Firms/figures/con_kl/Lconst_az.png')
 432 
end
 433 
end
 434 

< 0.001 
      1 
 435
if(bl_profile) 
 436 
profile off;
 437 
profile viewer;
 438 
st_file_name = '/Users/sidhantkhanna/Documents/GitHub/BKS modified/code/Profile/Firms/con_kl';
 439 
profsave(profile('info'), st_file_name);
 440 
end

Other subfunctions in this file are not included in this listing.