<div dir="ltr"><div>Most numerical analysis experts would agree with what was written at: <a href="https://www.johndcook.com/blog/2010/01/19/dont-invert-that-matrix/" target="_blank"><b>dont-invert-that-matrix</b></a></div><div><br></div><div>In fact, if the <a href="http://www.ncl.ucar.edu/Document/Functions/Built-in/determinant.shtml" target="_blank"><b>determinant</b></a> of a matrix is 0.0, it is not possible to invert the matrix. <br></div><div>The matrix you are using has a determinant value of -1.02827e-41. Possibly, only numerical rounding issues prevented the determinant value from being exactly 0.0. I guess the matrix would be characterized as ill-conditioned. <br></div><div><br></div><div>Matlab likely uses <b>much more sophisticated </b>approaches than using
the LAPACK's LU factorization method used by <a href="http://www.ncl.ucar.edu/Document/Functions/Built-in/inverse_matrix.shtml" target="_blank"><b>inverse_matrix </b></a>.That is what Matlab does ... and VERY well!</div><div><br></div><div>If you need the matrix inverse, perhaps use Matlab and write out the correct matrix inversion to a file and read that into NCL?</div><div><br></div><div>===</div><div><br></div><div>%> ncl invert.ncl_talk.ncl</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Oct 25, 2019 at 7:31 PM Prashanth Bhalachandran via ncl-talk <<a href="mailto:ncl-talk@ucar.edu" target="_blank">ncl-talk@ucar.edu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_default"><font face="garamond, times new roman, serif">Dear NCL'ers,</font></div><div class="gmail_default"><font face="garamond, times new roman, serif">I am having a problem with inverting the matrix using NCL that does not make any sense. </font></div><div class="gmail_default"><font face="garamond, times new roman, serif"><br></font></div><div class="gmail_default"><font face="garamond, times new roman, serif">I double-checked both the routines: inverse_matrix() as well as solve_linsys() and both are functioning well for sample matrices that I am trying out. I even compared their answers to Matlab and they were perfectly right. However, when I use any of the two routines for the below matrix A: </font></div><div class="gmail_default"><font face="garamond, times new roman, serif"><br></font></div><div class="gmail_default">
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:18px;line-height:normal;font-family:Courier;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">write_matrix(A,"9f11.7", False)</span></p><p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:18px;line-height:normal;font-family:Courier;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"><br></span></p></div><div class="gmail_default"><font face="garamond, times new roman, serif">A = </font></div><div class="gmail_default">
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:18px;line-height:normal;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"><font face="monospace"><span> </span>0.4000000 -0.0000000 -0.1000000 -0.0000000 -0.1000000<span> </span>0.3077683 -0.0000000<span> </span>0.0726542 -0.0000000</font></span></p>
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:18px;line-height:normal;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"><font face="monospace"><span> </span>-0.0000000<span> </span>0.1500000 -0.0000000 -0.1000000 -0.0000000 -0.0000000<span> </span>0.1902113 -0.0000000<span> </span>0.0363271</font></span></p>
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:18px;line-height:normal;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"><font face="monospace"><span> </span>-0.1000000 -0.0000000<span> </span>0.1500000<span> </span>0.0000000 -0.1000000 -0.1175570 -0.0000000<span> </span>0.1538842 -0.0000000</font></span></p>
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:18px;line-height:normal;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"><font face="monospace"><span> </span>-0.0000000 -0.1000000<span> </span>0.0000000<span> </span>0.1500000 -0.0000000 -0.0000000 -0.1538842 -0.0000000<span> </span>0.1175570</font></span></p>
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:18px;line-height:normal;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"><font face="monospace"><span> </span>-0.1000000 -0.0000000 -0.1000000 -0.0000000<span> </span>0.1500000 -0.0363271 -0.0000000 -0.1902113 -0.0000000</font></span></p>
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:18px;line-height:normal;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"><font face="monospace"><span> </span>0.3077683 -0.0000000 -0.1175570 -0.0000000 -0.0363271<span> </span>0.2500000<span> </span>0.0000000 -0.0000000 -0.0000000</font></span></p>
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:18px;line-height:normal;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"><font face="monospace"><span> </span>-0.0000000<span> </span>0.1902113 -0.0000000 -0.1538842 -0.0000000<span> </span>0.0000000<span> </span>0.2500000<span> </span>0.0000000<span> </span>0.0000000</font></span></p>
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:18px;line-height:normal;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"><font face="monospace"><span> </span>0.0726542 -0.0000000<span> </span>0.1538842 -0.0000000 -0.1902113 -0.0000000<span> </span>0.0000000<span> </span>0.2500000 -0.0000000</font></span></p>
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:18px;line-height:normal;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"><font face="monospace"><span> </span>-0.0000000<span> </span>0.0363271 -0.0000000<span> </span>0.1175570 -0.0000000 -0.0000000<span> </span>0.0000000 -0.0000000<span> </span>0.2500000</font></span></p>
<p style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-stretch:normal;font-size:18px;line-height:normal;color:rgb(0,0,0);min-height:22px"><span style="font-variant-ligatures:no-common-ligatures"><span><font face="monospace"> </font></span></span></p></div><div class="gmail_default"><font face="garamond, times new roman, serif">The inverse that NCL computes is totally wrong. That is: One, it does not satisfy the identity, A Ainv = Identity Matrix. Two, for the same values, it is very different from what Matlab produces and the one that Matlab produces satisfies the identity matrix test. Can someone please help me understand as to what is wrong with the above matrix and how to circumvent this problem? </font></div><div class="gmail_default"><font face="garamond, times new roman, serif"><br></font></div><div class="gmail_default"><font face="garamond, times new roman, serif">I read in previous NCL help that I could use solve_linsys(A, Identity matrix) and that should give me the inverse. However, that does not work for this particular A as well. It does work for other sample As that I tried. </font></div><div class="gmail_default"><font face="garamond, times new roman, serif"><br></font></div><div class="gmail_default"><font face="garamond, times new roman, serif">Please offer any advice if you have run into something like this before. </font></div><div class="gmail_default"><font face="garamond, times new roman, serif"><br></font></div><div><font face="garamond, times new roman, serif"><span class="gmail_default">Warm Regards, </span></font></div><div><span class="gmail_default"><font face="garamond, times new roman, serif">Prashanth </font></span></div><div dir="ltr"><div dir="ltr"></div></div></div>
_______________________________________________<br>
ncl-talk mailing list<br>
<a href="mailto:ncl-talk@ucar.edu" target="_blank">ncl-talk@ucar.edu</a><br>
List instructions, subscriber options, unsubscribe:<br>
<a href="http://mailman.ucar.edu/mailman/listinfo/ncl-talk" rel="noreferrer" target="_blank">http://mailman.ucar.edu/mailman/listinfo/ncl-talk</a></blockquote></div>