function invA=mnhf_inverse(A) %MNHF_INVERSE calculate matrix inverse % % A - the (square) matrix in question % % Usage mnhf_inverse(A); tolerance = 1e-8; [r,c] = size(A); % Compute the size of A. if isequal(r,c) if r==1 fprintf('Matrix has dimension 1-by-1.') else if abs(mnhf_determinant(A))<1e-6 error('Matrix is singular; determinant is 0.') else invA = mnhf_adjoint(A)/mnhf_determinant(A); end % Check residuals. resid1 = norm(A*invA-eye(length(A)),2); resid2 = norm(invA*A-eye(length(A)),2); if max(resid1,resid2)>tolerance error('Residual exceeds tolerance.') end end else fprintf('Can''t evaluate inverse of non-square A.\n\n') end