1function [M,oo,info,ip,ix,ixd] = homotopy1(values, step_nbr, M, options, oo)
2% function homotopy1(values, step_nbr)
3%
4% Implements homotopy (mode 1) for steady-state computation.
5% The multi-dimensional vector going from the set of initial values
6% to the set of final values is divided in as many sub-vectors as
7% there are steps, and the problem is solved as many times.
8%
9% INPUTS
10%    values:        a matrix with 4 columns, representing the content of
11%                   homotopy_setup block, with one variable per line.
12%                   Column 1 is variable type (1 for exogenous, 2 for
13%                   exogenous deterministic, 4 for parameters)
14%                   Column 2 is symbol integer identifier.
15%                   Column 3 is initial value, and column 4 is final value.
16%                   Column 3 can contain NaNs, in which case previous
17%                   initialization of variable will be used as initial value.
18%    step_nbr:      number of steps for homotopy
19%    M              struct of model parameters
20%    options        struct of options
21%    oo             struct of outputs
22%
23% OUTPUTS
24%    M              struct of model parameters
25%    oo             struct of outputs
26%    ip             index of parameters
27%    ix             index of exogenous variables
28%    ixp            index of exogenous deterministic variables
29%
30% SPECIAL REQUIREMENTS
31%    none
32
33% Copyright (C) 2008-2017 Dynare Team
34%
35% This file is part of Dynare.
36%
37% Dynare is free software: you can redistribute it and/or modify
38% it under the terms of the GNU General Public License as published by
39% the Free Software Foundation, either version 3 of the License, or
40% (at your option) any later version.
41%
42% Dynare is distributed in the hope that it will be useful,
43% but WITHOUT ANY WARRANTY; without even the implied warranty of
44% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
45% GNU General Public License for more details.
46%
47% You should have received a copy of the GNU General Public License
48% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
49
50nv = size(values, 1);
51
52ip = find(values(:,1) == 4); % Parameters
53ix = find(values(:,1) == 1); % Exogenous
54ixd = find(values(:,1) == 2); % Exogenous deterministic
55
56if length([ip; ix; ixd]) ~= nv
57    error('HOMOTOPY mode 1: incorrect variable types specified')
58end
59
60% Construct vector of starting values, using previously initialized values
61% when initial value has not been given in homotopy_setup block
62oldvalues = values(:,3);
63ipn = find(values(:,1) == 4 & isnan(oldvalues));
64oldvalues(ipn) = M.params(values(ipn, 2));
65ixn = find(values(:,1) == 1 & isnan(oldvalues));
66oldvalues(ixn) = oo.exo_steady_state(values(ixn, 2));
67ixdn = find(values(:,1) == 2 & isnan(oldvalues));
68oldvalues(ixdn) = oo.exo_det_steady_state(values(ixdn, 2));
69
70points = zeros(nv, step_nbr+1);
71for i = 1:nv
72    if (oldvalues(i) ~= values(i, 4))
73        points(i,:) = oldvalues(i):(values(i,4)-oldvalues(i))/step_nbr:values(i,4);
74    else
75        points(i,:) = values(i,4);
76    end
77end
78
79for i=1:step_nbr+1
80    disp([ 'HOMOTOPY mode 1: computing step ' int2str(i-1) '/' int2str(step_nbr) '...' ])
81    old_params = M.params;
82    old_exo = oo.exo_steady_state;
83    old_exo_det = oo.exo_det_steady_state;
84    M.params(values(ip,2)) = points(ip,i);
85    oo.exo_steady_state(values(ix,2)) = points(ix,i);
86    oo.exo_det_steady_state(values(ixd,2)) = points(ixd,i);
87
88    [steady_state,M.params,info] = steady_(M,options,oo);
89    if info(1) == 0
90        % if homotopy step is not successful, current values of steady
91        % state are not modified
92        oo.steady_state = steady_state;
93    else
94        M.params = old_params;
95        oo.exo_steady_state = old_exo;
96        oo.exo_det_steady_state = old_exo_det;
97        break
98    end
99end
100