It seems that the vectors produced by the Gram-Schmidt process may not be orthogonal (probably due to the presence of roundoff errors or when the matrix is ill conditionned). I would suggest to add an option to test for loss of orthogonality and reorthogonalize if needed. See http://www4.ncsu.edu/~ctk/newton/SOLVERS/nsoli.m for an example.
regards,
Christine