%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Solution to Homework 3, problem 3. % Solve the linear system Au=b where A is the % finite difference approximation to % - u_{xx} - u_{yy} -u_{zz} = f(x,y,z) % for (x,y,z) in (0,1) x (0,1) x (0,1), % with u = 0 on the boundary of the box. % % Use varying meshes, with h=1/(n+1), and solve using % preconditioned CG, with the preconditioner an incomplete % Cholesky factorization with varying drop tolerance. % % Plot the time and memory required by the various % solution algorithms. % % problem3.m Dianne O'Leary 04-2005 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% j = 0; % Initialize the colors for plotting. cc = ['b','g','r','c','m','k','b','g','r','c','m']; % Loop for n=10:5:30 % Generate the problem % Solve for various values of droptol for n=10:5:30 j = j + 1; i = 0; [A,b,h] = laplace3d(n); tol = .01*h^2; normb = norm(b); if (n < 30) tryd = [0 .0001 .001 .01 .05 .1 .2 .3 ]; else tryd = [.001 .01 .05 .1 .2 .3 ]; % otherwise: memory troubles end for droptol = tryd i = i + 1; disp(' ') disp(sprintf('Solving with n=%d and convergence tolerance = %5.2e', ... length(b),tol)) tic R = cholinc(A,droptol); [x,cflag(i,j),relres,iter(i,j)] = pcg(A,b,tol,500,R',R); nz(i,j) = 3*nnz(A) + 5*n + 3*nnz(R); ctime(i,j) = toc; rnorm(i,j) = norm(b-A*x)/normb; end end % Plot the results. hold off for jj=1:j, loglog(nz(:,jj),ctime(:,jj),cc(jj)) hold on loglog(nz(:,jj),ctime(:,jj),'*') end xlabel('memory') ylabel('time') print -depsc prob3.eps