% Run this file to find the steady state

%This version of find_ss has been modified to allow the declaration of policy instruments as arguments of the
%function. Still, if no policy tools arguments are provided the function will solve the usual version
 
function [x,flag] = find_ss(taxes)

       x0 = abs(randn(1,32));
       % x0 = [2.1061    1.9847    1.9847    0.9236    0.8662    0.8662    2.7259    2.6683...
       %       2.6683   23.8188   25.4048   25.4048    0.0381    0.0350    0.0350    1.9685...
       %       2.0546    2.0546    1.0139    1.0165    1.0165   23.4871   11.6041   11.6041...
       %      13.8007   13.8007   27.9331         0         0    1.0055    1.0055    1.8319...
       %       1.8319    1.8319    1.0166    1.0106    1.0106    0.1815    0.1777    0.1777...
       %       ];
       % x0 =  [2.1857    1.9903    1.9903    0.7893    0.9939    0.9939    2.4612    3.0020...
       %        3.0020   24.0124   27.4752   27.4752    0.0341    0.0364    0.0364    2.0798...
       %        2.0145    2.0145    1.0146    1.0187    1.0187    9.0205    5.2191    5.2191...
       %       22.2561   22.2561   59.5041    0.0000    0.0000    1.0101    1.0101    1.0101...
       %        1.0146    1.0146    2.8867    2.0005    2.0005    0.0327    0.0207    0.0207...
       %        0.1311    0.1999    0.1999];

       x0 =  [2.1857    1.9903    1.9903    0.7893    0.9939    0.9939    2.4612    3.0020    3.0020   24.0124   27.4752   27.4752    1.0146...
              1.0187    1.0187    9.0205    5.2191    5.2191   22.2561   22.2561   59.5041    1.0101    1.0101    1.0101    1.0146    1.0146...
              2.8867    2.0005    2.0005    0.0327    0.0207    0.0207];

       x0 = abs(x0);
       tol = 1e-8;
       %options = optimset('Display', 'iter', 'MaxFunEvals', 25000, 'MaxIter', 5000, 'TolFun', tol, 'TolX', tol);
       options = optimoptions('fsolve', 'Display', 'none', 'MaxFunctionEvaluations', 75000, 'MaxIterations', 3500, 'FunctionTolerance', tol, 'StepTolerance', tol);
       %options.Algorithm = 'levenberg-marquardt'; %trust-region-dogleg, trust-region

       %Declaration of objective function 
       if nargin == 0       %usual SS equations function w/o declaring policy tools as arguments
              obj = 'MaPCoord_sseqs'
       else
              obj = @(x)MaPCoord_sseqs(x,taxes); %function that takes 'taxes' as arguments and computes SS
       end

       [x,fval,flag] = fsolve(obj,x0,options); %'MaPCoord_sseqs'
end

   
%a= ["eq1 = - Lambdac(+1)*R + 1 + eta*Bc;";
%"eq2 = - Lambdaa(+1)*R + 1 + eta*Ba;";
%"eq3 = - Lambdab(+1)*R + 1 + eta*Bb;"]
