diff --git a/matlab/perfect-foresight-models/perfect_foresight_solver.m b/matlab/perfect-foresight-models/perfect_foresight_solver.m index 09b3bf435..b5fc7e4f2 100644 --- a/matlab/perfect-foresight-models/perfect_foresight_solver.m +++ b/matlab/perfect-foresight-models/perfect_foresight_solver.m @@ -173,7 +173,31 @@ if ~oo_.deterministic_simulation.status && ~options_.no_homotopy warning(warning_old_state); end -if oo_.deterministic_simulation.status == 1 + +if ~isreal(oo_.endo_simul(:)) %can only happen without bytecode + y0 = real(oo_.endo_simul(:,1)); + yT = real(oo_.endo_simul(:,options_.periods+2)); + yy = real(oo_.endo_simul(:,2:options_.periods+1)); + illi = M_.lead_lag_incidence'; + [i_cols,junk,i_cols_j] = find(illi(:)); + illi = illi(:,2:3); + [i_cols_J1,junk,i_cols_1] = find(illi(:)); + i_cols_T = nonzeros(M_.lead_lag_incidence(1:2,:)'); + residuals = perfect_foresight_problem(yy(:),str2func([M_.fname '_dynamic']), y0, yT, ... + oo_.exo_simul,M_.params,oo_.steady_state, ... + M_.maximum_lag,options_.periods,M_.endo_nbr,i_cols, ... + i_cols_J1, i_cols_1, i_cols_T, i_cols_j, ... + M_.NNZDerivatives(1)); + if max(abs(residuals))< options_.dynatol.f + oo_.deterministic_simulation.status = 1; + oo_.endo_simul=real(oo_.endo_simul); + else + oo_.deterministic_simulation.status = 0; + disp('Simulation terminated with imaginary parts in the residuals or endogenous variables.') + end +end + +if oo_.deterministic_simulation.status == 1 disp('Perfect foresight solution found.') else disp('Failed to solve perfect foresight model') diff --git a/matlab/perfect-foresight-models/sim1.m b/matlab/perfect-foresight-models/sim1.m index 0f92ca8ed..ccb8907d8 100644 --- a/matlab/perfect-foresight-models/sim1.m +++ b/matlab/perfect-foresight-models/sim1.m @@ -186,7 +186,7 @@ end if stop - if any(isnan(res)) || any(isinf(res)) || any(isnan(Y)) || any(isinf(Y)) || ~isreal(res) || ~isreal(Y) + if any(isnan(res)) || any(isinf(res)) || any(isnan(Y)) || any(isinf(Y)) oo.deterministic_simulation.status = false;% NaN or Inf occurred oo.deterministic_simulation.error = err; oo.deterministic_simulation.iterations = iter; @@ -195,11 +195,7 @@ if stop if verbose skipline() disp(sprintf('Total time of simulation: %s.', num2str(etime(clock,h1)))) - if ~isreal(res) || ~isreal(Y) - disp('Simulation terminated with imaginary parts in the residuals or endogenous variables.') - else - disp('Simulation terminated with NaN or Inf in the residuals or endogenous variables.') - end + disp('Simulation terminated with NaN or Inf in the residuals or endogenous variables.') disp('There is most likely something wrong with your model. Try model_diagnostics or another simulation method.') printline(105) end