Skip to content

Commit a4c2c65

Browse files
committed
Replace mldivide with lsqminnorm in diffsmooth2D_div_inv.m
1 parent 2a2fb19 commit a4c2c65

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

gcmfaces_smooth/diffsmooth2D_div_inv.m

+19-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,25 @@
101101

102102
%4) solve for potential:
103103
yy=convert2array(fld); yy=yy(find(KK~=0));
104-
xx=A\yy;
104+
%Solving A xx = yy
105+
%Original, but less robust, way to solve A xx = yy
106+
% is use xx=A\yy;.
107+
% When matrix A is close to singular, xx may become
108+
% all zeros. Different versions of MATLAB
109+
% may have different behaviors. For instance,
110+
% when using calc_barostream.m (which calls
111+
% diffsmooth2D_div_inv.m) to calculate barotropic stream function,
112+
% gcmfaces using matlab/2017b was able to find a solution that
113+
% appears fine, while matlab/2021a gives a solution
114+
% of all zeros.
115+
%xx=A\yy;
116+
%Use the more robust way to solve yy
117+
%in a least squares sense. This method
118+
%can aslo handle sparse matrix and is
119+
%more efficient than pinv (which cannot handle sparse matrix).
120+
%Warning is turned on to monitor if the matrix is close to singular.
121+
xx=lsqminnorm(A,yy,'warn');
122+
105123
yyFROMxx=A*xx;
106124

107125
%5) prepare output:

0 commit comments

Comments
 (0)