%DisplayPerf.m %Jonathan Dorfman %Math 128B Spring 2005 %Hmwk3 - Solutions (Feb. 5, 2005) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %gPerfCnt is number of iteration samples %gPerf is structure containing array of eigenvalue and Xn and err info %cmdStr is 'init', 'reset', or text to be displayed in title %A is nxn matrix (tolerance when cmdStr='init') %b is row vector to solve Ax=b' %x is col vector solution returned by our implementation function DisplayPerf(cmdStr, A, b, x); global gPerfCnt gPerf persistent cr tolStr yLim gFigure if strcmp(cmdStr, 'init') %b = row vector, A=tolerance (overloaded) cr = sprintf('\n'); n = length(b); %for pre-allocating Xn-field tol = A; N = 300; %initial pre-allocations %initialize global variables gPerfCnt = 0; gFigure = 1; gPerf = struct('eigval',zeros(N,1),'err',zeros(N,1),'Xn',zeros(N,n)); %initialize persistent variables yLim = tol ^ (.1); tolStr = ['{\bfTolerance} = ' num2str(tol) cr]; elseif strcmp(cmdStr, 'reset') gPerfCnt = 0; else %make new figure figure(gFigure); gFigure = gFigure + 1; %get Matlab's computed solution z = A\b'; disp(['%%%%%%%%%% ' cmdStr ' %%%%%%%%%%' cr]); if (gPerfCnt > 500) disp('Failed to converge in 500 iterations'); end iterStr = 'Number of iterations = '; iterStr = [iterStr num2str(gPerfCnt) cr]; disp(iterStr); str = 'Error in computed solution (vs. Matlab): '; str = [str num2str(norm(x - z, inf)) cr]; disp(str); str = [' [' num2str(x') ']' cr ' vs. [' num2str(z') ']' cr]; disp(str); str = 'Error in residual: '; str = [str num2str(norm((A*x)' - b, inf)) cr]; disp(str); %display plot of errors t = [1:gPerfCnt]; zz = repmat(z,1,gPerfCnt); nn = gPerf.Xn(t,:)' - zz; plot(t, max(abs(nn)), 'x', t, gPerf.err(t), 'o'); %cosmetics (cmdStr is title text) legend('|| x^{(k)} - \{Matlab solution\} || _{\infty}', '|| x^{(k)} - x^{(k-1)} || _{\infty}'); ylim([0, yLim]); xlim([1, gPerfCnt]); title(['Convergence Statistics for ' cmdStr],'FontSize',12); text(0.6, 0.77, tolStr, 'FontSize',10,'Units', 'normalized'); text(0.6, 0.70, iterStr,'FontSize',10,'Units', 'normalized', 'FontWeight', 'bold'); end return