1function [X,info] = quadratic_matrix_equation_solver(A,B,C,tol,maxit,line_search_flag,X) 2 3%@info: 4%! @deftypefn {Function File} {[@var{X1}, @var{info}] =} quadratic_matrix_equation_solver (@var{A},@var{B},@var{C},@var{tol},@var{maxit},@var{line_search_flag},@var{X0}) 5%! @anchor{logarithmic_reduction} 6%! @sp 1 7%! Solves the quadratic matrix equation AX^2 + BX + C = 0 with a Newton algorithm. 8%! @sp 2 9%! @strong{Inputs} 10%! @sp 1 11%! @table @ @var 12%! @item A 13%! Square matrix of doubles, n*n. 14%! @item B 15%! Square matrix of doubles, n*n. 16%! @item C 17%! Square matrix of doubles, n*n. 18%! @item tol 19%! Scalar double, tolerance parameter. 20%! @item maxit 21%! Scalar integer, maximum number of iterations. 22%! @item line_search_flag 23%! Scalar integer, if nonzero an exact line search algorithm is used. 24%! @item X 25%! Square matrix of doubles, n*n, initial condition. 26%! @end table 27%! @sp 1 28%! @strong{Outputs} 29%! @sp 1 30%! @table @ @var 31%! @item X 32%! Square matrix of doubles, n*n, solution of the matrix equation. 33%! @item info 34%! Scalar integer, if nonzero the algorithm failed in finding the solution of the matrix equation. 35%! @end table 36%! @sp 2 37%! @strong{This function is called by:} 38%! @sp 2 39%! @strong{This function calls:} 40%! @sp 1 41%! @ref{fastgensylv} 42%! @sp 2 43%! @strong{References:} 44%! @sp 1 45%! N.J. Higham and H.-M. Kim (2001), "Solving a quadratic matrix equation by Newton's method with exact line searches.", in SIAM J. Matrix Anal. Appl., Vol. 23, No. 3, pp. 303-316. 46%! @sp 2 47%! @end deftypefn 48%@eod: 49 50% Copyright (C) 2012-2017 Dynare Team 51% 52% This file is part of Dynare. 53% 54% Dynare is free software: you can redistribute it and/or modify 55% it under the terms of the GNU General Public License as published by 56% the Free Software Foundation, either version 3 of the License, or 57% (at your option) any later version. 58% 59% Dynare is distributed in the hope that it will be useful, 60% but WITHOUT ANY WARRANTY; without even the implied warranty of 61% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 62% GNU General Public License for more details. 63% 64% You should have received a copy of the GNU General Public License 65% along with Dynare. If not, see <http://www.gnu.org/licenses/>. 66 67provide_initial_condition_to_fastgensylv = 0; 68 69info = 0; 70 71F = eval_quadratic_matrix_equation(A,B,C,X); 72 73if max(max(abs(F)))<tol 74 return 75end 76 77kk = 0.0; 78cc = 1+tol; 79 80step_length = 1.0; 81 82while kk<maxit && cc>tol 83 if provide_initial_condition_to_fastgensylv && exist('H','var') 84 H = fastgensylv(A*X+B,A,X,F,tol,maxit,H); 85 else 86 try 87 H = fastgensylv(A*X+B,A,X,F,tol,maxit); 88 catch 89 X = zeros(length(X)); 90 H = fastgensylv(A*X+B,A,X,F,tol,maxit); 91 end 92 end 93 if line_search_flag 94 step_length = line_search(A,H,F); 95 end 96 X = X + step_length*H; 97 F = eval_quadratic_matrix_equation(A,B,C,X); 98 cc = max(max(abs(F))); 99 kk = kk +1; 100end 101 102if cc>tol 103 X = NaN(size(X)); 104 info = 1; 105end 106 107 108function f = eval_quadratic_matrix_equation(A,B,C,X) 109f = C + (B + A*X)*X; 110 111function [p0,p1] = merit_polynomial(A,H,F) 112AHH = A*H*H; 113gamma = norm(AHH,'fro')^2; 114alpha = norm(F,'fro')^2; 115beta = trace(F*AHH*AHH*F); 116p0 = [gamma, -beta, alpha+beta, -2*alpha, alpha]; 117p1 = [4*gamma, -3*beta, 2*(alpha+beta), -2*alpha]; 118 119function t = line_search(A,H,F) 120[p0,p1] = merit_polynomial(A,H,F); 121if any(isnan(p0)) || any(isinf(p0)) 122 t = 1.0; 123 return 124end 125r = roots(p1); 126s = [Inf(3,1),r]; 127for i = 1:3 128 if isreal(r(i)) 129 s(i,1) = p0(1)*r(i)^4 + p0(2)*r(i)^3 + p0(3)*r(i)^2 + p0(4)*r(i) + p0(5); 130 end 131end 132s = sortrows(s,1); 133t = s(1,2); 134if t<=1e-12 || t>=2 135 t = 1; 136end 137 138%@test:1 139%$ addpath ../matlab 140%$ 141%$ % Set the dimension of the problem to be solved 142%$ n = 200; 143%$ % Set the equation to be solved 144%$ A = eye(n); 145%$ B = diag(30*ones(n,1)); B(1,1) = 20; B(end,end) = 20; B = B - diag(10*ones(n-1,1),-1); B = B - diag(10*ones(n-1,1),1); 146%$ C = diag(15*ones(n,1)); C = C - diag(5*ones(n-1,1),-1); C = C - diag(5*ones(n-1,1),1); 147%$ 148%$ % Solve the equation with the cycle reduction algorithm 149%$ tic, X1 = cycle_reduction(C,B,A,1e-7); toc 150%$ 151%$ % Solve the equation with the logarithmic reduction algorithm 152%$ tic, X2 = quadratic_matrix_equation_solver(A,B,C,1e-16,100,1,zeros(n)); toc 153%$ 154%$ % Check the results. 155%$ t(1) = dassert(X1,X2,1e-12); 156%$ 157%$ T = all(t); 158%@eof:1