function N = nulbasis_modified(A) % modified by % ----------- % name: Ahmed Turk % class: Linear Algebra and Applications % prof. \ Roe Goodman % reason for modification: % the previous version returns error when the matrix has only % the empty vector as the null basis (when the rank(r)= no. of columns) % % N = nulbasis_modified(A) returns a basis for the nullspace of A % in the columns of N. The basis contains the n-r special % solutions to Ax=0. freecol is the list of free columns. % % Example: % % >> A = [1 2 0 3; % [0 0 1 4]; % % >> N = nulbasis_modified(A) % % N = [-2 -3] % [ 1 0] % [ 0 -4] % [ 0 1] % % See also fourbase. [R, pivcol] = rref(A, sqrt(eps)); [m, n] = size(A); r = length(pivcol); if r == n N='empty basis--zero subspace'; else freecol = 1:n; freecol(pivcol) = []; N = zeros(n, n-r); N(freecol, : ) = eye(n-r); N(pivcol, : ) = -R(1:r, freecol); end;