| 1 |
function [fval, fjac] = watson(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 |
C9 = 29.0; |
| 23 |
|
| 24 |
if isnumeric(x) && isvector(x) && all(~isnan(x)) |
| 25 |
fval = zeros(n, 1); |
| 26 |
for i=1:29 |
| 27 |
ti = i/C9; |
| 28 |
sum1 = 0; |
| 29 |
tmp = 1; |
| 30 |
for j=2:n |
| 31 |
sum1 = sum1+(j-1)*tmp*x(j); |
| 32 |
tmp = tmp*ti; |
| 33 |
end |
| 34 |
sum2 = 0; |
| 35 |
tmp = 1; |
| 36 |
for j=1:n |
| 37 |
sum2 = sum2+tmp*x(j); |
| 38 |
tmp = tmp*ti; |
| 39 |
end |
| 40 |
tmp1 = sum1-sum2*sum2-1; |
| 41 |
tmp2 = 2*ti*sum2; |
| 42 |
tmp = 1/ti; |
| 43 |
for k=1:n |
| 44 |
fval(k) = fval(k) + tmp*((k-1)-tmp2)*tmp1; |
| 45 |
tmp = tmp*ti; |
| 46 |
end |
| 47 |
end |
| 48 |
tmp = x(2)-x(1)*x(1)-1; |
| 49 |
fval(1) = fval(1)+ x(1)*(1-2*tmp); |
| 50 |
fval(2) = fval(2)+tmp; |
| 51 |
if nargout>1 |
| 52 |
fjac = zeros(n); |
| 53 |
for i=1:29 |
| 54 |
ti = i/C9; |
| 55 |
sum1 = 0; |
| 56 |
tmp = 1; |
| 57 |
for j=2:n |
| 58 |
sum1 = sum1 + (j-1)*tmp*x(j); |
| 59 |
tmp = tmp*ti; |
| 60 |
end |
| 61 |
sum2 = 0; |
| 62 |
tmp = 1; |
| 63 |
for j=1:n |
| 64 |
sum2 = sum2+tmp*x(j); |
| 65 |
tmp = tmp*ti; |
| 66 |
end |
| 67 |
tmp1 = 2*(sum1-sum2*sum2-1); |
| 68 |
tmp2 = 2*sum2; |
| 69 |
tmp = ti*ti; |
| 70 |
tk = 1; |
| 71 |
for k=1:n |
| 72 |
tj = tk; |
| 73 |
for j=k:n |
| 74 |
fjac(k,j) = fjac(k,j)+tj*(((k-1)/ti-tmp2)*((j-1)/ti-tmp2)-tmp1); |
| 75 |
tj = tj*ti; |
| 76 |
end |
| 77 |
tk = tk*tmp; |
| 78 |
end |
| 79 |
end |
| 80 |
fjac(1,1) = fjac(1,1)+6*x(1)*x(1)-2*x(2)+3; |
| 81 |
fjac(1,2) = fjac(1,2)-2*x(1); |
| 82 |
fjac(2,2) = fjac(2,2)+1; |
| 83 |
for k=1:n |
| 84 |
for j=k:n |
| 85 |
fjac(j,k) = fjac(k,j); |
| 86 |
end |
| 87 |
end |
| 88 |
end |
| 89 |
elseif isnumeric(x) && isvector(x) && all(isnan(x)) |
| 90 |
if nargout==1 |
| 91 |
fval = zeros(length(x), 1); |
| 92 |
else |
| 93 |
error('One output is required for initialization mode.') |
| 94 |
end |
| 95 |
else |
| 96 |
error('Wrong input argument.') |
| 97 |
end |