Loren on the Art of MATLAB

Using Symbolic Equations And Symbolic Functions In MATLAB

Posted by Loren Shure,

I am pleased to introduce guest blogger Kai Gehrs. Kai is a developer for the Symbolic Math Toolbox.

Contents

In A Nutshell: What Is This Article About?

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.

For demonstration purposes, let's consider the Bessel differential equation. We will see the advantages of symbolic equations and symbolic functions over string input:

  • First, we solve the differential equation using string syntax and point out some limitations of string syntax.
  • Then we compute the same results again using symbolic equations and symbolic functions and discuss how this improves our workflow.

Solving Ordinary Differential Equations Using String Input

The usual way to solve ordinary differential equations (ODEs) using the Symbolic Math Toolbox dsolve command is to set up the equations using string syntax.

Here is a typical example that shows how you can solve a Bessel ODE with two given initial values:

besselODE = 't^2*D2y+t*Dy+(t^2-n^2)*y';
f = dsolve(besselODE,'y(1)=1','y(2)=n','t');
pretty(f)
 
       (n besselj(n, 1) - besselj(n, 2)) bessely(n, t) 
  --------------------------------------------------------- - 
  besselj(n, 1) bessely(n, 2) - besselj(n, 2) bessely(n, 1) 
   
          (n bessely(n, 1) - bessely(n, 2)) besselj(n, t) 
     --------------------------------------------------------- 
     besselj(n, 1) bessely(n, 2) - besselj(n, 2) bessely(n, 1)

Such string-based input for the dsolve function has some limitations. Say you want to assign a special value to n, e.g., n = 1, and solve the equation again using this new value for n. Just defining

n = 1
n =
     1

and re-evaluating the command

f = dsolve(besselODE,'y(1)=1','y(2)=n','t');
pretty(f)
 
       (n besselj(n, 1) - besselj(n, 2)) bessely(n, t) 
  --------------------------------------------------------- - 
  besselj(n, 1) bessely(n, 2) - besselj(n, 2) bessely(n, 1) 
   
          (n bessely(n, 1) - bessely(n, 2)) besselj(n, t) 
     --------------------------------------------------------- 
     besselj(n, 1) bessely(n, 2) - besselj(n, 2) bessely(n, 1)

does not work, since the value for n shows up in the string defining the differential equation:

besselODE
besselODE =
t^2*D2y+t*Dy+(t^2-n^2)*y

We more or less have to modify the ODE by hand replacing all appearances of n by 1:

f = dsolve('t^2*D2y+t*Dy+(t^2-1^2)*y','y(1)=1','y(2)=1','t');
pretty(f)
 
        (besselj(1, 1) - besselj(1, 2)) bessely(1, t) 
  --------------------------------------------------------- - 
  besselj(1, 1) bessely(1, 2) - besselj(1, 2) bessely(1, 1) 
   
           (bessely(1, 1) - bessely(1, 2)) besselj(1, t) 
     --------------------------------------------------------- 
     besselj(1, 1) bessely(1, 2) - besselj(1, 2) bessely(1, 1)

Another limitation becomes obvious when we try to check the solution.

In order to verify that the solution is correct, we need to plug it into the ODE and see if this gives 0. But because the string input does not let us use subs to directly plug the solution into the ODE, we, again, have to do some manual work:

syms t;
result = simplify(t^2*diff(f,2)+t*diff(f)+(t^2-n^2)*f)
result =
0

Finally, we should also check the initial values. Do f(1) = 1 and f(2) = n really hold?

checkInitCond1 = subs(f,t,1)
checkInitCond2 = subs(f,t,2)
checkInitCond1 =
     1
checkInitCond2 =
     1

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:

  • When using variables inside a string and afterwards assigning values to these variables, the values do not show up in the string.
  • Verification of solutions and initial conditions is not convenient, since we cannot use the subs command on string representations of ODEs.

In R2012a, symbolic equations and symbolic functions were introduced in the Symbolic Math Toolbox.

These new features let you make the worklfow for solving ODEs and testing solutions much more smooth and convenient.

