function A = nma_FDM_matrix_laplace_1D_Neumann_scheme_2(N) % builds finite difference A matrix for 1-D laplace nuemman boundary conditions %----------------------------------------------------------------- % A = nma_FDM_matrix_laplace_1D_Neumann_scheme_2(N) % % returns the A matrix, which is the system finite difference matrix for % numerical solution of 1-D laplace equation Uxx = f, with nuemman % boundary conditions on both sides of the element. Based on 3 point % scheme U'' = U(j-1)-2*U(j)+U(j+1) for middle points and the left % and right most points, the following scheme is used % In this scheme, the Neumann boundary condition is approximated using % % a =(1/h)((3/2)U(0)-2*U(1)+(1/2)U(2)) % b =(1/h)((3/2)U(N+1)-2*U(N)+(1/2)U(N+1)) % % where a=U'(0) and b=U'(1) % % notice that spacing h between the nodes is not used. The caller must % divide by h^2 this A matrix abone return. % % INPUT: N, the number of nodes % OUTPUT: A, the matrix , see below % % EXAMPLE: % A = nma_FDM_matrix_laplace_1D_Neumann_scheme_2(5) % % 1.5000 -2.0000 0.5000 0 0 % 1.0000 -2.0000 1.0000 0 0 % 0 1.0000 -2.0000 1.0000 0 % 0 0 1.0000 -2.0000 1.0000 % 0 0 0.5000 -2.0000 1.5000 % % % A = A/h^2; % h is space between points % U = A\f; % solve for U, where Uxx=f, need to set f as function before. % % copyright: Nasser M. Abbasi % 10/18/2010 A = zeros(N); for i=1:N for j=1:N % as before, part(a), Dirichlet B.C. if(i==j) A(i,j) = -2; else if( i==j+1 || i==j-1 ) A(i,j) = 1; end end end end %fix A above for Von Neumann B.C. only 1st and last rows need fixed A(1,1) = 3/2; A(1,2) = -2; A(1,3) = 1/2; A(end,end) = (3/2); A(end,end-1) = -2; A(end,end-2) = (1/2); end