Debian Codegraph @

tests/solver-test-functions/discreteboundaryvalue.m

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