Commit 70e162c7 authored by MichelJuillard's avatar MichelJuillard
Browse files

removed globals from solve1.m

parent 267b8fd9
This diff is collapsed.
......@@ -74,7 +74,9 @@ if options_.solve_algo == 0
end
elseif options_.solve_algo == 1
nn = size(x,1);
[x,info]=solve1(func,x,1:nn,1:nn,jacobian_flag,1,varargin{:});
[x,info]=solve1(func,x,1:nn,1:nn,jacobian_flag,1,options_.gstep, ...
options_.solve_tolf,options_.solve_tolx, ...
options_.solve_maxit,options_.debug,varargin{:});
elseif options_.solve_algo == 2 || options_.solve_algo == 4
nn = size(x,1) ;
tolf = options_.solve_tolf ;
......@@ -125,14 +127,19 @@ elseif options_.solve_algo == 2 || options_.solve_algo == 4
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]=solve1(func,x,j1(r(i):r(i+1)-1),j2(r(i):r(i+1)-1),jacobian_flag, bad_cond_flag, varargin{:});
[x,info]=solve1(func,x,j1(r(i):r(i+1)-1),j2(r(i):r(i+1)-1),jacobian_flag, ...
bad_cond_flag, options_.gstep, ...
options_.solve_tolf,options_.solve_tolx, ...
options_.solve_maxit,options_.debug,varargin{:});
if info
return
end
end
fvec = feval(func,x,varargin{:});
if max(abs(fvec)) > tolf
[x,info]=solve1(func,x,1:nn,1:nn,jacobian_flag, bad_cond_flag, varargin{:});
[x,info]=solve1(func,x,1:nn,1:nn,jacobian_flag, bad_cond_flag, ...
options_.gstep, options_.solve_tolf,options_.solve_tolx, ...
options_.solve_maxit,options_.debug,varargin{:});
end
elseif options_.solve_algo == 3
if jacobian_flag
......
......@@ -34,7 +34,11 @@ function sim1_purely_backward()
yb = oo_.endo_simul(:,it-1); % Values at previous period, also used as guess value for current period
yb1 = yb(iyb);
tmp = solve1(model_dynamic, [yb1; yb], 1:M_.endo_nbr, nyb+1:nyb+M_.endo_nbr, 1, 1, oo_.exo_simul, M_.params, oo_.steady_state, it);
tmp = solve1(model_dynamic, [yb1; yb], 1:M_.endo_nbr, nyb+1:nyb+ ...
M_.endo_nbr, 1, 1, options_.gstep, ...
options_.solve_tolf,options_.solve_tolx, ...
options_.solve_maxit,options_.debug,oo_.exo_simul, ...
M_.params, oo_.steady_state, it);
oo_.endo_simul(:,it) = tmp(nyb+1:nyb+M_.endo_nbr);
end
\ No newline at end of file
......@@ -33,7 +33,11 @@ function sim1_purely_forward()
yf = oo_.endo_simul(:,it+1); % Values at next period, also used as guess value for current period
yf1 = yf(iyf);
tmp = solve1(model_dynamic, [yf; yf1], 1:M_.endo_nbr, 1:M_.endo_nbr, 1, 1, oo_.exo_simul, M_.params, oo_.steady_state, it);
tmp = solve1(model_dynamic, [yf; yf1], 1:M_.endo_nbr, 1:M_.endo_nbr, ...
1, 1, options_.gstep, options_.solve_tolf, ...
options_.solve_tolx, options_.solve_maxit, ...
options_.debug,oo_.exo_simul, M_.params, oo_.steady_state, ...
it);
oo_.endo_simul(:,it) = tmp(1:M_.endo_nbr);
end
\ No newline at end of file
......@@ -104,6 +104,10 @@ DynareOutput.endo_simul(:,1) = DynareOutput.steady_state;
for it = 2:sample_size+1
y(jdx) = DynareOutput.endo_simul(:,it-1); % A good guess for the initial conditions is the previous values for the endogenous variables.
y(hdx) = y(jdx(iy1)); % Set lagged variables.
y(jdx) = solve1(model_dynamic, y, idx, jdx, 1, 1, DynareOutput.exo_simul, DynareModel.params, DynareOutput.steady_state, it);
y(jdx) = solve1(model_dynamic, y, idx, jdx, 1, 1, DynareOptions.gstep, ...
DynareOptions.solve_tolf,DynareOptions.solve_tolx, ...
DynareOptions.solve_maxit,DynareOptions.debug, ...
DynareOutput.exo_simul, DynareModel.params, ...
DynareOutput.steady_state, it);
DynareOutput.endo_simul(:,it) = y(jdx);
end
\ No newline at end of file
function [x,check] = solve1(func,x,j1,j2,jacobian_flag,bad_cond_flag,varargin)
function [x,check] = solve1(func,x,j1,j2,jacobian_flag,bad_cond_flag,gstep,tolf,tolx,maxit,debug,varargin)
% function [x,check] = solve1(func,x,j1,j2,jacobian_flag,bad_cond_flag,varargin)
% Solves systems of non linear equations of several variables
%
......@@ -11,6 +11,12 @@ function [x,check] = solve1(func,x,j1,j2,jacobian_flag,bad_cond_flag,varargin)
% jacobian_flag=0: jacobian obtained numerically
% bad_cond_flag=1: when Jacobian is badly conditionned, use an
% alternative formula to Newton step
% gstep increment multiplier in numercial derivative
% computation
% tolf tolerance for residuals
% tolx tolerance for solution variation
% maxit maximum number of iterations
% debug debug flag
% varargin: list of arguments following bad_cond_flag
%
% OUTPUTS
......@@ -37,18 +43,13 @@ function [x,check] = solve1(func,x,j1,j2,jacobian_flag,bad_cond_flag,varargin)
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
global M_ options_ fjac
nn = length(j1);
fjac = zeros(nn,nn) ;
g = zeros(nn,1) ;
tolf = options_.solve_tolf ;
tolx = options_.solve_tolx;
tolmin = tolx ;
stpmx = 100 ;
maxit = options_.solve_maxit ;
check = 0 ;
......@@ -77,7 +78,7 @@ for its = 1:maxit
fvec = fvec(j1);
fjac = fjac(j1,j2);
else
dh = max(abs(x(j2)),options_.gstep(1)*ones(nn,1))*eps^(1/3);
dh = max(abs(x(j2)),gstep(1)*ones(nn,1))*eps^(1/3);
for j = 1:nn
xdh = x ;
......@@ -89,33 +90,10 @@ for its = 1:maxit
end
g = (fvec'*fjac)';
if options_.debug
if debug
disp(['cond(fjac) ' num2str(cond(fjac))])
end
M_.unit_root = 0;
if M_.unit_root
if first_time
first_time = 0;
[q,r,e]=qr(fjac);
n = sum(abs(diag(r)) < 1e-12);
fvec = q'*fvec;
p = e*[-r(1:end-n,1:end-n)\fvec(1:end-n);zeros(n,1)];
disp(' ')
disp('STEADY with unit roots:')
disp(' ')
if n > 0
disp([' The following variable(s) kept their value given in INITVAL' ...
' or ENDVAL'])
disp(char(e(:,end-n+1:end)'*M_.endo_names))
else
disp(' STEADY can''t find any unit root!')
end
else
[q,r]=qr(fjac*e);
fvec = q'*fvec;
p = e*[-r(1:end-n,1:end-n)\fvec(1:end-n);zeros(n,1)];
end
elseif bad_cond_flag && cond(fjac) > 1/sqrt(eps)
if bad_cond_flag && rcond(fjac) < sqrt(eps)
fjac2=fjac'*fjac;
p=-(fjac2+1e6*sqrt(nn*eps)*max(sum(abs(fjac2)))*eye(nn))\(fjac'*fvec);
else
......@@ -126,7 +104,7 @@ for its = 1:maxit
[x,f,fvec,check]=lnsrch1(xold,fold,g,p,stpmax,func,j1,j2,varargin{:});
if options_.debug
if debug
disp([its f])
disp([xold x])
end
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment