Home > matlab > sim1.m

sim1

PURPOSE ^

function sim1

SYNOPSIS ^

function sim1

DESCRIPTION ^

 function sim1
 performs deterministic simulations with lead or lag on one period

 INPUTS
   ...
 OUTPUTS
   ...
 ALGORITHM
   Laffargue, Boucekkine, Juillard (LBJ)
   see Juillard (1996) Dynare: A program for the resolution and
   simulation of dynamic models with forward variables through the use
   of a relaxation algorithm. CEPREMAP. Couverture Orange. 9602.

 SPECIAL REQUIREMENTS
   None.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function sim1
0002 % function sim1
0003 % performs deterministic simulations with lead or lag on one period
0004 %
0005 % INPUTS
0006 %   ...
0007 % OUTPUTS
0008 %   ...
0009 % ALGORITHM
0010 %   Laffargue, Boucekkine, Juillard (LBJ)
0011 %   see Juillard (1996) Dynare: A program for the resolution and
0012 %   simulation of dynamic models with forward variables through the use
0013 %   of a relaxation algorithm. CEPREMAP. Couverture Orange. 9602.
0014 %
0015 % SPECIAL REQUIREMENTS
0016 %   None.
0017 
0018 % Copyright (C) 1996-2010 Dynare Team
0019 %
0020 % This file is part of Dynare.
0021 %
0022 % Dynare is free software: you can redistribute it and/or modify
0023 % it under the terms of the GNU General Public License as published by
0024 % the Free Software Foundation, either version 3 of the License, or
0025 % (at your option) any later version.
0026 %
0027 % Dynare is distributed in the hope that it will be useful,
0028 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0029 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0030 % GNU General Public License for more details.
0031 %
0032 % You should have received a copy of the GNU General Public License
0033 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
0034 
0035 global M_ options_ oo_
0036 
0037 lead_lag_incidence = M_.lead_lag_incidence;
0038 
0039 ny = size(oo_.endo_simul,1) ;
0040 nyp = nnz(lead_lag_incidence(1,:)) ;
0041 nyf = nnz(lead_lag_incidence(3,:)) ;
0042 nrs = ny+nyp+nyf+1 ;
0043 nrc = nyf+1 ;
0044 iyf = find(lead_lag_incidence(3,:)>0) ;
0045 iyp = find(lead_lag_incidence(1,:)>0) ;
0046 isp = [1:nyp] ;
0047 is = [nyp+1:ny+nyp] ;
0048 isf = iyf+nyp ;
0049 isf1 = [nyp+ny+1:nyf+nyp+ny+1] ;
0050 stop = 0 ;
0051 iz = [1:ny+nyp+nyf];
0052 
0053 disp (['-----------------------------------------------------']) ;
0054 disp (['MODEL SIMULATION :']) ;
0055 fprintf('\n') ;
0056 
0057 it_init = M_.maximum_lag+1 ;
0058 
0059 h1 = clock ;
0060 for iter = 1:options_.maxit_
0061     h2 = clock ;
0062     
0063     if options_.terminal_condition == 0
0064         c = zeros(ny*options_.periods,nrc) ;
0065     else
0066         c = zeros(ny*(options_.periods+1),nrc) ;
0067     end
0068     
0069     it_ = it_init ;
0070     z = [oo_.endo_simul(iyp,it_-1) ; oo_.endo_simul(:,it_) ; oo_.endo_simul(iyf,it_+1)] ;
0071     [d1,jacobian] = feval([M_.fname '_dynamic'],z,oo_.exo_simul, M_.params, oo_.steady_state,it_);
0072     jacobian = [jacobian(:,iz) -d1] ;
0073     ic = [1:ny] ;
0074     icp = iyp ;
0075     c (ic,:) = jacobian(:,is)\jacobian(:,isf1) ;
0076     for it_ = it_init+(1:options_.periods-1)
0077         z = [oo_.endo_simul(iyp,it_-1) ; oo_.endo_simul(:,it_) ; oo_.endo_simul(iyf,it_+1)] ;
0078         [d1,jacobian] = feval([M_.fname '_dynamic'],z,oo_.exo_simul, ...
0079                               M_.params, oo_.steady_state, it_);
0080         jacobian = [jacobian(:,iz) -d1] ;
0081         jacobian(:,[isf nrs]) = jacobian(:,[isf nrs])-jacobian(:,isp)*c(icp,:) ;
0082         ic = ic + ny ;
0083         icp = icp + ny ;
0084         c (ic,:) = jacobian(:,is)\jacobian(:,isf1) ;
0085     end
0086     
0087     if options_.terminal_condition == 1
0088         s = eye(ny) ;
0089         s(:,isf) = s(:,isf)+c(ic,1:nyf) ;
0090         ic = ic + ny ;
0091         c(ic,nrc) = s\c(ic,nrc) ;
0092         c = bksup1(c,ny,nrc,iyf,options_.periods) ;
0093         c = reshape(c,ny,options_.periods+1) ;
0094         oo_.endo_simul(:,it_init+(0:options_.periods)) = oo_.endo_simul(:,it_init+(0:options_.periods))+options_.slowc*c ;
0095     else
0096         c = bksup1(c,ny,nrc,iyf,options_.periods) ;
0097         c = reshape(c,ny,options_.periods) ;
0098         oo_.endo_simul(:,it_init+(0:options_.periods-1)) = oo_.endo_simul(:,it_init+(0:options_.periods-1))+options_.slowc*c ;
0099     end
0100     
0101     err = max(max(abs(c./options_.scalv')));
0102     disp([num2str(iter) ' -     err = ' num2str(err)]) ;
0103     disp(['     Time of iteration       :' num2str(etime(clock,h2))]) ;
0104     
0105     if err < options_.dynatol.f
0106         stop = 1 ;
0107         fprintf('\n') ;
0108         disp([' Total time of simulation        :' num2str(etime(clock,h1))]) ;
0109         fprintf('\n') ;
0110         disp([' Convergency obtained.']) ;
0111         fprintf('\n') ;
0112         oo_.deterministic_simulation.status = 1;% Convergency obtained.
0113         oo_.deterministic_simulation.error = err;
0114         oo_.deterministic_simulation.iterations = iter;
0115         break
0116     end
0117 end
0118 
0119 if ~stop
0120     fprintf('\n') ;
0121     disp(['     Total time of simulation        :' num2str(etime(clock,h1))]) ;
0122     fprintf('\n') ;
0123     disp(['WARNING : maximum number of iterations is reached (modify options_.maxit_).']) ;
0124     fprintf('\n') ;
0125     oo_.deterministic_simulation.status = 0;% more iterations are needed.
0126     oo_.deterministic_simulation.error = err;
0127     oo_.deterministic_simulation.errors = c/abs(err);    
0128     oo_.deterministic_simulation.iterations = options_.maxit_;
0129 end
0130 disp (['-----------------------------------------------------']) ;
0131

Generated on Tue 22-May-2012 02:40:23 by m2html © 2005