function A = nma_FDM_matrix_laplace_1D_Neumann_scheme_1(N ) % builds finite difference A matrix for 1-D laplace %----------------------------------------------------------------- % nma_FDM_matrix_laplace_1D_Neumann_scheme_1(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 based on % scheme ((-2*U(0)+2*U(1))/(h^2))=f(0)+((2*a)/h) for the left most point, % where a = U'(0), and h is the spacing. and for for the right most point % ((2*U(N)-2*U(N+1))/(h^2)) = f(N+1)-((2*b)/h) where 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_1(5) % -2 2 0 0 0 % 1 -2 1 0 0 % 0 1 -2 1 0 % 0 0 1 -2 1 % 0 0 0 2 -2 % % % 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 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,2) = 2; A(end,end-1) = 2; end