function lu=lapmf(u) % LAPMF % % matrix-free negative Laplacian % % function lu=lapmf(u) % % This function applies the 2D Laplacian to a vector in a matrix-free % manner. To make it consistent with the solves, the input u is % a linear array with dimension n*n, where n+1 MUST be a power of 2 % if you plan to use P2D in any way. You must use reshape to get to/from % 2D n x n arrays defined on the geometric grid to the n*n x 1 arrays % that solvers expect. This code does the 1D -> 2D -> 1D conversion % for you. % n2=length(u); n=sqrt(n2); h2=(n+1)*(n+1); % % transform the linear array into a 2d array % uu=zeros(n,n); uu(:)=u; % % compute the discrete negative Laplacian on the 2d array % luu=4*uu; if n>1 luu(1:n-1,:)=luu(1:n-1,:)-uu(2:n,:); luu(2:n,:)=luu(2:n,:)-uu(1:n-1,:); luu(:,2:n)=luu(:,2:n)-uu(:,1:n-1); luu(:,1:n-1)=luu(:,1:n-1)-uu(:,2:n); end % % convert to a linear array, divide by h^2 % lu=luu(:)*h2;