{"id":293,"date":"2011-10-25T13:58:55","date_gmt":"2011-10-25T13:58:55","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2011\/10\/25\/simplifying-symbolic-results\/"},"modified":"2018-06-11T11:23:42","modified_gmt":"2018-06-11T16:23:42","slug":"simplifying-symbolic-results","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2011\/10\/25\/simplifying-symbolic-results\/","title":{"rendered":"Simplifying Symbolic Results"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>I am pleased to introduce guest blogger Kai Gehrs. Kai is a developer for the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/toolbox\/symbolic\/\">Symbolic Math Toolbox<\/a>. His main focus in this post is on special approaches to symbolic simplification and equation solving.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Have You Noticed Results Not Being Simplified Well?<\/a><\/li>\r\n         <li><a href=\"#2\">Using Assumptions on Variables<\/a><\/li>\r\n         <li><a href=\"#4\">When Things Get More Complicated<\/a><\/li>\r\n         <li><a href=\"#7\">Using Option IgnoreAnalyticConstraints for Simplification<\/a><\/li>\r\n         <li><a href=\"#9\">Using IgnoreAnalyticConstraints for Equation Solving<\/a><\/li>\r\n         <li><a href=\"#16\">Have You Tried IgnoreAnalyticConstraints?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Have You Noticed Results Not Being Simplified Well?<a name=\"1\"><\/a><\/h3>\r\n   <p>When using the Symbolic Math Toolbox for symbolic simplification, have you ever been wondering why it does not apply certain\r\n      classic book rules automatically to return the results you have in mind?\r\n   <\/p>\r\n   <p>Some of these classic book rules are:<\/p>\r\n   <div>\r\n      <ul>\r\n         <li><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq18740.png\"> <\/li>\r\n         <li><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq11583.png\"> <\/li>\r\n         <li><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq30295.png\"> <\/li>\r\n         <li><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq62163.png\"> <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>Just using <tt>simplify<\/tt> to get the result <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq27864.png\">  on input <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq78137.png\">  does not work:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">syms <span style=\"color: #A020F0\">a<\/span> <span style=\"color: #A020F0\">b<\/span>\r\nsimplify(log(a)+log(b))<\/pre><pre style=\"font-style:oblique\">ans =\r\nlog(a) + log(b)\r\n<\/pre><h3>Using Assumptions on Variables<a name=\"2\"><\/a><\/h3>\r\n   <p>Of course, we all know that the rule <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq11583.png\">  applies only under appropriate mathematical assumptions on <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq34276.png\">  and <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq28812.png\"> . For example, if we assume that <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq34276.png\">  and <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq28812.png\">  are positive, we will get the desired result:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">syms <span style=\"color: #A020F0\">a<\/span> <span style=\"color: #A020F0\">b<\/span> <span style=\"color: #A020F0\">positive<\/span>\r\nsimplify(log(a)+log(b))<\/pre><pre style=\"font-style:oblique\">ans =\r\nlog(a*b)\r\n<\/pre><p>To get rid of all previously specified assumptions, use<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">syms <span style=\"color: #A020F0\">a<\/span> <span style=\"color: #A020F0\">b<\/span> <span style=\"color: #A020F0\">clear<\/span><\/pre><h3>When Things Get More Complicated<a name=\"4\"><\/a><\/h3>\r\n   <p>Does it mean that setting the right assumptions is a universal solution here? Well, not always!<\/p>\r\n   <p>Assume <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq34276.png\">  and <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq28812.png\">  appear as intermediate results in some really huge symbolic computations somewhere in, lets say, line 454 of your MATLAB\r\n      script. From the context of your application, you know that <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq34276.png\">  and <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq28812.png\">  are positive. Now you want MATLAB to automatically compute a simplified form of <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq78137.png\">  in line 455. How will you manage to set the right assumptions?\r\n   <\/p>\r\n   <p>As an example, think of <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq34276.png\">  and <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq28812.png\">  as being something like\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">syms <span style=\"color: #A020F0\">x<\/span> <span style=\"color: #A020F0\">y<\/span>\r\na = -(x + 1)^(1\/2)\/((exp(x - y) - sin(x + y)) * <span style=\"color: #0000FF\">...<\/span>\r\n     (log((x^2 + 1)\/(y^2 + 1))\/exp(y) + (x - y)^y + <span style=\"color: #0000FF\">...<\/span>\r\n      1\/(x - y)^x));\r\nb = (cos(x)*sin(y))\/((x - y)^x*(x + 1)^(1\/2)) - <span style=\"color: #0000FF\">...<\/span>\r\n    (exp(x)*(x - y)^y)\/(exp(y)*(x + 1)^(1\/2)) + <span style=\"color: #0000FF\">...<\/span>\r\n    (cos(y)*sin(x))\/((x - y)^x*(x + 1)^(1\/2)) - <span style=\"color: #0000FF\">...<\/span>\r\n    (exp(x)*log((x^2 + 1)\/(y^2 + 1)))\/ <span style=\"color: #0000FF\">...<\/span>\r\n    (exp(2*y)*(x + 1)^(1\/2)) - exp(x)\/ <span style=\"color: #0000FF\">...<\/span>\r\n    (exp(y)*(x - y)^x*(x + 1)^(1\/2)) + <span style=\"color: #0000FF\">...<\/span>\r\n    (cos(x)*sin(y)*(x - y)^y)\/(x + 1)^(1\/2) + <span style=\"color: #0000FF\">...<\/span>\r\n    (cos(y)*sin(x)*(x - y)^y)\/(x + 1)^(1\/2) + <span style=\"color: #0000FF\">...<\/span>\r\n    (log((x^2 + 1)\/(y^2 + 1))*cos(x)*sin(y))\/ <span style=\"color: #0000FF\">...<\/span>\r\n    (exp(y)*(x + 1)^(1\/2)) + (log((x^2 + 1)\/ <span style=\"color: #0000FF\">...<\/span>\r\n    (y^2 + 1))*cos(y)*sin(x))\/(exp(y)*(x + 1)^(1\/2));<\/pre><p>Now executing the <tt>simplify<\/tt> command does not seem to be helpful:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">S = simplify(log(a)+log(b));\r\npretty(S)<\/pre><pre style=\"font-style:oblique\"> \r\n     \/                                         y \r\n     |     sin(x + y)        sin(x + y) (x - y) \r\n  log| ------------------- + ------------------- + \r\n     |        x        1\/2              1\/2 \r\n     \\ (x - y)  (x + 1)          (x + 1) \r\n   \r\n                                   y \r\n     #1 sin(x + y)   exp(x) (x - y) \r\n     ------------- - --------------- - \r\n          #2               #2 \r\n   \r\n                                                      \\ \r\n          exp(x) #1                  exp(x)           | \r\n     ------------------- - -------------------------- | + \r\n                     1\/2                 x        1\/2 | \r\n     exp(2 y) (x + 1)      exp(y) (x - y)  (x + 1)    \/ \r\n   \r\n        \/           1\/2    \/ \r\n     log| - ((x + 1)   ) \/ | (exp(x - y) - sin(x + y)) \r\n        |                  | \r\n        \\                  \\ \r\n   \r\n     \/   #1            y      1     \\ \\ \\ \r\n     | ------ + (x - y)  + -------- | | | \r\n     | exp(y)                     x | | | \r\n     \\                     (x - y)  \/ \/ \/ \r\n   \r\n  where \r\n   \r\n             \/  2     \\ \r\n             | x  + 1 | \r\n     #1 = log| ------ | \r\n             |  2     | \r\n             \\ y  + 1 \/ \r\n   \r\n                        1\/2 \r\n     #2 = exp(y) (x + 1)\r\n<\/pre><p>Assuming <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq43551.png\">  and <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq44020.png\">  to be positive does not significantly improve the result either. The reason is that we need to set such assumptions on <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq43551.png\">  and <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq44020.png\">  that would make the expressions <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq34276.png\">  and <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq28812.png\">  positive. We can try to find the right assumptions for this example, but in general it seems like one has to be a genius\r\n      to guess what's appropriate.\r\n   <\/p>\r\n   <h3>Using Option IgnoreAnalyticConstraints for Simplification<a name=\"7\"><\/a><\/h3>\r\n   <p>A possible solution to the problem is to ignore certain analytic constraints, that is, to use the <tt>IgnoreAnalyticConstraints<\/tt> option for <tt>simplify<\/tt>.\r\n   <\/p>\r\n   <p>With this option the simplifier internally applies the following rules:<\/p>\r\n   <div>\r\n      <ul>\r\n         <li><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq11583.png\">  for all values of <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq34276.png\">  and <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq28812.png\"> . In particular <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq94804.png\">  for all values of <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq34276.png\"> , <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq28812.png\">  and <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq27869.png\"> .\r\n         <\/li>\r\n         <li><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq65318.png\">  for all values of <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq34276.png\">  and <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq28812.png\"> . In particular <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq99194.png\">  for all values of <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq34276.png\"> , <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq28812.png\">  and <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq27869.png\"> .\r\n         <\/li>\r\n         <li>If <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq85998.png\">  and <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq31049.png\">  are standard math functions and <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq21938.png\">  holds for all small positive numbers, then <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq21938.png\">  is assumed to be valid for all <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq43551.png\">  (for example as in case of  <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq62163.png\"> ).\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>So how does this work in our example?<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">simplify(log(a)+log(b),<span style=\"color: #A020F0\">'IgnoreAnalyticConstraints'<\/span>,true)<\/pre><pre style=\"font-style:oblique\">ans =\r\n0\r\n<\/pre><p>The result is <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq12896.png\"> , because <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq06833.png\"> . Hence, under the above assumptions, we get <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq49915.png\"> .\r\n   <\/p>\r\n   <p>Of course, it is important to keep in mind that the rules applied by <tt>IgnoreAnalyticConstraints<\/tt> are not correct in a strict mathematical sense. Nevertheless, in practice these rules are often very helpful to get simpler\r\n      results. Another nice side effect is that ignoring some analytic constraints often helps you speed up your computations.\r\n   <\/p>\r\n   <p>This <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/toolbox\/symbolic\/\/simplify.html\">documentation<\/a> describes more details on <tt>IgnoreAnalyticConstraints<\/tt>.\r\n   <\/p>\r\n   <h3>Using IgnoreAnalyticConstraints for Equation Solving<a name=\"9\"><\/a><\/h3>\r\n   <p>Not surprisingly the concept of ignoring analytic constraints also makes sense for equation solving. Imagine that you want\r\n      to solve the equation <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq43471.png\">  for <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq43551.png\"> . Ignoring analytic constraints would certainly mean to write this as <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq45405.png\"> . Assuming <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq25947.png\">  to be nonzero we get <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq73458.png\">  and, finally, <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq47764.png\"> .\r\n   <\/p>\r\n   <p>Without using any restrictions, Symbolic Math Toolbox returns the result:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">syms <span style=\"color: #A020F0\">x<\/span> <span style=\"color: #A020F0\">n<\/span>\r\nsolve(log(x^n),x)<\/pre><pre style=\"font-style:oblique\">Warning: The solutions are parametrized by the symbols:\r\nk = Z_ intersect Dom::Interval([-1\/(2*Re(1\/n))],\r\n1\/(2*Re(1\/n)))\r\n \r\nans =\r\n1\/exp((pi*k*2*i)\/n)\r\n<\/pre><p>So you get a parameterized solution which strongly depends on the values of <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq25947.png\"> . This is reasonable, because, for example, for <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq24578.png\">  you get the four solutions\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">solve(log(x^4),x)<\/pre><pre style=\"font-style:oblique\">ans =\r\n  1\r\n -1\r\n  i\r\n -i\r\n<\/pre><p>whereas for <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/293\/SimplifyingSymbolicResults_eq08039.png\">  there only is one solution:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">solve(log(x^(1\/2)),x)<\/pre><pre style=\"font-style:oblique\">ans =\r\n1\r\n<\/pre><p>Applying <tt>IgnoreAnalyticConstraints<\/tt> we get\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">solve(log(x^n),x,<span style=\"color: #A020F0\">'IgnoreAnalyticConstraints'<\/span>,true)<\/pre><pre style=\"font-style:oblique\">ans =\r\n1\r\n<\/pre><p>Also for equations involving roots where no additional symbolic parameters are present, it may be useful to apply <tt>IgnoreAnalyticConstraints<\/tt> to get simpler results:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">solve(x^(5\/2) - 8^(sym(10\/3)), <span style=\"color: #A020F0\">'IgnoreAnalyticConstraints'<\/span>, true)<\/pre><pre style=\"font-style:oblique\">ans =\r\n16\r\n<\/pre><p>Here the solver ignores branch cuts during internal simplifications and, hence, returns only one solution.<\/p>\r\n   <p>See the MATLAB doc page on <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/toolbox\/symbolic\/\/solve.html\">solve<\/a> for further details.\r\n   <\/p>\r\n   <p>The <tt>IgnoreAnalyticConstraints<\/tt> option can also be used for other Symbolic Math Toolbox functions like the function <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/toolbox\/symbolic\/\/int-integral.html\">int<\/a> for doing symbolic integration. The option is also available for the related MuPAD Notebook Interface functions.\r\n   <\/p>\r\n   <h3>Have You Tried IgnoreAnalyticConstraints?<a name=\"16\"><\/a><\/h3>\r\n   <p>Have you tried the <tt>IgnoreAnalyticConstraints<\/tt> option to get simpler, shorter, and easier to handle results?\r\n   <\/p>\r\n   <p>Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=293#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_dfaf2d6d91024777b15afd5ba977bfc8() {\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='dfaf2d6d91024777b15afd5ba977bfc8 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' dfaf2d6d91024777b15afd5ba977bfc8';\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 = 'Kai Gehrs';\r\n        copyright = 'Copyright 2011 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_dfaf2d6d91024777b15afd5ba977bfc8()\"><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.13<br><\/p>\r\n<\/div>\r\n<!--\r\ndfaf2d6d91024777b15afd5ba977bfc8 ##### SOURCE BEGIN #####\r\n%% Simplifying Symbolic Results\r\n% I am pleased to introduce guest blogger Kai Gehrs. Kai is a developer for \r\n% the <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/toolbox\/symbolic\/ Symbolic Math Toolbox>. \r\n% His main focus in this post is on special approaches to symbolic \r\n% simplification and equation solving.\r\n%\r\n%% Have You Noticed Results Not Being Simplified Well?\r\n% When using the Symbolic Math Toolbox for symbolic simplification, have \r\n% you ever been wondering why it does not apply certain classic book \r\n% rules automatically to return the results you have in mind? \r\n% \r\n% Some of these classic book rules are: \r\n% \r\n% * $\\sqrt{x^2} = x$ \r\n% * $\\log(a) + \\log(b) = \\log(a \\cdot b)$\r\n% * $b \\cdot \\log(a) = \\log(a^b)$\r\n% * $\\mbox{asin}(\\sin(x)) = x$\r\n%\r\n% Just using |simplify| to get the result $\\log(a \\cdot b)$ on input \r\n% $\\log(a) + \\log(b)$ does not work: \r\nsyms a b \r\nsimplify(log(a)+log(b))\r\n%% Using Assumptions on Variables\r\n% Of course, we all know that the rule $\\log(a) + \\log(b) = \\log(a \\cdot b)$ \r\n% applies only under appropriate mathematical assumptions on $a$ and $b$. \r\n% For example, if we assume that $a$ and $b$ are positive, we will get the \r\n% desired result:\r\nsyms a b positive\r\nsimplify(log(a)+log(b))\r\n%%\r\n% To get rid of all previously specified assumptions, use \r\nsyms a b clear\r\n%% When Things Get More Complicated\r\n% Does it mean that setting the right assumptions is a universal solution \r\n% here? Well, not always! \r\n% \r\n% Assume $a$ and $b$ appear as intermediate results in some really huge \r\n% symbolic computations somewhere in, lets say, line 454 of your MATLAB \r\n% script. From the context of your application, you know that $a$ and $b$ \r\n% are positive. \r\n% Now you want MATLAB to automatically compute a simplified form of $\\log(a) + \r\n% \\log(b)$ in line 455. How will you manage to set the right assumptions? \r\n% \r\n% As an example, think of $a$ and $b$ as being something like \r\nsyms x y  \r\na = -(x + 1)^(1\/2)\/((exp(x - y) - sin(x + y)) * ...\r\n     (log((x^2 + 1)\/(y^2 + 1))\/exp(y) + (x - y)^y + ...\r\n      1\/(x - y)^x));\r\nb = (cos(x)*sin(y))\/((x - y)^x*(x + 1)^(1\/2)) - ...\r\n    (exp(x)*(x - y)^y)\/(exp(y)*(x + 1)^(1\/2)) + ...\r\n    (cos(y)*sin(x))\/((x - y)^x*(x + 1)^(1\/2)) - ...\r\n    (exp(x)*log((x^2 + 1)\/(y^2 + 1)))\/ ... \r\n    (exp(2*y)*(x + 1)^(1\/2)) - exp(x)\/ ...\r\n    (exp(y)*(x - y)^x*(x + 1)^(1\/2)) + ...\r\n    (cos(x)*sin(y)*(x - y)^y)\/(x + 1)^(1\/2) + ...\r\n    (cos(y)*sin(x)*(x - y)^y)\/(x + 1)^(1\/2) + ...\r\n    (log((x^2 + 1)\/(y^2 + 1))*cos(x)*sin(y))\/ ...\r\n    (exp(y)*(x + 1)^(1\/2)) + (log((x^2 + 1)\/ ...\r\n    (y^2 + 1))*cos(y)*sin(x))\/(exp(y)*(x + 1)^(1\/2));\r\n%%\r\n% Now executing the |simplify| command does not seem to be helpful:\r\nS = simplify(log(a)+log(b));\r\npretty(S)\r\n%%\r\n% Assuming $x$ and $y$ to be positive does not significantly improve the \r\n% result either. The reason is that we need to set such assumptions on $x$ \r\n% and $y$ that would make the expressions $a$ and $b$ positive. We can try \r\n% to find the right assumptions for this example, but in general it seems \r\n% like one has to be a genius to guess what's appropriate.  \r\n%% Using Option |IgnoreAnalyticConstraints| for Simplification\r\n% A possible solution to the problem is to ignore certain analytic \r\n% constraints, that is, to use the |IgnoreAnalyticConstraints| option for \r\n% |simplify|. \r\n%\r\n% With this option the simplifier internally applies the following rules: \r\n% \r\n% * $\\log(a) + \\log(b) = \\log(a \\cdot b)$ for all values of $a$ and $b$. In\r\n% particular $(a \\cdot b)^c = a^c \\cdot b^c$ for all values of $a$, $b$ and\r\n% $c$. \r\n% * $\\log(a^b) = b \\cdot \\log(a)$ for all values of $a$ and $b$. In\r\n% particular $(a^b)^c = a^{b \\cdot c}$ for all values of $a$, $b$ and $c$. \r\n% * If $f$ and $g$ are standard math functions and $f(g(x)) = x$ holds\r\n% for all small positive numbers, then $f(g(x)) = x$ is assumed to be valid\r\n% for all $x$ (for example as in case of  $\\mbox{asin}(\\sin(x)) = x$). \r\n% \r\n% So how does this work in our example?\r\nsimplify(log(a)+log(b),'IgnoreAnalyticConstraints',true)\r\n%%\r\n% The result is $0$, because $b = 1\/a$. Hence, under the above\r\n% assumptions, we get $\\log(a) + \\log(b) = \\log(a \\cdot b) = \\log(1) = 0$. \r\n% \r\n% Of course, it is important to keep in mind that the rules applied by\r\n% |IgnoreAnalyticConstraints| are not correct in a strict\r\n% mathematical sense. Nevertheless, in practice these rules are often very\r\n% helpful to get simpler results. Another nice side effect is that ignoring \r\n% some analytic constraints often helps you speed up your computations. \r\n%\r\n% This <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/toolbox\/symbolic\/\/simplify.html\r\n% documentation> describes more details on |IgnoreAnalyticConstraints|.\r\n%% Using |IgnoreAnalyticConstraints| for Equation Solving\r\n% Not surprisingly the concept of ignoring analytic constraints also makes \r\n% sense for equation solving. Imagine that you want to solve the equation\r\n% $\\log(x^n) = 0$ for $x$. Ignoring analytic constraints would certainly\r\n% mean to write this as $n \\cdot \\log(x) = 0$. Assuming $n$ to be nonzero\r\n% we get $\\log(x) = 0$ and, finally, $x = 1$. \r\n%\r\n% Without using any restrictions, Symbolic Math Toolbox returns the\r\n% result:\r\nsyms x n\r\nsolve(log(x^n),x)\r\n%%\r\n% So you get a parameterized solution which strongly depends on the values\r\n% of $n$. This is reasonable, because, for example, for $n = 4$ you get the \r\n% four solutions \r\nsolve(log(x^4),x)\r\n%%\r\n% whereas for $n = \\frac12$ there only is one solution: \r\nsolve(log(x^(1\/2)),x)\r\n%%\r\n% Applying |IgnoreAnalyticConstraints| we get\r\nsolve(log(x^n),x,'IgnoreAnalyticConstraints',true)\r\n%%\r\n% Also for equations involving roots where no additional symbolic\r\n% parameters are present, it may be useful to apply \r\n% |IgnoreAnalyticConstraints| to get simpler results: \r\nsolve(x^(5\/2) - 8^(sym(10\/3)), 'IgnoreAnalyticConstraints', true)\r\n%%\r\n% Here the solver ignores branch cuts during internal simplifications and, \r\n% hence, returns only one solution. \r\n%\r\n% See the MATLAB doc page on \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/toolbox\/symbolic\/\/solve.html solve> for \r\n% further details. \r\n%%\r\n% The |IgnoreAnalyticConstraints| option can also be used for other\r\n% Symbolic Math Toolbox functions like the function  \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/toolbox\/symbolic\/\/int.html int> for doing \r\n% symbolic integration. The option is also available for the related \r\n% MuPAD Notebook Interface functions. \r\n% \r\n%% Have You Tried |IgnoreAnalyticConstraints|?\r\n% Have you tried the |IgnoreAnalyticConstraints| option to get simpler,\r\n% shorter, and easier to handle results? \r\n% \r\n% Let me know <https:\/\/blogs.mathworks.com\/loren\/?p=293#respond\r\n% here>.\r\n\r\n\r\n##### SOURCE END ##### dfaf2d6d91024777b15afd5ba977bfc8\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      I am pleased to introduce guest blogger Kai Gehrs. Kai is a developer for the Symbolic Math Toolbox. His main focus in this post is on special approaches to symbolic simplification and... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2011\/10\/25\/simplifying-symbolic-results\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[38],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/293"}],"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=293"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/293\/revisions"}],"predecessor-version":[{"id":2950,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/293\/revisions\/2950"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}