| 1 |
source_dir = getenv('source_root'); |
| 2 |
addpath([source_dir filesep 'matlab']); |
| 3 |
|
| 4 |
dynare_config; |
| 5 |
|
| 6 |
cd solver-test-functions |
| 7 |
|
| 8 |
testFailed = 0; |
| 9 |
|
| 10 |
skipline() |
| 11 |
disp('*** TESTING: nonlinearsolvers.m ***'); |
| 12 |
|
| 13 |
tolf = 1e-6; |
| 14 |
tolx = 1e-6; |
| 15 |
maxit = 50; |
| 16 |
factor = 10; |
| 17 |
|
| 18 |
% List of function handles |
| 19 |
objfun = { @rosenbrock, |
| 20 |
@powell1, |
| 21 |
@powell2, |
| 22 |
@wood, |
| 23 |
@helicalvalley, |
| 24 |
@watson, |
| 25 |
@chebyquad, |
| 26 |
@brown, |
| 27 |
@discreteboundaryvalue, |
| 28 |
@discreteintegralequation, |
| 29 |
@trigonometric, |
| 30 |
@variablydimensioned, |
| 31 |
@broydentridiagonal, |
| 32 |
@broydenbanded }; |
| 33 |
|
| 34 |
% FIXME block_trust_region (mex) or trust_region (matlab) do not work for all n (not sure we can fix that). |
| 35 |
|
| 36 |
% |
| 37 |
% Test mex routine |
| 38 |
% |
| 39 |
|
| 40 |
t0 = clock; |
| 41 |
|
| 42 |
for i=1:length(objfun) |
| 43 |
switch func2str(objfun{i}) |
| 44 |
case 'helicalvalley' |
| 45 |
% FIXME block_trust_region is diverging if x(1)<0. |
| 46 |
x = helicalvalley(); |
| 47 |
x(1) = 5; |
| 48 |
case 'chebyquad' |
| 49 |
% Fails with a system of 10 equations. |
| 50 |
x = objfun{i}(nan(9,1)); |
| 51 |
case {'watson', 'brown', 'discreteintegralequation', 'discreteboundaryvalue', 'chebyquad', 'trigonometric', 'variablydimensioned', 'broydenbanded', 'broydentridiagonal'} |
| 52 |
x = objfun{i}(nan(4,1)); |
| 53 |
otherwise |
| 54 |
x = objfun{i}(); |
| 55 |
end |
| 56 |
try |
| 57 |
[x, errorflag, exitflag] = block_trust_region(objfun{i}, x, tolf, tolx, maxit, factor, true, false); |
| 58 |
if isequal(func2str(objfun{i}), 'powell2') |
| 59 |
if ~errorflag |
| 60 |
testFailed = testFailed+1; |
| 61 |
dprintf('Nonlinear solver is expected to fail on %s function but did not return an error.', func2str(objfun{i})) |
| 62 |
end |
| 63 |
elseif isequal(func2str(objfun{i}), 'trigonometric') |
| 64 |
% FIXME block_trust_region (mex) fails, with exit code equal to 4, but not trust_region (matlab). Would be nice to undertsand the difference. |
| 65 |
if ~errorflag |
| 66 |
testFailed = testFailed+1; |
| 67 |
dprintf('Nonlinear solver is expected to fail on %s function but did not return an error.', func2str(objfun{i})) |
| 68 |
end |
| 69 |
else |
| 70 |
if errorflag || norm(objfun{i}(x))>tolf |
| 71 |
testFailed = testFailed+1; |
| 72 |
dprintf('Nonlinear solver (mex) failed on %s function (norm(f(x))=%s).', func2str(objfun{i}), num2str(norm(objfun{i}(x)))) |
| 73 |
end |
| 74 |
end |
| 75 |
catch |
| 76 |
testFailed = testFailed+1; |
| 77 |
dprintf('Nonlinear solver (mex) failed on %s function.', func2str(objfun{i})) |
| 78 |
end |
| 79 |
end |
| 80 |
|
| 81 |
t1 = clock; etime(t1, t0) |
| 82 |
|
| 83 |
% |
| 84 |
% Test matlab routine |
| 85 |
% |
| 86 |
|
| 87 |
for i=1:length(objfun) |
| 88 |
switch func2str(objfun{i}) |
| 89 |
case 'chebyquad' |
| 90 |
% Fails with a system of 10 equations. |
| 91 |
x = objfun{i}(nan(9,1)); |
| 92 |
case {'watson', 'brown', 'discreteintegralequation', 'discreteboundaryvalue', 'trigonometric', 'variablydimensioned', 'broydenbanded', 'broydentridiagonal'} |
| 93 |
x = objfun{i}(nan(10,1)); |
| 94 |
otherwise |
| 95 |
x = objfun{i}(); |
| 96 |
end |
| 97 |
try |
| 98 |
[x, errorflag, info] = trust_region(objfun{i}, x, 1:length(x), 1:length(x), true, [], tolf, tolx, maxit, factor); |
| 99 |
if isequal(func2str(objfun{i}), 'powell2') |
| 100 |
if ~errorflag |
| 101 |
testFailed = testFailed+1; |
| 102 |
dprintf('Nonlinear solver is expected to fail on %s function but did not return an error.', func2str(objfun{i})) |
| 103 |
end |
| 104 |
if info~=3 |
| 105 |
testFailed = testFailed+1; |
| 106 |
dprintf('Nonlinear solver is expected to fail on %s function with info==3 but did not the correct value of info.', func2str(objfun{i})) |
| 107 |
end |
| 108 |
else |
| 109 |
if errorflag |
| 110 |
testFailed = testFailed+1; |
| 111 |
dprintf('Nonlinear solver failed on %s function (info=%s).', func2str(objfun{i}), int2str(info)) |
| 112 |
end |
| 113 |
end |
| 114 |
catch |
| 115 |
testFailed = testFailed+1; |
| 116 |
dprintf('Nonlinear solver failed on %s function.', func2str(objfun{i})) |
| 117 |
end |
| 118 |
end |
| 119 |
|
| 120 |
t2 = clock; |
| 121 |
|
| 122 |
fprintf('\n*** Elapsed time (in seconds): %.1f\n\n', etime(t2, t0)); |
| 123 |
|
| 124 |
quit(testFailed > 0) |