debug = false; source_dir = getenv('source_root'); addpath([source_dir filesep 'matlab']); dynare_config; testFailed = 0; if ~debug skipline() disp('*** TESTING: logarithmicreduction.m ***'); end matlab_cr_path = [source_dir filesep 'matlab' filesep 'missing' filesep 'mex' filesep 'logarithmic_reduction']; addpath(matlab_cr_path); logarithmic_reduction_matlab = @logarithmic_reduction; rmpath(matlab_cr_path); t0 = clock; % Set the dimension of the problem to be solved. n = 500; % Set the equation to be solved A = eye(n); B = diag(30*ones(n,1)); B(1,1) = 20; B(end,end) = 20; B = B - diag(10*ones(n-1,1),-1); B = B - diag(10*ones(n-1,1),1); C = diag(15*ones(n,1)); C = C - diag(5*ones(n-1,1),-1); C = C - diag(5*ones(n-1,1),1); cvg_tol = 1e-12; X1 = zeros(n,n); X2 = zeros(n,n); % 1. Solve the equation with the Matlab logarithmic reduction algorithm tElapsed1 = 0.; try tic; [X1,info] = logarithmic_reduction_matlab(A,B,C,cvg_tol,300,[0.]); tElapsed1 = toc; disp(['Elapsed time for the Matlab logarithmic reduction algorithm is: ' num2str(tElapsed1) ' (n=' int2str(n) ').']) R = norm(C+B*X1+A*X1*X1,1); if (R > cvg_tol) testFailed = testFailed+1; if debug dprintf('Matlab logarithmic_reduction solution is wrong') end end catch testFailed = testFailed+1; if debug dprintf('Matlab logarithmic_reduction failed') end end % 2. Solve the equation with the Fortran logarithmic reduction algorithm tElapsed2 = 0.; try tic; [X2,info] = logarithmic_reduction(A,B,C,cvg_tol,300,[0.]); tElapsed2 = toc; disp(['Elapsed time for the Fortran logarithmic reduction algorithm is: ' num2str(tElapsed2) ' (n=' int2str(n) ').']) R = norm(C+B*X2+A*X2*X2,1); if (R > cvg_tol) testFailed = testFailed+1; if debug dprintf('Fortran logarithmic_reduction solution is wrong') end end catch testFailed = testFailed+1; if debug dprintf('Fortran logarithmic_reduction failed') end end % 3. Compare solutions of the Fortran and Matlab routines if (norm(X1 - X2, 1) > cvg_tol) testFailed = testFailed+1; if debug dprintf('Fortran and Matlab logarithmic reduction solutions differ'); end end % Compare the Fortran and Matlab execution time if debug if tElapsed1 0)