From 2937471aedabcd98dacb66db3b29b157b6618d4b Mon Sep 17 00:00:00 2001 From: Michel Juillard <michel.juillard@mjui.fr> Date: Wed, 31 Oct 2012 11:08:22 +0100 Subject: [PATCH] corrected and clarified error messages when steady state computation fails (cherry-picked by hand from commit 48e00c578996b8bd098440b368e0c1faf646f75e) --- matlab/dynare_solve.m | 54 +++++++++++++++++++++++-------------------- matlab/print_info.m | 4 ++-- matlab/solve1.m | 6 +++-- 3 files changed, 35 insertions(+), 29 deletions(-) diff --git a/matlab/dynare_solve.m b/matlab/dynare_solve.m index 72555ed85..d020df370 100644 --- a/matlab/dynare_solve.m +++ b/matlab/dynare_solve.m @@ -35,7 +35,35 @@ function [x,info] = dynare_solve(func,x,jacobian_flag,varargin) global options_ +tolf = options_.solve_tolf ; info = 0; + +% checking initial values +if jacobian_flag + [fvec,fjac] = feval(func,x,varargin{:}); +else + fvec = feval(func,x,varargin{:}); + fjac = zeros(nn,nn) ; +end + +i = find(~isfinite(fvec)); + +if ~isempty(i) + disp(['STEADY: numerical initial values or parameters incompatible with the following' ... + ' equations']) + disp(i') + disp('Please check for example') + disp(' i) if all parameters occurring in these equations are defined') + disp(' ii) that no division by an endogenous variable initialized to 0 occurs') + info = 1; + x = NaN; + return; +end + +if max(abs(fvec)) < tolf + return ; +end + if options_.solve_algo == 0 if ~exist('OCTAVE_VERSION') if ~user_has_matlab_license('optimization_toolbox') @@ -60,7 +88,7 @@ if options_.solve_algo == 0 func = @(x) func2(x, varargin{:}); % The Octave version of fsolve does not converge when it starts from the solution fvec = feval(func,x); - if max(abs(fvec)) >= options_.solve_tolf + if max(abs(fvec)) >= tolf [x,fval,exitval,output] = fsolve(func,x,options); else exitval = 3; @@ -77,30 +105,6 @@ elseif options_.solve_algo == 1 [x,info]=solve1(func,x,1:nn,1:nn,jacobian_flag,1,varargin{:}); elseif options_.solve_algo == 2 || options_.solve_algo == 4 nn = size(x,1) ; - tolf = options_.solve_tolf ; - - if jacobian_flag - [fvec,fjac] = feval(func,x,varargin{:}); - else - fvec = feval(func,x,varargin{:}); - fjac = zeros(nn,nn) ; - end - - i = find(~isfinite(fvec)); - - if ~isempty(i) - disp(['STEADY: numerical initial values incompatible with the following' ... - ' equations']) - disp(i') - disp('Please check for example') - disp(' i) if all parameters occurring in these equations are defined') - disp(' ii) that no division by an endogenous variable initialized to 0 occurs') - error('exiting ...') - end - - if max(abs(fvec)) < tolf - return ; - end if ~jacobian_flag fjac = zeros(nn,nn) ; diff --git a/matlab/print_info.m b/matlab/print_info.m index b406e964a..e19738381 100644 --- a/matlab/print_info.m +++ b/matlab/print_info.m @@ -55,8 +55,8 @@ if ~noprint error('The steadystate file did not compute the steady state') case 20 error(['Impossible to find the steady state. Either the model' ... - ' doesn''t have a unique steady state of the guess values' ... - ' are too far from the solution']) + ' doesn''t have a steady state or there is an infinity of steady states' ... + ' or the guess values are too far from the solution']) case 21 error('The steady state is complex') case 22 diff --git a/matlab/solve1.m b/matlab/solve1.m index 74111276f..83d3efb12 100644 --- a/matlab/solve1.m +++ b/matlab/solve1.m @@ -58,9 +58,11 @@ fvec = fvec(j1); i = find(~isfinite(fvec)); if ~isempty(i) - disp(['STEADY: numerical initial values incompatible with the following' ... - ' equations']) + disp(['SOLVE1: during the resolution of the non-linear system, the evaluation of the following ' ... + 'equation(s) resulted in a non-finite number:']) disp(j1(i)') + check = 1; + return; end f = 0.5*fvec'*fvec ; -- GitLab