gcmfaces icon indicating copy to clipboard operation
gcmfaces copied to clipboard

Replace mldivide with lsqminnorm in diffsmooth2D_div_inv.m

Open owang01 opened this issue 2 years ago • 2 comments

When solving A xx = yy, diffsmooth2D_div_inv.m uses xx=A\yy. This becomes less robust when matrix A is close to singular. The solution xx may become all zeros.

Different versions of MATLAB may have different behaviors. For instance, when using calc_barostream.m (which calls diffsmooth2D_div_inv.m) to calculate barotropic stream function, gcmfaces using matlab/2017b was able to find a solution that appears fine, while matlab/2021a gives a solution of all zeros.

This Pull Request uses the more robust MATLAB function lsqminnorm to find a solution in a least-squares sense. This method can also handle sparse matrix and is more efficient than pinv (which cannot handle sparse matrix). Warning is turned on to monitor if the matrix is close to singular.

owang01 avatar Apr 15 '22 17:04 owang01

I'll double check with latest Matlab -- I had been using 2018b. Do you know when lsqminnorm was introduced? We may need a check on matlab version being used for backward compatibility.

gaelforget avatar Apr 15 '22 19:04 gaelforget

That is a good point. lsqminnorm was introduced in R2017b.

owang01 avatar Apr 15 '22 20:04 owang01