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