Improving Our Workflow Using Symbolic Functions And Symbolic Equations

Before starting with any new computations, let us clean up the workspace:

clear all;

Now the dependent ODE variable is declared as a symfun (symbolic function) y(t):

syms n y(t);

After executing the last command, we can see the symfun y as well as the symbolic variables n and t in the MATLAB workspace. Now we define the Bessel ODE by typing

besselODE = t^2*diff(y,2) + t*diff(y) + (t^2-n^2)*y == 0;

Note that we used diff to introduce the symbolic derivatives of y and the == sign to set up a symbolic equation.

Now we can easily solve the original initial value problem by typing

f(t) = dsolve(besselODE, y(1)==1, y(2)==n);
pretty(f(t));
 
       (n besselj(n, 1) - besselj(n, 2)) bessely(n, t) 
  --------------------------------------------------------- - 
  besselj(n, 1) bessely(n, 2) - besselj(n, 2) bessely(n, 1) 
   
          (n bessely(n, 1) - bessely(n, 2)) besselj(n, t) 
     --------------------------------------------------------- 
     besselj(n, 1) bessely(n, 2) - besselj(n, 2) bessely(n, 1)

Here we have stated the initial values making use of symbolic functions and symbolic equations as well.

Note that in the call to dsolve, y(1) and y(2) do not mean indexing, but function evaluation - just like when you write sin(pi) to evaluate sin(x) at x = pi.

But the main benefits of using symbolic functions and symbolic equations become obvious when we switch to using n = 1, solve the ODE again, and then verify our solution.

We can now use subs to automatically introduce the new value for n in the definition of besselODE. Afterwards solving the ODE works "out of the box":

n = 1;
besselODE = subs(besselODE);
f(t) = dsolve(besselODE,y(1)==1,y(2)==n);
pretty(f(t))
 
        (besselj(1, 1) - besselj(1, 2)) bessely(1, t) 
  --------------------------------------------------------- - 
  besselj(1, 1) bessely(1, 2) - besselj(1, 2) bessely(1, 1) 
   
           (bessely(1, 1) - bessely(1, 2)) besselj(1, t) 
     --------------------------------------------------------- 
     besselj(1, 1) bessely(1, 2) - besselj(1, 2) bessely(1, 1)

Now let us check whether f is a solution of the Bessel ODE. We use subs to plug the solution into the ODE. The result is not simplified. Using simplify, we directly get

result = subs(besselODE,[y,diff(y),diff(y,2)],[f,diff(f),diff(f,2)]);
s = simplify(result(t))
s =
TRUE

This way we have made sure that f is a solution of the Bessel ODE.

Finally, we need to check whether f satisfies the initial values. Since f is a symfun, we can evaluate f at t = 1 and t = 2 simply as f(1) and f(2). We set up the equations corresponding to the initial values and apply simplify to see if they are true:

checkInitCond1 = simplify(f(1) == 1)
checkInitCond2 = simplify(f(2) == 1)
checkInitCond1 =
TRUE
checkInitCond2 =
TRUE

As we can see, the results are correct.

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., solve.

Summary

  • We have seen that the Symbolic Math Toolbox lets us define symbolic equations via the == operator.
  • We have learned that by writing syms y(t) we can define a symbolic function y in the variable t.
  • Combining symbolic equations with symbolic functions lets us set up ODEs in a convenient way.
  • Using symbolic functions lets us write math formulas and equations in a less technical way which looks similar to textbook notation.

For further details on symbolic functions, see the MATLAB documentation page on symfun.

Also, take a look at Loren Shure's article ODEs, from Symbolic to Numeric Code. Loren shows you how you might generate ODE solutions in the case where a symbolic closed-form solution can be found.

Have You Tried Symbolic Equations And Symbolic Functions?

Have you tried the new Symbolic Math Toolbox features to make your workflows smoother? Let me know here.


Get the MATLAB code

Published with MATLAB® 7.14

Comments are closed.

These postings are the author's and don't necessarily represent the opinions of MathWorks.