Debian Codegraph @

tests/solver-test-functions/broydenbanded.m

raw
1 function [fval, fjac] = broydenbanded(x)
2
3 % Copyright © 2021 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 <https://www.gnu.org/licenses/>.
19
20 n = length(x);
21
22 if isnumeric(x) && isvector(x) && all(~isnan(x))
23 fval = zeros(n, 1);
24 ml = 5;
25 mu = 1;
26 for k=1:n
27 k1 = max(1, k-ml);
28 k2 = min(k+mu, n);
29 tmp = 0;
30 for j=k1:k2
31 if j~=k
32 tmp = tmp + x(j)*(1+x(j));
33 end
34 end
35 fval(k) = x(k)*(2+5*x(k)*x(k))+1-tmp;
36 end
37 if nargout>1
38 fjac = zeros(n);
39 ml = 5;
40 mu = 1;
41 for k=1:n
42 k1 = max(1,k-ml);
43 k2 = min(k+mu,n);
44 for j=k1:k2
45 if j~=k
46 fjac(k,j) = -(1+2*x(j));
47 end
48 end
49 fjac(k,k) = 2+15*x(k)*x(k);
50 end
51 end
52 elseif isnumeric(x) && isvector(x) && all(isnan(x))
53 if nargout==1
54 fval = broydentridiagonal(nan(n,1));
55 else
56 error('One output is required for initialization mode.')
57 end
58 else
59 error('Wrong input argument.')
60 end