2022-04-09 16:03:37 +02:00
|
|
|
function [x, errorflag, errorcode] = solve1(func, x, j1, j2, jacobian_flag, gstep, tolf, tolx, maxit, fake, debug, varargin)
|
2022-03-03 20:24:18 +01:00
|
|
|
|
2008-01-03 13:21:25 +01:00
|
|
|
% Solves systems of non linear equations of several variables
|
|
|
|
%
|
|
|
|
% INPUTS
|
|
|
|
% func: name of the function to be solved
|
|
|
|
% x: guess values
|
|
|
|
% j1: equations index for which the model is solved
|
|
|
|
% j2: unknown variables index
|
2019-03-19 14:26:16 +01:00
|
|
|
% jacobian_flag=true: jacobian given by the 'func' function
|
|
|
|
% jacobian_flag=false: jacobian obtained numerically
|
2012-08-06 23:32:15 +02:00
|
|
|
% gstep increment multiplier in numercial derivative
|
|
|
|
% computation
|
|
|
|
% tolf tolerance for residuals
|
|
|
|
% tolx tolerance for solution variation
|
|
|
|
% maxit maximum number of iterations
|
2021-07-22 16:46:10 +02:00
|
|
|
% fake unused argument (compatibity with trust_region).
|
2012-08-06 23:32:15 +02:00
|
|
|
% debug debug flag
|
2014-02-04 17:46:54 +01:00
|
|
|
% varargin: list of extra arguments to the function
|
2017-05-16 15:10:20 +02:00
|
|
|
%
|
2008-01-03 13:21:25 +01:00
|
|
|
% OUTPUTS
|
|
|
|
% x: results
|
2022-03-03 20:24:18 +01:00
|
|
|
% errorflag=1: the model can not be solved
|
2008-01-03 13:21:25 +01:00
|
|
|
%
|
|
|
|
% SPECIAL REQUIREMENTS
|
|
|
|
% none
|
|
|
|
|
2022-03-03 20:24:18 +01:00
|
|
|
% Copyright © 2001-2022 Dynare Team
|
2008-08-01 14:40:33 +02:00
|
|
|
%
|
|
|
|
% This file is part of Dynare.
|
|
|
|
%
|
|
|
|
% Dynare is free software: you can redistribute it and/or modify
|
|
|
|
% it under the terms of the GNU General Public License as published by
|
|
|
|
% the Free Software Foundation, either version 3 of the License, or
|
|
|
|
% (at your option) any later version.
|
|
|
|
%
|
|
|
|
% Dynare is distributed in the hope that it will be useful,
|
|
|
|
% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
% GNU General Public License for more details.
|
|
|
|
%
|
|
|
|
% You should have received a copy of the GNU General Public License
|
2021-06-09 17:33:48 +02:00
|
|
|
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
2008-01-03 13:21:25 +01:00
|
|
|
|
2009-12-16 18:17:34 +01:00
|
|
|
nn = length(j1);
|
|
|
|
g = zeros(nn,1) ;
|
2005-02-18 20:54:39 +01:00
|
|
|
|
2009-12-16 18:17:34 +01:00
|
|
|
tolmin = tolx ;
|
2005-02-18 20:54:39 +01:00
|
|
|
|
2009-12-16 18:17:34 +01:00
|
|
|
stpmx = 100 ;
|
2005-02-18 20:54:39 +01:00
|
|
|
|
2022-03-03 20:24:18 +01:00
|
|
|
errorflag = false ;
|
2005-02-18 20:54:39 +01:00
|
|
|
|
2009-12-16 18:17:34 +01:00
|
|
|
fvec = feval(func,x,varargin{:});
|
|
|
|
fvec = fvec(j1);
|
|
|
|
|
2022-03-03 18:30:36 +01:00
|
|
|
idInf = isinf(fvec);
|
2022-03-03 18:33:34 +01:00
|
|
|
idNan = isnan(fvec);
|
|
|
|
idCpx = ~isreal(fvec);
|
2009-12-16 18:17:34 +01:00
|
|
|
|
2022-03-03 18:30:36 +01:00
|
|
|
if any(idInf)
|
|
|
|
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(idInf)')
|
2022-04-09 16:03:37 +02:00
|
|
|
errorcode = 0;
|
2022-03-03 20:24:18 +01:00
|
|
|
errorflag = true;
|
2017-05-16 12:42:01 +02:00
|
|
|
return
|
2009-12-16 18:17:34 +01:00
|
|
|
end
|
|
|
|
|
2022-03-03 18:33:34 +01:00
|
|
|
if any(idNan)
|
|
|
|
disp('SOLVE1: during the resolution of the non-linear system, the evaluation of the following equation(s) resulted in a nan:')
|
|
|
|
disp(j1(idNan)')
|
2022-04-09 16:03:37 +02:00
|
|
|
errorcode = 0;
|
2022-03-03 20:24:18 +01:00
|
|
|
errorflag = true;
|
2022-03-03 18:33:34 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if any(idNan)
|
|
|
|
disp('SOLVE1: during the resolution of the non-linear system, the evaluation of the following equation(s) resulted in a complex number:')
|
|
|
|
disp(j1(idCpx)')
|
2022-04-09 16:03:37 +02:00
|
|
|
errorcode = 0;
|
2022-03-03 20:24:18 +01:00
|
|
|
errorflag = true;
|
2022-03-03 18:33:34 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-03-03 20:24:18 +01:00
|
|
|
f = 0.5*(fvec'*fvec);
|
2022-03-03 18:33:34 +01:00
|
|
|
|
2022-03-03 20:24:18 +01:00
|
|
|
if max(abs(fvec))<tolf*tolf
|
|
|
|
% Initial guess is a solution
|
2022-04-09 16:03:37 +02:00
|
|
|
errorcode = -1;
|
2022-03-03 20:24:18 +01:00
|
|
|
return
|
2009-12-16 18:17:34 +01:00
|
|
|
end
|
2005-02-18 20:54:39 +01:00
|
|
|
|
2009-12-16 18:17:34 +01:00
|
|
|
stpmax = stpmx*max([sqrt(x'*x);nn]) ;
|
|
|
|
first_time = 1;
|
2014-05-11 14:44:06 +02:00
|
|
|
if ~jacobian_flag
|
2022-03-03 20:24:18 +01:00
|
|
|
fjac = zeros(nn,nn);
|
2014-05-11 14:44:06 +02:00
|
|
|
end
|
2009-12-16 18:17:34 +01:00
|
|
|
for its = 1:maxit
|
2006-01-18 17:50:33 +01:00
|
|
|
if jacobian_flag
|
2022-03-03 20:24:18 +01:00
|
|
|
[fvec,fjac] = feval(func,x, varargin{:});
|
2009-12-16 18:17:34 +01:00
|
|
|
fvec = fvec(j1);
|
|
|
|
fjac = fjac(j1,j2);
|
2022-03-03 20:26:06 +01:00
|
|
|
g = (fvec'*fjac)';
|
2006-01-18 17:50:33 +01:00
|
|
|
else
|
2012-08-06 23:32:15 +02:00
|
|
|
dh = max(abs(x(j2)),gstep(1)*ones(nn,1))*eps^(1/3);
|
2009-12-16 18:17:34 +01:00
|
|
|
for j = 1:nn
|
2022-03-03 20:24:18 +01:00
|
|
|
xdh = x;
|
|
|
|
xdh(j2(j)) = xdh(j2(j))+dh(j);
|
2009-12-16 18:17:34 +01:00
|
|
|
t = feval(func,xdh,varargin{:});
|
2022-03-03 20:24:18 +01:00
|
|
|
fjac(:,j) = (t(j1) - fvec)./dh(j);
|
|
|
|
g(j) = fvec'*fjac(:,j);
|
2009-12-16 18:17:34 +01:00
|
|
|
end
|
2006-01-18 17:50:33 +01:00
|
|
|
end
|
2012-08-06 23:32:15 +02:00
|
|
|
if debug
|
2014-05-07 21:12:56 +02:00
|
|
|
disp(['cond(fjac) ' num2str(condest(fjac))])
|
2005-02-18 20:54:39 +01:00
|
|
|
end
|
2014-05-07 21:12:56 +02:00
|
|
|
if issparse(fjac)
|
|
|
|
rcond_fjac = 1/condest(fjac);
|
|
|
|
else
|
|
|
|
rcond_fjac = rcond(fjac);
|
|
|
|
end
|
|
|
|
if rcond_fjac < sqrt(eps)
|
2012-05-30 10:25:10 +02:00
|
|
|
fjac2=fjac'*fjac;
|
2022-05-25 14:21:28 +02:00
|
|
|
temp=max(sum(abs(fjac2)));
|
|
|
|
if temp>0
|
|
|
|
p=-(fjac2+sqrt(nn*eps)*temp*eye(nn))\(fjac'*fvec);
|
|
|
|
else
|
|
|
|
errorflag = true;
|
|
|
|
errorcode = 5;
|
|
|
|
if nargout<3
|
|
|
|
skipline()
|
|
|
|
dprintf('SOLVE: Iteration %s', num2str(its))
|
|
|
|
disp('Zero Jacobian.')
|
|
|
|
skipline()
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
2005-02-18 20:54:39 +01:00
|
|
|
else
|
2009-12-16 18:17:34 +01:00
|
|
|
p = -fjac\fvec ;
|
2005-02-18 20:54:39 +01:00
|
|
|
end
|
|
|
|
xold = x ;
|
|
|
|
fold = f ;
|
|
|
|
|
2022-03-03 20:24:18 +01:00
|
|
|
[x, f, fvec, lnsearchflag] = lnsrch1(xold, fold, g, p, stpmax, func, j1, j2, tolx, varargin{:});
|
2005-02-18 20:54:39 +01:00
|
|
|
|
2012-08-06 23:32:15 +02:00
|
|
|
if debug
|
2009-12-16 18:17:34 +01:00
|
|
|
disp([its f])
|
|
|
|
disp([xold x])
|
2005-02-18 20:54:39 +01:00
|
|
|
end
|
2017-05-16 15:10:20 +02:00
|
|
|
|
2022-03-03 20:24:18 +01:00
|
|
|
if lnsearchflag
|
|
|
|
errorflag = true;
|
2009-12-16 18:17:34 +01:00
|
|
|
den = max([f;0.5*nn]) ;
|
|
|
|
if max(abs(g).*max([abs(x(j2)') ones(1,nn)])')/den < tolmin
|
2016-03-12 19:43:08 +01:00
|
|
|
if max(abs(x(j2)-xold(j2))./max([abs(x(j2)') ones(1,nn)])') < tolx
|
2022-04-09 16:03:37 +02:00
|
|
|
errorcode = 3;
|
2022-03-03 22:30:38 +01:00
|
|
|
if nargout<3
|
|
|
|
skipline()
|
|
|
|
dprintf('SOLVE: Iteration %s', num2str(its))
|
|
|
|
disp('Convergence on dX.')
|
|
|
|
skipline()
|
|
|
|
end
|
2016-03-12 19:43:08 +01:00
|
|
|
return
|
|
|
|
end
|
2009-12-16 18:17:34 +01:00
|
|
|
else
|
2022-04-09 16:03:37 +02:00
|
|
|
errorcode = 4;
|
2022-03-03 22:30:38 +01:00
|
|
|
if nargout<3
|
|
|
|
skipline()
|
|
|
|
dprintf('SOLVE: Iteration %s', num2str(its))
|
|
|
|
disp('Spurious convergence.')
|
|
|
|
disp(x)
|
|
|
|
end
|
2010-01-05 11:46:10 +01:00
|
|
|
return
|
2009-12-16 18:17:34 +01:00
|
|
|
end
|
2007-06-25 16:48:55 +02:00
|
|
|
elseif max(abs(fvec)) < tolf
|
2022-04-09 16:03:37 +02:00
|
|
|
errorcode = 1;
|
2009-12-16 18:17:34 +01:00
|
|
|
return
|
2005-02-18 20:54:39 +01:00
|
|
|
end
|
2009-12-16 18:17:34 +01:00
|
|
|
end
|
|
|
|
|
2022-03-03 20:24:18 +01:00
|
|
|
errorflag = true;
|
2022-04-09 16:03:37 +02:00
|
|
|
errorcode = 2;
|
2005-02-18 20:54:39 +01:00
|
|
|
|
2022-03-03 22:30:38 +01:00
|
|
|
if nargout<3
|
|
|
|
skipline()
|
|
|
|
disp('SOLVE: maxit has been reached')
|
|
|
|
end
|
2022-04-09 16:03:37 +02:00
|
|
|
|
2005-02-18 20:54:39 +01:00
|
|
|
% 01/14/01 MJ lnsearch is now a separate function
|
|
|
|
% 01/16/01 MJ added varargin to function evaluation
|
|
|
|
% 04/13/01 MJ added test f < tolf !!
|
|
|
|
% 05/11/01 MJ changed tests for 'check' so as to remove 'continue' which is
|
|
|
|
% an instruction which appears only in version 6
|