Debian Codegraph @

tests/solver-test-functions/brown.m

raw
1 function [fval, fjac] = brown(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 sum = -(n+1);
25 prod = 1;
26 for j=1:n
27 sum = sum+x(j);
28 prod = prod*x(j);
29 end
30 for k=1:n-1
31 fval(k) = x(k)+sum;
32 end
33 fval(n) = prod-1;
34 if nargout>1
35 fjac = zeros(n);
36 prod = 1;
37 for j=1:n
38 prod = prod*x(j);
39 for k=1:n
40 fjac(k,j) = 1;
41 end
42 fjac(j,j) = 2;
43 end
44 for j=1:n
45 tmp = x(j);
46 if abs(tmp)<eps()
47 tmp = 1;
48 prod = 1;
49 for k=1:n
50 if k~=j
51 prod = prod*x(k);
52 end
53 end
54 end
55 fjac(n,j) = prod/tmp;
56 end
57 end
58 elseif isnumeric(x) && isvector(x) && all(isnan(x))
59 if nargout==1
60 fval = 0.5*ones(length(x), 1);
61 else
62 error('One output is required for initialization mode.')
63 end
64 else
65 error('Wrong input argument.')
66 end