This is a nice short code, but it covers only a very special case and does not tell which one exactly.
I recommend spincalc() from Jonathan Fuller. It is much more powerfull!
I'm wondering how you tested the LSMR algorithm with the sparse Matrices? Please let me know.
I checked, by MATLAB, the result of one matrix (produced by mesh2d2.m) from this site: http://www.cise.ufl.edu/research/sparse
and the result was not good at all.
Fantastic! I would only like to pass an initial start guess to the function, so for me I changed the interface to lsmr(..., x0) and in the code I put a
x = x0; clear x0
x = zeros(n,1);
Very useful code, thank you!