diff --git a/matlab/evaluate_max_dynamic_residual.m b/matlab/evaluate_max_dynamic_residual.m new file mode 100644 index 0000000000000000000000000000000000000000..41aa4ec249c2fae2dcfb9721832e2aee20bcedef --- /dev/null +++ b/matlab/evaluate_max_dynamic_residual.m @@ -0,0 +1,33 @@ +function err = evaluate_max_dynamic_residual(model_dynamic, Y, exogenous_variables, params, steady_state, periods, ny, max_lag, lead_lag_incidence) + +% Copyright (C) 2013 Dynare Team +% +% 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 +% along with Dynare. If not, see <http://www.gnu.org/licenses/>. + +i_rows = 1:ny; +i_cols = find(lead_lag_incidence'); + +err = 0; + +for it = (max_lag+1):(max_lag+periods) + d = model_dynamic(Y(i_cols), exogenous_variables, params, steady_state, it); + i_rows = i_rows + ny; + i_cols = i_cols + ny; + r = max(abs(d)); + if r>err + err = r; + end +end \ No newline at end of file diff --git a/matlab/sim1.m b/matlab/sim1.m index fcf445a4de740ce88f62ff5be7c26a75c6779ab8..ac01d2beb1d7ea78f4229f8478e892d419dd8a1a 100644 --- a/matlab/sim1.m +++ b/matlab/sim1.m @@ -72,6 +72,7 @@ i_upd = ny+(1:periods*ny); Y = endo_simul(:); +skipline() disp (['-----------------------------------------------------']) ; fprintf('MODEL SIMULATION:\n'); @@ -146,6 +147,10 @@ for iter = 1:options_.simul.maxit end +if endogenous_terminal_period + err = evaluate_max_dynamic_residual(model_dynamic, Y, oo_.exo_simul, params, steady_state, periods, ny, max_lag, lead_lag_incidence); +end + if stop if any(isnan(res)) || any(isinf(res)) || any(isnan(Y)) || any(isinf(Y)) @@ -156,13 +161,14 @@ if stop oo_.endo_simul = reshape(Y,ny,periods+2); skipline(); fprintf('\nSimulation terminated after %d iterations.\n',iter); - fprintf('Total time of simulation : %10.3f\n',etime(clock,h1)); + fprintf('Total time of simulation: %16.13f\n',etime(clock,h1)); error('Simulation terminated with NaN or Inf in the residuals or endogenous variables. There is most likely something wrong with your model.'); else skipline(); fprintf('\nSimulation concluded successfully after %d iterations.\n',iter); - fprintf('Total time of simulation : %10.3f\n',etime(clock,h1)); - fprintf('Convergency obtained.\n'); + fprintf('Total time of simulation: %16.13f\n',etime(clock,h1)); + fprintf('Max. Abs. Error : %16.13f\n',err); + fprintf('Convergency obtained!\n'); oo_.deterministic_simulation.status = 1;% Convergency obtained. oo_.deterministic_simulation.error = err; oo_.deterministic_simulation.iterations = iter; @@ -172,12 +178,12 @@ if stop elseif ~stop skipline(); fprintf('\nSimulation terminated after %d iterations.\n',iter); - fprintf('Total time of simulation : %10.3f\n',etime(clock,h1)); + fprintf('Total time of simulation: %16.13f\n',etime(clock,h1)); + fprintf('Max. Abs. Error : %16.13f\n',err); fprintf('WARNING : maximum number of iterations is reached (modify options_.simul.maxit).\n') ; oo_.deterministic_simulation.status = 0;% more iterations are needed. oo_.deterministic_simulation.error = err; oo_.deterministic_simulation.periods = vperiods(1:iter); - %oo_.deterministic_simulation.errors = c/abs(err) oo_.deterministic_simulation.iterations = options_.simul.maxit; end disp (['-----------------------------------------------------']) ;