diff --git a/matlab/dyn_ramsey_static.m b/matlab/dyn_ramsey_static.m index a92cba11d..7427603e9 100644 --- a/matlab/dyn_ramsey_static.m +++ b/matlab/dyn_ramsey_static.m @@ -50,9 +50,11 @@ elseif options_.steadystate_flag M.endo_names,'exact')]; end if inst_nbr == 1 - inst_val = csolve(nl_func,ys_init(k_inst),'',options_.solve_tolf,100); %solve for instrument, using univariate solver, starting at initial value for instrument + %solve for instrument, using univariate solver, starting at initial value for instrument + inst_val = csolve(nl_func,ys_init(k_inst),'',options_.solve_tolf,100); else - [inst_val,info1] = dynare_solve(nl_func,ys_init(k_inst),0); %solve for instrument, using multivariate solver, starting at initial value for instrument + %solve for instrument, using multivariate solver, starting at initial value for instrument + [inst_val,info1] = dynare_solve(nl_func,ys_init(k_inst),options_); end ys_init(k_inst) = inst_val; exo_ss = [oo.exo_steady_state oo.exo_det_steady_state]; @@ -61,7 +63,7 @@ elseif options_.steadystate_flag else n_var = M.orig_endo_nbr; xx = oo.steady_state(1:n_var); - [xx,check] = dynare_solve(nl_func,xx,0); + [xx,check] = dynare_solve(nl_func,xx,options_); [junk,junk,steady_state] = nl_func(xx); end diff --git a/matlab/dynare_solve.m b/matlab/dynare_solve.m index fa7d385bc..4cedaf5f9 100644 --- a/matlab/dynare_solve.m +++ b/matlab/dynare_solve.m @@ -1,12 +1,11 @@ -function [x,info] = dynare_solve(func,x,jacobian_flag,varargin) -% function [x,info] = dynare_solve(func,x,jacobian_flag,varargin) +function [x,info] = dynare_solve(func,x,options,varargin) +% function [x,info] = dynare_solve(func,x,options,varargin) % proposes different solvers % % INPUTS % func: name of the function to be solved % x: guess values -% jacobian_flag=1: jacobian given by the 'func' function -% jacobian_flag=0: jacobian obtained numerically +% options: struct of Dynare options % varargin: list of arguments following jacobian_flag % % OUTPUTS @@ -16,7 +15,7 @@ function [x,info] = dynare_solve(func,x,jacobian_flag,varargin) % SPECIAL REQUIREMENTS % none -% Copyright (C) 2001-2014 Dynare Team +% Copyright (C) 2001-2015 Dynare Team % % This file is part of Dynare. % @@ -33,14 +32,16 @@ function [x,info] = dynare_solve(func,x,jacobian_flag,varargin) % You should have received a copy of the GNU General Public License % along with Dynare. If not, see . -global options_ +% jacobian_flag=1: jacobian given by the 'func' function +% jacobian_flag=0: jacobian obtained numerically +jacobian_flag = options.jacobian_flag; % Set tolerance parameter depending the the caller function. stack = dbstack; if strcmp(stack(2).file,'simulation_core.m') - tolf = options_.dynatol.f; + tolf = options.dynatol.f; else - tolf = options_.solve_tolf; + tolf = options.solve_tolf; end info = 0; @@ -79,7 +80,7 @@ if max(abs(fvec)) < tolf return ; end -if options_.solve_algo == 0 +if options.solve_algo == 0 if ~isoctave if ~user_has_matlab_license('optimization_toolbox') error('You can''t use solve_algo=0 since you don''t have MATLAB''s Optimization Toolbox') @@ -87,7 +88,7 @@ if options_.solve_algo == 0 end options=optimset('fsolve'); options.MaxFunEvals = 50000; - options.MaxIter = options_.steady.maxit; + options.MaxIter = options.steady.maxit; options.TolFun = tolf; options.Display = 'iter'; if jacobian_flag @@ -115,17 +116,17 @@ if options_.solve_algo == 0 else info = 1; end -elseif options_.solve_algo == 1 - [x,info]=solve1(func,x,1:nn,1:nn,jacobian_flag,options_.gstep, ... - tolf,options_.solve_tolx, ... - options_.steady.maxit,options_.debug,varargin{:}); -elseif options_.solve_algo == 9 - [x,info]=trust_region(func,x,1:nn,1:nn,jacobian_flag,options_.gstep, ... - tolf,options_.solve_tolx, ... - options_.steady.maxit,options_.debug,varargin{:}); -elseif options_.solve_algo == 2 || options_.solve_algo == 4 +elseif options.solve_algo == 1 + [x,info]=solve1(func,x,1:nn,1:nn,jacobian_flag,options.gstep, ... + tolf,options.solve_tolx, ... + options.steady.maxit,options.debug,varargin{:}); +elseif options.solve_algo == 9 + [x,info]=trust_region(func,x,1:nn,1:nn,jacobian_flag,options.gstep, ... + tolf,options.solve_tolx, ... + options.steady.maxit,options.debug,varargin{:}); +elseif options.solve_algo == 2 || options.solve_algo == 4 - if options_.solve_algo == 2 + if options.solve_algo == 2 solver = @solve1; else solver = @trust_region; @@ -133,7 +134,7 @@ elseif options_.solve_algo == 2 || options_.solve_algo == 4 if ~jacobian_flag fjac = zeros(nn,nn) ; - dh = max(abs(x),options_.gstep(1)*ones(nn,1))*eps^(1/3); + dh = max(abs(x),options.gstep(1)*ones(nn,1))*eps^(1/3); for j = 1:nn xdh = x ; xdh(j) = xdh(j)+dh(j) ; @@ -143,18 +144,18 @@ elseif options_.solve_algo == 2 || options_.solve_algo == 4 [j1,j2,r,s] = dmperm(fjac); - if options_.debug + if options.debug disp(['DYNARE_SOLVE (solve_algo=2|4): number of blocks = ' num2str(length(r))]); end for i=length(r)-1:-1:1 - if options_.debug + if options.debug disp(['DYNARE_SOLVE (solve_algo=2|4): solving block ' num2str(i) ', of size ' num2str(r(i+1)-r(i)) ]); end [x,info]=solver(func,x,j1(r(i):r(i+1)-1),j2(r(i):r(i+1)-1),jacobian_flag, ... - options_.gstep, ... - tolf,options_.solve_tolx, ... - options_.steady.maxit,options_.debug,varargin{:}); + options.gstep, ... + tolf,options.solve_tolx, ... + options.steady.maxit,options.debug,varargin{:}); if info return end @@ -162,17 +163,17 @@ elseif options_.solve_algo == 2 || options_.solve_algo == 4 fvec = feval(func,x,varargin{:}); if max(abs(fvec)) > tolf [x,info]=solver(func,x,1:nn,1:nn,jacobian_flag, ... - options_.gstep, tolf,options_.solve_tolx, ... - options_.steady.maxit,options_.debug,varargin{:}); + options.gstep, tolf,options.solve_tolx, ... + options.steady.maxit,options.debug,varargin{:}); end -elseif options_.solve_algo == 3 +elseif options.solve_algo == 3 if jacobian_flag [x,info] = csolve(func,x,func,1e-6,500,varargin{:}); else [x,info] = csolve(func,x,[],1e-6,500,varargin{:}); end -elseif options_.solve_algo == 10 - olmmcp = options_.lmmcp; +elseif options.solve_algo == 10 + olmmcp = options.lmmcp; [x,fval,exitflag] = lmmcp(func,x,olmmcp.lb,olmmcp.ub,olmmcp,varargin{:}); if exitflag == 1 info = 0; diff --git a/matlab/dynare_solve_block_or_bytecode.m b/matlab/dynare_solve_block_or_bytecode.m index ce82bc5df..3fc9d7a22 100644 --- a/matlab/dynare_solve_block_or_bytecode.m +++ b/matlab/dynare_solve_block_or_bytecode.m @@ -1,5 +1,5 @@ function [x,info] = dynare_solve_block_or_bytecode(y, exo, params, options, M) -% Copyright (C) 2010-2012 Dynare Team +% Copyright (C) 2010-2015 Dynare Team % % This file is part of Dynare. % @@ -26,7 +26,7 @@ if options.block && ~options.bytecode if options.solve_algo <= 4 [y, check] = dynare_solve('block_mfs_steadystate', ... ss(M.block_structure_stat.block(b).variable), ... - options.jacobian_flag, b, ss, exo, params, M); + options, b, ss, exo, params, M); if check ~= 0 % error(['STEADY: convergence % problems in block ' int2str(b)]) @@ -62,18 +62,21 @@ elseif options.bytecode if M.block_structure_stat.block(b).Simulation_Type ~= 1 && ... M.block_structure_stat.block(b).Simulation_Type ~= 2 [y, check] = dynare_solve('block_bytecode_mfs_steadystate', ... - x(M.block_structure_stat.block(b).variable), ... - options.jacobian_flag, b, x, exo, params, M); + x(M.block_structure_stat ... + .block(b).variable), ... + options, b, x, exo, params, M); if check - % error(['STEADY: convergence - % problems in block ' int2str(b)]) + % error(['STEADY: convergence problems in block ' + % int2str(b)]) info = 1; return end x(M.block_structure_stat.block(b).variable) = y; else [chk, nulldev, nulldev1, x] = bytecode( x, exo, params, ... - x, 1, x, 'evaluate', 'static', ['block = ' int2str(b)]); + x, 1, x, 'evaluate', ... + 'static', ['block ' ... + '= '] int2str(b)]); if chk info = 1; return @@ -81,9 +84,8 @@ elseif options.bytecode end; end else - [x, check] = dynare_solve('bytecode_steadystate', ... - y, ... - options.jacobian_flag, exo, params); + [x, check] = dynare_solve('bytecode_steadystate', y, ... + options, exo, params); if check % error('STEADY: convergence problems') info = 1; diff --git a/matlab/ep/solve_stochastic_perfect_foresight_model_1.m b/matlab/ep/solve_stochastic_perfect_foresight_model_1.m index 56249d3c6..e9aa577aa 100644 --- a/matlab/ep/solve_stochastic_perfect_foresight_model_1.m +++ b/matlab/ep/solve_stochastic_perfect_foresight_model_1.m @@ -146,7 +146,7 @@ y = repmat(steady_state,block_nbr,1); old_options = options_; options_.solve_algo = options_.ep.solve_algo; options_.steady.maxit = options_.ep.maxit; -[y,info] = dynare_solve(@ep_problem_2,y,1,exo_simul,pfm); +[y,info] = dynare_solve(@ep_problem_2,y,options_,exo_simul,pfm); options_ = old_options; if info flag = 1; diff --git a/matlab/evaluate_steady_state_file.m b/matlab/evaluate_steady_state_file.m index ee2a9922d..12d361a37 100644 --- a/matlab/evaluate_steady_state_file.m +++ b/matlab/evaluate_steady_state_file.m @@ -117,8 +117,7 @@ function [ys,params,info] = evaluate_steady_state_file(ys_init,exo_ss,M,options) end [ys,check] = dynare_solve('restricted_steadystate',... ys(indv),... - options.jacobian_flag, ... - exo_ss,indv); + options, exo_ss,indv); end diff --git a/matlab/partial_information/PCL_resol.m b/matlab/partial_information/PCL_resol.m index 3787de03b..154bfb883 100644 --- a/matlab/partial_information/PCL_resol.m +++ b/matlab/partial_information/PCL_resol.m @@ -89,7 +89,7 @@ else % nonlinear models if max(abs(feval(fh,dr.ys,[oo_.exo_steady_state; ... oo_.exo_det_steady_state], M_.params))) > options_.dynatol.f - [dr.ys,check1] = dynare_solve(fh,dr.ys,options_.jacobian_flag,... + [dr.ys,check1] = dynare_solve(fh,dr.ys,options_,... [oo_.exo_steady_state; ... oo_.exo_det_steady_state], M_.params); end diff --git a/matlab/partial_information/dr1_PI.m b/matlab/partial_information/dr1_PI.m index 996c02e9d..4230828b4 100644 --- a/matlab/partial_information/dr1_PI.m +++ b/matlab/partial_information/dr1_PI.m @@ -87,7 +87,7 @@ if options_.ramsey_policy && options_.ACES_solver == 0 end old_solve_algo = options_.solve_algo; % options_.solve_algo = 1; - oo_.steady_state = dynare_solve('ramsey_static',oo_.steady_state,0,M_,options_,oo_,it_); + oo_.steady_state = dynare_solve('ramsey_static',oo_.steady_state,options_,M_,options_,oo_,it_); options_.solve_algo = old_solve_algo; [junk,junk,multbar] = ramsey_static(oo_.steady_state,M_,options_,oo_,it_); [jacobia_,M_] = ramsey_dynamic(oo_.steady_state,multbar,M_,options_,oo_,it_);