1function [endogenousvariables, info] = solve_stacked_linear_problem(endogenousvariables, exogenousvariables, steadystate_y, steadystate_x, M, options)
2
3% Copyright (C) 2015-2019 Dynare Team
4%
5% This file is part of Dynare.
6%
7% Dynare is free software: you can redistribute it and/or modify
8% it under the terms of the GNU General Public License as published by
9% the Free Software Foundation, either version 3 of the License, or
10% (at your option) any later version.
11%
12% Dynare is distributed in the hope that it will be useful,
13% but WITHOUT ANY WARRANTY; without even the implied warranty of
14% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15% GNU General Public License for more details.
16%
17% You should have received a copy of the GNU General Public License
18% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
19
20[options, y0, yT, z, i_cols, i_cols_J1, i_cols_T, i_cols_j, i_cols_1, i_cols_0, i_cols_J0, dynamicmodel] = ...
21    initialize_stacked_problem(endogenousvariables, options, M, steadystate_y);
22
23ip = find(M.lead_lag_incidence(1,:)');
24ic = find(M.lead_lag_incidence(2,:)');
25in = find(M.lead_lag_incidence(3,:)');
26
27% Evaluate the Jacobian of the dynamic model at the deterministic steady state.
28[d1,jacobian] = dynamicmodel(steadystate_y([ip; ic; in]), transpose(steadystate_x), M.params, steadystate_y, 1);
29
30% Check that the dynamic model was evaluated at the steady state.
31if max(abs(d1))>1e-12
32    error('Jacobian is not evaluated at the steady state!')
33end
34
35nyp = nnz(M.lead_lag_incidence(1,:));
36ny0 = nnz(M.lead_lag_incidence(2,:));
37nyf = nnz(M.lead_lag_incidence(3,:));
38nd = nyp+ny0+nyf; % size of y (first argument passed to the dynamic file).
39jexog = transpose(nd+(1:M.exo_nbr));
40jendo = transpose(1:nd);
41z = bsxfun(@minus, z, steadystate_y);
42x = bsxfun(@minus, exogenousvariables, steadystate_x');
43
44[y, check] = dynare_solve(@linear_perfect_foresight_problem,z(:), options, ...
45                          jacobian, y0-steadystate_y, yT-steadystate_y, ...
46                          x, M.params, steadystate_y, ...
47                          M.maximum_lag, options.periods, M.endo_nbr, i_cols, ...
48                          i_cols_J1, i_cols_1, i_cols_T, i_cols_j, i_cols_0, i_cols_J0, ...
49                          jendo, jexog);
50
51if all(imag(y)<.1*options.dynatol.x)
52    if ~isreal(y)
53        y = real(y);
54    end
55else
56    check = 1;
57end
58
59endogenousvariables = [y0 bsxfun(@plus,reshape(y,M.endo_nbr,options.periods), steadystate_y) yT];
60
61if check
62    info.status = false;
63else
64    info.status = true;
65end
66