% Test script for Jacobi and GS % First define: method = 'Jacobi' or: method = 'GS' . % Also define: myname = 'xxxxx xxxxx' , when you are % ready to print pictures. % ------------------------------------------------------ if ~exist('method') method = 'Jacobi'; end fprintf('\n ====== CAAM 454 Jacobi/GS Testing ======\n') format1 = 'err = %8.4f cputime = %5.2f \n'; format2 = ' r1 = %8.4f r2 = %5.2f \n'; N = input(' N = '); A = gallery('poisson',N); n = size(A,1); b = A*ones(n,1); tol = 5.e-4; fprintf('\n\t----- Poisson Matrix, size = %g -----\n',n) % ------ run my code ------ fprintf('mycode: ') t = cputime; [myx, myinfo, myres] = feval(['my' method],A,b,10*n,tol); mycput = cputime - t; myerr = norm(myx-1)/sqrt(n); fprintf(format1,myerr,mycput); % ------ run instructor's code ------ fprintf(' Zcode: ') t = cputime; [zx, zinfo, zres] = feval(['z' method],A,b,10*n,tol); zcput = cputime - t; zerr = norm(zx-1)/sqrt(n); fprintf(format1,zerr,zcput); % ------ calculate ratios ------ fprintf('ratios: ') r1 = myerr/zerr; r2 = mycput/zcput; fprintf(format2,r1,r2); disp(['myinfo: ' myinfo]); disp([' zinfo: ' zinfo]); if r1 < 1.01 str1 = 'My solution is accurate'; else str1 = 'My solution is inaccurate'; end str2 = ['CPU ratio = ' num2str(r2)]; if exist('myname') str3 = [myname ', ' date]; else str3 = ['Test Date: ' date]; end % ------ plotting ------ m = length(myres); clf; semilogy(1:m, myres, '.r'); xlabel('Iteration'); ylabel('Residual') title([method ' method']) text(m/2,8,str1); text(m/2,4,str2); text(m/2,2,str3); fprintf([' ====== Method is ' method ' ====== \n\n'])