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:
Solving Ordinary Differential Equations Using String Input
The usual way to solve ordinary differential equations (ODEs) using the Symbolic Math Toolbox dsolvecommand 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:
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
and re-evaluating the command
does not work, since the value for n shows up in the string defining the differential equation:
We more or less have to modify the ODE by hand replacing all appearances of n by 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:
Finally, we should also check the initial values. Do f(1) = 1 and f(2) = n really hold?
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:
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:
Now the dependent ODE variable is declared as a symfun (symbolic function) 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
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
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":
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
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 applysimplify to see if they are 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
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.
No comments:
Post a Comment