{"id":209,"date":"2009-12-11T18:52:54","date_gmt":"2009-12-11T18:52:54","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2009\/12\/11\/how-to-check-for-existence-of-solution-to-matrix-equations\/"},"modified":"2018-01-08T15:23:13","modified_gmt":"2018-01-08T20:23:13","slug":"how-to-check-for-existence-of-solution-to-matrix-equations","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2009\/12\/11\/how-to-check-for-existence-of-solution-to-matrix-equations\/","title":{"rendered":"How to Check for Existence of Solution to Matrix Equations"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>There was a great <a>question<\/a> on the newsgroup this past week asking how to determine if a system of equations had a solution.  The poster wasn't (at least\r\n         yet) concerned with what the solution was.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">First Solution<\/a><\/li>\r\n         <li><a href=\"#2\">Use rank<\/a><\/li>\r\n         <li><a href=\"#8\">Did You Know...?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>First Solution<a name=\"1\"><\/a><\/h3>\r\n   <p>The first recommendation was to use <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/det.html\"><tt>det<\/tt><\/a>, the determinant.  While this is pedagogically correct for some cases, it is insufficient since it doesn't correctly account\r\n      for non-singular systems which do have solutions. <a>John D'Errico<\/a> followed up with some examples and some smart math.\r\n   <\/p>\r\n   <h3>Use rank<a name=\"2\"><\/a><\/h3>\r\n   <p>John points out that using <tt>det<\/tt> would not give the correct answer for this problem.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">A = ones(2);\r\nb = [2;2];<\/pre><p>Does a solution exist for <tt>A*x = b<\/tt>? It does despite the singularity of <tt>A<\/tt>. <tt>det(A)<\/tt> is zero of course.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">det(A)<\/pre><pre style=\"font-style:oblique\">ans =\r\n     0\r\n<\/pre><p>Yet the answer is just <tt>x = [1;1]<\/tt>. Find it using <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/pinv.html\"><tt>pinv<\/tt><\/a>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">pinv(A)*b<\/pre><pre style=\"font-style:oblique\">ans =\r\n            1\r\n            1\r\n<\/pre><p>Using <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/rank.html\"><tt>rank<\/tt><\/a>, check to see if the    rank([A,b]) == rank(A)\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">rank([A,b]) == rank(A)<\/pre><pre style=\"font-style:oblique\">ans =\r\n     1\r\n<\/pre><p>If the result is true, then a solution exists.<\/p>\r\n   <p>Let's try it for a problem that has no solution.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">c = [1;2];\r\nrank([A,c]) == rank(A)<\/pre><pre style=\"font-style:oblique\">ans =\r\n     0\r\n<\/pre><h3>Did You Know...?<a name=\"8\"><\/a><\/h3>\r\n   <p>I had a previous post about <a href=\"https:\/\/blogs.mathworks.com\/loren\/2008\/06\/06\/collinearity\/\">collinearity<\/a> in which we tried a bunch of solutions, and the superior solution again was <tt>rank<\/tt> vs. <tt>det<\/tt>. Did you know that some singular systems had valid solutions?  Have you had similar instances where what the textbooks sometimes\r\n      recommend isn't ideal for the full spectrum of possible conditions.  Post your thoughts <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=209#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_21baa7e819e04346b40a0347877bf7f3() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='21baa7e819e04346b40a0347877bf7f3 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 21baa7e819e04346b40a0347877bf7f3';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Loren Shure';\r\n        copyright = 'Copyright 2009 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_21baa7e819e04346b40a0347877bf7f3()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.9<br><\/p>\r\n<\/div>\r\n<!--\r\n21baa7e819e04346b40a0347877bf7f3 ##### SOURCE BEGIN #####\r\n%% How to Check for Existence of Solution to Matrix Equations\r\n% There was a great \r\n% <http:\/\/view_thread\/267926#700801 question>\r\n% on the newsgroup this past week asking how to determine if a system of\r\n% equations had a solution.  The poster wasn't (at least yet) concerned\r\n% with what the solution was.\r\n%% First Solution\r\n% The first recommendation was to use\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/det.html |det|>,\r\n% the determinant.  While this is pedagogically correct for some cases, it\r\n% is insufficient since it doesn't correctly account for non-singular\r\n% systems which do have solutions.\r\n% <http:\/\/author\/45419 John D'Errico>\r\n% followed up with some examples and some smart math.\r\n%% Use rank\r\n% John points out that using |det| would not give the correct answer for\r\n% this problem.\r\nA = ones(2);\r\nb = [2;2];\r\n%% \r\n% Does a solution exist for |A*x = b|? It does \r\n% despite the singularity of |A|. |det(A)| is zero of course.\r\ndet(A)\r\n%%\r\n% Yet the answer is just |x = [1;1]|. Find it using \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/pinv.html |pinv|>.\r\npinv(A)*b\r\n%%\r\n% Using <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/rank.html |rank|>,\r\n% check to see if the \r\n%    rank([A,b]) == rank(A)\r\nrank([A,b]) == rank(A)\r\n%%\r\n% If the result is true, then a solution exists. \r\n%%\r\n% Let's try it for a problem that has no solution.\r\nc = [1;2];\r\nrank([A,c]) == rank(A)\r\n%% Did You Know...?\r\n% I had a previous post about\r\n% <https:\/\/blogs.mathworks.com\/loren\/2008\/06\/06\/collinearity\/ collinearity>\r\n% in which we tried a bunch of solutions, and the superior solution again\r\n% was |rank| vs. |det|. Did you know that some singular systems had valid\r\n% solutions?  Have you had similar instances where what the textbooks\r\n% sometimes recommend isn't ideal for the full spectrum of possible\r\n% conditions.  Post your thoughts\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=209#respond here>.\r\n\r\n##### SOURCE END ##### 21baa7e819e04346b40a0347877bf7f3\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      There was a great question on the newsgroup this past week asking how to determine if a system of equations had a solution.  The poster wasn't (at least\r\n         yet) concerned with... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2009\/12\/11\/how-to-check-for-existence-of-solution-to-matrix-equations\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/209"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=209"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/209\/revisions"}],"predecessor-version":[{"id":2572,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/209\/revisions\/2572"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}