{"id":504,"date":"2012-07-27T08:04:33","date_gmt":"2012-07-27T13:04:33","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=504"},"modified":"2012-07-27T08:04:33","modified_gmt":"2012-07-27T13:04:33","slug":"using-symbolic-equations-and-symbolic-functions-in-matlab","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2012\/07\/27\/using-symbolic-equations-and-symbolic-functions-in-matlab\/","title":{"rendered":"Using Symbolic Equations And Symbolic Functions In MATLAB"},"content":{"rendered":"<!DOCTYPE html\r\n  PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN\">\r\n<style type=\"text\/css\">\r\n\r\nh1 { font-size:18pt; }\r\nh2.titlebg { font-size:13pt; }\r\nh3 { color:#4A4F55; padding:0px; margin:5px 0px 5px; font-family:Arial, Helvetica, sans-serif; font-size:11pt; font-weight:bold; line-height:140%; border-bottom:1px solid #d6d4d4; display:block; }\r\nh4 { color:#4A4F55; padding:0px; margin:0px 0px 5px; font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-weight:bold; line-height:140%; border-bottom:1px solid #d6d4d4; display:block; }\r\n   \r\np { padding:0px; margin:0px 0px 20px; }\r\nimg { padding:0px; margin:0px 0px 20px; border:none; }\r\np img, pre img, tt img, li img { margin-bottom:0px; } \r\n\r\nul { padding:0px; margin:0px 0px 20px 23px; list-style:square; }\r\nul li { padding:0px; margin:0px 0px 7px 0px; background:none; }\r\nul li ul { padding:5px 0px 0px; margin:0px 0px 7px 23px; }\r\nul li ol li { list-style:decimal; }\r\nol { padding:0px; margin:0px 0px 20px 0px; list-style:decimal; }\r\nol li { padding:0px; margin:0px 0px 7px 23px; list-style-type:decimal; }\r\nol li ol { padding:5px 0px 0px; margin:0px 0px 7px 0px; }\r\nol li ol li { list-style-type:lower-alpha; }\r\nol li ul { padding-top:7px; }\r\nol li ul li { list-style:square; }\r\n\r\npre, tt, code { font-size:12px; }\r\npre { margin:0px 0px 20px; }\r\npre.error { color:red; }\r\npre.codeinput { padding:10px; border:1px solid #d3d3d3; background:#f7f7f7; }\r\npre.codeoutput { padding:10px 11px; margin:0px 0px 20px; color:#4c4c4c; }\r\n\r\n@media print { pre.codeinput, pre.codeoutput { word-wrap:break-word; width:100%; } }\r\n\r\nspan.keyword { color:#0000FF }\r\nspan.comment { color:#228B22 }\r\nspan.string { color:#A020F0 }\r\nspan.untermstring { color:#B20000 }\r\nspan.syscmd { color:#B28C00 }\r\n\r\n.footer { width:auto; padding:10px 0px; margin:25px 0px 0px; border-top:1px dotted #878787; font-size:0.8em; line-height:140%; font-style:italic; color:#878787; text-align:left; float:none; }\r\n.footer p { margin:0px; }\r\n\r\n  <\/style><div class=\"content\"><!--introduction--><p>I am pleased to introduce guest blogger Kai Gehrs. Kai is a developer for the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/symbolic\/\">Symbolic Math Toolbox<\/a>.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#6bfa0157-b1bf-40aa-bc3a-2224e890ddb2\">In A Nutshell: What Is This Article About?<\/a><\/li><li><a href=\"#b9513543-4897-4b92-bfa3-41cc51fc3284\">Solving Ordinary Differential Equations Using String Input<\/a><\/li><li><a href=\"#8ce21b02-66d9-436e-a107-93d2fca81355\">Improving Our Workflow Using Symbolic Functions And Symbolic Equations<\/a><\/li><li><a href=\"#98469368-5da6-4eba-8f27-ebbe8d1afe97\">Summary<\/a><\/li><li><a href=\"#b99615aa-0d74-49b6-afb4-e24a0e053076\">Have You Tried Symbolic Equations And Symbolic Functions?<\/a><\/li><\/ul><\/div><h4>In A Nutshell: What Is This Article About?<a name=\"6bfa0157-b1bf-40aa-bc3a-2224e890ddb2\"><\/a><\/h4><p>This article discusses how symbolic workflows can be improved by using symbolic equations and symbolic functions in MATLAB. Symbolic equations and symbolic functions were introduced in the Symbolic Math Toolbox in Release 2012a.<\/p><p>For demonstration purposes, let's consider the <i>Bessel differential equation<\/i>. We will see the advantages of symbolic equations and symbolic functions over string input:<\/p><div><ul><li>First, we solve the differential equation using string syntax and point out some limitations of string syntax.<\/li><li>Then we compute the same results again using symbolic equations and symbolic functions and discuss how this improves our workflow.<\/li><\/ul><\/div><h4>Solving Ordinary Differential Equations Using String Input<a name=\"b9513543-4897-4b92-bfa3-41cc51fc3284\"><\/a><\/h4><p>The usual way to solve ordinary differential equations (ODEs) using the Symbolic Math Toolbox <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/symbolic\/\/dsolve.html\"><tt>dsolve<\/tt><\/a> command is to set up the equations using string syntax.<\/p><p>Here is a typical example that shows how you can solve a Bessel ODE with two given initial values:<\/p><pre class=\"codeinput\">besselODE = <span class=\"string\">'t^2*D2y+t*Dy+(t^2-n^2)*y'<\/span>;\r\nf = dsolve(besselODE,<span class=\"string\">'y(1)=1'<\/span>,<span class=\"string\">'y(2)=n'<\/span>,<span class=\"string\">'t'<\/span>);\r\npretty(f)\r\n<\/pre><pre class=\"codeoutput\"> \r\n       (n besselj(n, 1) - besselj(n, 2)) bessely(n, t) \r\n  --------------------------------------------------------- - \r\n  besselj(n, 1) bessely(n, 2) - besselj(n, 2) bessely(n, 1) \r\n   \r\n          (n bessely(n, 1) - bessely(n, 2)) besselj(n, t) \r\n     --------------------------------------------------------- \r\n     besselj(n, 1) bessely(n, 2) - besselj(n, 2) bessely(n, 1)\r\n<\/pre><p>Such string-based input for the <tt>dsolve<\/tt> function has some limitations. Say you want to assign a special value to <tt>n<\/tt>, e.g., <tt>n = 1<\/tt>, and solve the equation again using this new value for <tt>n<\/tt>. Just defining<\/p><pre class=\"codeinput\">n = 1\r\n<\/pre><pre class=\"codeoutput\">n =\r\n     1\r\n<\/pre><p>and re-evaluating the command<\/p><pre class=\"codeinput\">f = dsolve(besselODE,<span class=\"string\">'y(1)=1'<\/span>,<span class=\"string\">'y(2)=n'<\/span>,<span class=\"string\">'t'<\/span>);\r\npretty(f)\r\n<\/pre><pre class=\"codeoutput\"> \r\n       (n besselj(n, 1) - besselj(n, 2)) bessely(n, t) \r\n  --------------------------------------------------------- - \r\n  besselj(n, 1) bessely(n, 2) - besselj(n, 2) bessely(n, 1) \r\n   \r\n          (n bessely(n, 1) - bessely(n, 2)) besselj(n, t) \r\n     --------------------------------------------------------- \r\n     besselj(n, 1) bessely(n, 2) - besselj(n, 2) bessely(n, 1)\r\n<\/pre><p>does not work, since the value for <tt>n<\/tt> shows up in the string defining the differential equation:<\/p><pre class=\"codeinput\">besselODE\r\n<\/pre><pre class=\"codeoutput\">besselODE =\r\nt^2*D2y+t*Dy+(t^2-n^2)*y\r\n<\/pre><p>We more or less have to modify the ODE by hand replacing all appearances of <tt>n<\/tt> by <tt>1<\/tt>:<\/p><pre class=\"codeinput\">f = dsolve(<span class=\"string\">'t^2*D2y+t*Dy+(t^2-1^2)*y'<\/span>,<span class=\"string\">'y(1)=1'<\/span>,<span class=\"string\">'y(2)=1'<\/span>,<span class=\"string\">'t'<\/span>);\r\npretty(f)\r\n<\/pre><pre class=\"codeoutput\"> \r\n        (besselj(1, 1) - besselj(1, 2)) bessely(1, t) \r\n  --------------------------------------------------------- - \r\n  besselj(1, 1) bessely(1, 2) - besselj(1, 2) bessely(1, 1) \r\n   \r\n           (bessely(1, 1) - bessely(1, 2)) besselj(1, t) \r\n     --------------------------------------------------------- \r\n     besselj(1, 1) bessely(1, 2) - besselj(1, 2) bessely(1, 1)\r\n<\/pre><p>Another limitation becomes obvious when we try to check the solution.<\/p><p>In order to verify that the solution is correct, we need to plug it into the ODE and see if this gives <tt>0<\/tt>. But because the string input does not let us use <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/symbolic\/\/subs.html\"><tt>subs<\/tt><\/a> to directly plug the solution into the ODE, we, again, have to do some manual work:<\/p><pre class=\"codeinput\">syms <span class=\"string\">t<\/span>;\r\nresult = simplify(t^2*diff(f,2)+t*diff(f)+(t^2-n^2)*f)\r\n<\/pre><pre class=\"codeoutput\">result =\r\n0\r\n<\/pre><p>Finally, we should also check the initial values. Do <tt>f(1) = 1<\/tt> and <tt>f(2) = n<\/tt> really hold?<\/p><pre class=\"codeinput\">checkInitCond1 = subs(f,t,1)\r\ncheckInitCond2 = subs(f,t,2)\r\n<\/pre><pre class=\"codeoutput\">checkInitCond1 =\r\n     1\r\ncheckInitCond2 =\r\n     1\r\n<\/pre><p>We have solved the ODE, and we have checked the correctness of the solutions. But our workflow was not completely smooth. Manual work has been required to overcome these limitations of string syntax:<\/p><div><ul><li>When using variables inside a string and afterwards assigning values to these variables, the values do not show up in the string.<\/li><li>Verification of solutions and initial conditions is not convenient, since we cannot use the <tt>subs<\/tt> command on string representations of ODEs.<\/li><\/ul><\/div><p>In R2012a, symbolic equations and symbolic functions were introduced in the Symbolic Math Toolbox.<\/p><p>These new features let you make the worklfow for solving ODEs and testing solutions much more smooth and convenient.<\/p><h4>Improving Our Workflow Using Symbolic Functions And Symbolic Equations<a name=\"8ce21b02-66d9-436e-a107-93d2fca81355\"><\/a><\/h4><p>Before starting with any new computations, let us clean up the workspace:<\/p><pre class=\"codeinput\">clear <span class=\"string\">all<\/span>;\r\n<\/pre><p>Now the dependent ODE variable is declared as a <tt>symfun<\/tt> (symbolic function) <tt>y(t)<\/tt>:<\/p><pre class=\"codeinput\">syms <span class=\"string\">n<\/span> <span class=\"string\">y(t)<\/span>;\r\n<\/pre><p>After executing the last command, we can see the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/symbolic\/\/symfun.html\"><tt>symfun<\/tt><\/a> <tt>y<\/tt> as well as the symbolic variables <tt>n<\/tt> and <tt>t<\/tt> in the MATLAB workspace. Now we define the Bessel ODE by typing<\/p><pre class=\"codeinput\">besselODE = t^2*diff(y,2) + t*diff(y) + (t^2-n^2)*y == 0;\r\n<\/pre><p>Note that we used <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/symbolic\/\/diff.html\"><tt>diff<\/tt><\/a> to introduce the symbolic derivatives of <tt>y<\/tt> and the <tt>==<\/tt> sign to set up a symbolic equation.<\/p><p>Now we can easily solve the original initial value problem by typing<\/p><pre class=\"codeinput\">f(t) = dsolve(besselODE, y(1)==1, y(2)==n);\r\npretty(f(t));\r\n<\/pre><pre class=\"codeoutput\"> \r\n       (n besselj(n, 1) - besselj(n, 2)) bessely(n, t) \r\n  --------------------------------------------------------- - \r\n  besselj(n, 1) bessely(n, 2) - besselj(n, 2) bessely(n, 1) \r\n   \r\n          (n bessely(n, 1) - bessely(n, 2)) besselj(n, t) \r\n     --------------------------------------------------------- \r\n     besselj(n, 1) bessely(n, 2) - besselj(n, 2) bessely(n, 1)\r\n<\/pre><p>Here we have stated the initial values making use of symbolic functions and symbolic equations as well.<\/p><p>Note that in the call to <tt>dsolve<\/tt>, <tt>y(1)<\/tt> and <tt>y(2)<\/tt> do not mean indexing, but function evaluation - just like when you write <tt>sin(pi)<\/tt> to evaluate <tt>sin(x)<\/tt> at <tt>x = pi<\/tt>.<\/p><p>But the main benefits of using symbolic functions and symbolic equations become obvious when we switch to using <tt>n = 1<\/tt>, solve the ODE again, and then verify our solution.<\/p><p>We can now use <tt>subs<\/tt> to automatically introduce the new value for <tt>n<\/tt> in the definition of <tt>besselODE<\/tt>. Afterwards solving the ODE works \"out of the box\":<\/p><pre class=\"codeinput\">n = 1;\r\nbesselODE = subs(besselODE);\r\nf(t) = dsolve(besselODE,y(1)==1,y(2)==n);\r\npretty(f(t))\r\n<\/pre><pre class=\"codeoutput\"> \r\n        (besselj(1, 1) - besselj(1, 2)) bessely(1, t) \r\n  --------------------------------------------------------- - \r\n  besselj(1, 1) bessely(1, 2) - besselj(1, 2) bessely(1, 1) \r\n   \r\n           (bessely(1, 1) - bessely(1, 2)) besselj(1, t) \r\n     --------------------------------------------------------- \r\n     besselj(1, 1) bessely(1, 2) - besselj(1, 2) bessely(1, 1)\r\n<\/pre><p>Now let us check whether <tt>f<\/tt> is a solution of the Bessel ODE. We use <tt>subs<\/tt> to plug the solution into the ODE. The <tt>result<\/tt> is not simplified. Using <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/symbolic\/\/simplify.html\"><tt>simplify<\/tt><\/a>, we directly get<\/p><pre class=\"codeinput\">result = subs(besselODE,[y,diff(y),diff(y,2)],[f,diff(f),diff(f,2)]);\r\ns = simplify(result(t))\r\n<\/pre><pre class=\"codeoutput\">s =\r\nTRUE\r\n<\/pre><p>This way we have made sure that <tt>f<\/tt> is a solution of the Bessel ODE.<\/p><p>Finally, we need to check whether <tt>f<\/tt> satisfies the initial values. Since <tt>f<\/tt> is a <tt>symfun<\/tt>, we can evaluate <tt>f<\/tt> at <tt>t = 1<\/tt> and <tt>t = 2<\/tt> simply as <tt>f(1)<\/tt> and <tt>f(2)<\/tt>. We set up the equations corresponding to the initial values and apply <tt>simplify<\/tt> to see if they are true:<\/p><pre class=\"codeinput\">checkInitCond1 = simplify(f(1) == 1)\r\ncheckInitCond2 = simplify(f(2) == 1)\r\n<\/pre><pre class=\"codeoutput\">checkInitCond1 =\r\nTRUE\r\ncheckInitCond2 =\r\nTRUE\r\n<\/pre><p>As we can see, the results are correct.<\/p><p>Even if you are not very interested in differential equations, keep in mind that symbolic equations and symbolic functions can help you define inputs for other Symbolic Math Toolbox functions, e.g., <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/symbolic\/\/solve.html\"><tt>solve<\/tt><\/a>.<\/p><h4>Summary<a name=\"98469368-5da6-4eba-8f27-ebbe8d1afe97\"><\/a><\/h4><div><ul><li>We have seen that the Symbolic Math Toolbox lets us define symbolic equations via the <tt>==<\/tt> operator.<\/li><li>We have learned that by writing <tt>syms y(t)<\/tt> we can define a symbolic function <tt>y<\/tt> in the variable <tt>t<\/tt>.<\/li><li>Combining symbolic equations with symbolic functions lets us set up ODEs in a convenient way.<\/li><li>Using symbolic functions lets us write math formulas and equations in a less technical way which looks similar to textbook notation.<\/li><\/ul><\/div><p>For further details on symbolic functions, see the MATLAB documentation page on <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/symbolic\/\/symfun.html\">symfun<\/a>.<\/p><p>Also, take a look at Loren Shure's article <a href=\"https:\/\/blogs.mathworks.com\/loren\/2010\/04\/08\/odes-from-symbolic-to-numeric-code\">ODEs, from Symbolic to Numeric Code<\/a>. Loren shows you how you might generate ODE solutions in the case where a symbolic closed-form solution can be found.<\/p><h4>Have You Tried Symbolic Equations And Symbolic Functions?<a name=\"b99615aa-0d74-49b6-afb4-e24a0e053076\"><\/a><\/h4><p>Have you tried the new Symbolic Math Toolbox features to make your workflows smoother? Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=504#respond\">here<\/a>.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_6582ca2a194a4e239602b80aafe00965() {\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='6582ca2a194a4e239602b80aafe00965 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 6582ca2a194a4e239602b80aafe00965';\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        copyright = 'Copyright 2012 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 copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\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     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_6582ca2a194a4e239602b80aafe00965()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.14<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; 7.14<br><\/p><\/div><!--\r\n6582ca2a194a4e239602b80aafe00965 ##### SOURCE BEGIN #####\r\n%% Using Symbolic Equations And Symbolic Functions In MATLAB \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\/R2012a\/toolbox\/symbolic\/ Symbolic Math Toolbox>. \r\n%\r\n%% In A Nutshell: What Is This Article About?\r\n% This article discusses how symbolic workflows can be improved by using\r\n% symbolic equations and symbolic functions in MATLAB. Symbolic equations \r\n% and symbolic functions were introduced in the Symbolic Math Toolbox\r\n% in Release 2012a.  \r\n%\r\n% For demonstration purposes, \r\n% let's consider the _Bessel differential equation_. We will see the \r\n% advantages of symbolic equations and symbolic functions over string\r\n% input: \r\n% \r\n% * First, we solve the differential equation using string syntax and  \r\n% point out some limitations of string syntax. \r\n% * Then we compute the same results again using symbolic equations and \r\n% symbolic functions and discuss how this improves our workflow.\r\n%\r\n%% Solving Ordinary Differential Equations Using String Input \r\n%\r\n% The usual way to solve ordinary differential equations (ODEs) using the \r\n% Symbolic Math Toolbox  \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/symbolic\/\/dsolve.html |dsolve|> \r\n% command is to set up the equations using string syntax. \r\n%\r\n% Here is a typical example that shows how you can solve a Bessel ODE with \r\n% two given initial values: \r\nbesselODE = 't^2*D2y+t*Dy+(t^2-n^2)*y';\r\nf = dsolve(besselODE,'y(1)=1','y(2)=n','t');\r\npretty(f)\r\n%%\r\n% Such string-based input for the |dsolve| function has some limitations. \r\n% Say you want to assign a special value to |n|, e.g., |n = 1|, and \r\n% solve the equation again using this new value for |n|. Just defining  \r\nn = 1\r\n%%\r\n% and re-evaluating the command \r\nf = dsolve(besselODE,'y(1)=1','y(2)=n','t');\r\npretty(f)\r\n%%\r\n% does not work, since the value for |n| shows up in the string defining \r\n% the differential equation: \r\nbesselODE\r\n%%\r\n% We more or less have to modify the ODE by hand replacing all appearances \r\n% of |n| by |1|: \r\nf = dsolve('t^2*D2y+t*Dy+(t^2-1^2)*y','y(1)=1','y(2)=1','t');\r\npretty(f)\r\n%%\r\n% Another limitation becomes obvious when we try to check the solution. \r\n%\r\n% In order to verify that the solution is correct, we need to plug it into \r\n% the ODE and see if this gives |0|. But because the string input does not \r\n% let us use  \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/symbolic\/\/subs.html |subs|> \r\n% to directly plug the solution into the ODE, we, again, have to do some \r\n% manual work:  \r\nsyms t;\r\nresult = simplify(t^2*diff(f,2)+t*diff(f)+(t^2-n^2)*f)\r\n%%\r\n% Finally, we should also check the initial values. Do |f(1) = 1| and |f(2) = n|\r\n% really hold?\r\ncheckInitCond1 = subs(f,t,1)\r\ncheckInitCond2 = subs(f,t,2)\r\n%%\r\n% We have solved the ODE, and we have checked the correctness of the\r\n% solutions. But our workflow was not completely smooth. Manual work \r\n% has been required to overcome these limitations of string syntax:  \r\n% \r\n% * When using variables inside a string and afterwards assigning values to\r\n% these variables, the values do not show up in the string. \r\n% * Verification of solutions and initial conditions is not convenient,\r\n% since we cannot use the |subs| command on string representations of\r\n% ODEs. \r\n%\r\n% In R2012a, symbolic equations and symbolic functions were introduced\r\n% in the Symbolic Math Toolbox. \r\n%\r\n% These new features let you make the worklfow for solving ODEs and testing \r\n% solutions much more smooth and convenient.\r\n%\r\n%% Improving Our Workflow Using Symbolic Functions And Symbolic Equations\r\n%\r\n% Before starting with any new computations, let us clean up the workspace: \r\nclear all; \r\n%%\r\n% Now the dependent ODE variable is declared as a |symfun| (symbolic \r\n% function) |y(t)|: \r\nsyms n y(t);\r\n%%\r\n% After executing the last command, we can see the \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/symbolic\/\/symfun.html |symfun|>\r\n% |y| as well as the symbolic variables |n| and |t| in the MATLAB \r\n% workspace. Now we define the Bessel ODE by typing \r\n%\r\nbesselODE = t^2*diff(y,2) + t*diff(y) + (t^2-n^2)*y == 0;\r\n%%\r\n% Note that we used \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/symbolic\/\/diff.html |diff|>  \r\n% to introduce the symbolic derivatives of |y|\r\n% and the |==| sign to set up a symbolic equation. \r\n%\r\n% Now we can easily solve the original initial value problem by typing  \r\nf(t) = dsolve(besselODE, y(1)==1, y(2)==n);\r\npretty(f(t));\r\n%%\r\n% Here we have stated the initial values making use of symbolic functions\r\n% and symbolic equations as well. \r\n% \r\n% Note that in the call to |dsolve|, |y(1)| and |y(2)| do not mean indexing,  \r\n% but function evaluation - just like when you write |sin(pi)| to evaluate \r\n% |sin(x)| at |x = pi|. \r\n%\r\n% But the main benefits of using symbolic functions and symbolic\r\n% equations become obvious when we switch to using |n = 1|, solve the ODE\r\n% again, and then verify our solution. \r\n%\r\n% We can now use |subs| to automatically introduce the new value for |n| \r\n% in the definition of |besselODE|. Afterwards solving the ODE works \r\n% \"out of the box\": \r\n%\r\nn = 1; \r\nbesselODE = subs(besselODE);\r\nf(t) = dsolve(besselODE,y(1)==1,y(2)==n);\r\npretty(f(t))\r\n%%\r\n% Now let us check whether |f| is a solution of the Bessel ODE. We use |subs|\r\n% to plug the solution into the ODE. The |result| is not simplified. Using \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/symbolic\/\/simplify.html |simplify|>, \r\n% we directly get\r\nresult = subs(besselODE,[y,diff(y),diff(y,2)],[f,diff(f),diff(f,2)]);\r\ns = simplify(result(t))\r\n%%\r\n% This way we have made sure that |f| is a solution of the Bessel ODE. \r\n% \r\n% Finally, we need to check whether |f| satisfies the initial values. Since \r\n% |f| is a |symfun|, we can evaluate |f| at |t = 1| and |t = 2| simply as\r\n% |f(1)| and |f(2)|. We set up the equations corresponding to the initial \r\n% values and apply |simplify| to see if they are true:\r\ncheckInitCond1 = simplify(f(1) == 1)\r\ncheckInitCond2 = simplify(f(2) == 1)\r\n%%\r\n% As we can see, the results are correct. \r\n% \r\n% Even if you are not very interested in differential equations, keep in\r\n% mind that symbolic equations and symbolic functions can help you \r\n% define inputs for other Symbolic Math Toolbox functions, e.g., \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/symbolic\/\/solve.html |solve|>.\r\n%% Summary \r\n% \r\n% * We have seen that the Symbolic Math Toolbox lets us define symbolic \r\n% equations via the |==| operator.\r\n% * We have learned that by writing |syms y(t)| \r\n% we can define a symbolic function |y| in the variable |t|. \r\n% * Combining symbolic\r\n% equations with symbolic functions lets us set up ODEs in a convenient\r\n% way. \r\n% * Using symbolic functions lets us write math formulas and equations in a \r\n% less technical way which looks similar to textbook notation. \r\n%\r\n% For further details on symbolic functions, see the MATLAB documentation page on \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/symbolic\/\/symfun.html symfun>. \r\n%\r\n% Also, take a look at Loren Shure's article  \r\n% <https:\/\/blogs.mathworks.com\/loren\/2010\/04\/08\/odes-from-symbolic-to-numeric-code ODEs, from Symbolic to Numeric Code>.\r\n% Loren shows you how you might generate ODE solutions in the case where \r\n% a symbolic closed-form solution can be found. \r\n%% Have You Tried Symbolic Equations And Symbolic Functions?\r\n% Have you tried the new Symbolic Math Toolbox features to make your \r\n% workflows smoother? Let me know \r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=504#respond here>.\r\n\r\n\r\n##### SOURCE END ##### 6582ca2a194a4e239602b80aafe00965\r\n-->","protected":false},"excerpt":{"rendered":"<!--introduction--><p>I am pleased to introduce guest blogger Kai Gehrs. Kai is a developer for the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/toolbox\/symbolic\/\">Symbolic Math Toolbox<\/a>.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2012\/07\/27\/using-symbolic-equations-and-symbolic-functions-in-matlab\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[6,38],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/504"}],"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=504"}],"version-history":[{"count":5,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/504\/revisions"}],"predecessor-version":[{"id":509,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/504\/revisions\/509"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}