function A=nma_laplaceRectDirchlet(nx,ny,bot,right,top,left,lambda,tol) % solve laplace PDE for rectangular region Dirclet BC %function A=nma_laplaceRectDirchlet(nx,ny,bot,right,top,left,lambda,tol) % % %INPUT: % nx: number of grid points in the x-direction % ny: number of grid points in the y-direction % bot: bottom edge boundary value % right: right edge boundary value % top: top edge boundary value % left: left edge boundary value % lambda: the value lambda to use for the relaxation method of Liebmann % tol: the absolute tolerance to check for convergance. % %OUTPUT: % the solution matrix. % %Author: Nasser Abbasi %May 25, 2003 TRUE = 1; FALSE = 0; A = zeros(nx+2,ny+2); A_old = A; % apply dirchlet conditions A(1,:) = top; A(:,1) = left; A(end,:) = bot; A(:,end) = right; %patch the 4 corner, just for cosmotics purposes A(1,1) = 0; A(end,1) = 0; A(1,end) = 0; A(end,end) = 0; A_old = A; converged = FALSE; nIter = 0; while(~converged) nIter = nIter+1; %fprintf('Iteration number %d\n',nIter); for(i=2+nx-1:-1:2) for(j=2:1:2+ny-1) A(i,j) = A(i,j+1) + A(i,j-1) + A(i-1,j) + A(i+1,j); A(i,j) = A(i,j)/4; A(i,j) = lambda*A(i,j) + (1-lambda)*A_old(i,j); end end [max_row , max_row_index] = max(A(2:end-1,2:end-1)); [max_element , max_col_index] = max(max_row); pos = [max_row_index(1)+1,max_col_index+1]; epsilonA = abs(A(pos(1),pos(2)) - A_old(pos(1),pos(2)))/A(pos(1),pos(2))*100; if( epsilonA < tol ) converged = TRUE; end A_old = A; %A %epsilonA end %A=A(2:end-1,2:end-1);