# MATLAB Programming/Advanced Topics/Toolboxes and Extensions/Symbolic Toolbox

[MATLAB Programming\|/MATLAB Programming]m]
Chapter 1: MATLAB ._.
    Fundamentals of MATLAB
    MATLAB Workspace
    MATLAB Variables
    *.mat files
Chapter 2: MATLAB Concepts
    MATLAB operator
    Data File I/O
Chapter 3: Variable Manipulation
    Numbers and Booleans
    Strings
    Portable Functions
    Complex Numbers
Chapter 4: Vector and matrices
    Vector and Matrices
    Special Matrices
    Operation on Vectors
    Operation on Matrices
    Sparse Matrices
Chapter 5: Array
    Arrays
    Introduction to array operations
    Vectors and Basic Vector Operations
    Mathematics with Vectors and Matrices
    Struct Arrays
    Cell Arrays
Chapter 6: Graphical Plotting
    Basic Graphics Commands
    Plot
    Polar Plot
    Semilogx or Semilogy
    Loglog
    Bode Plot
    Nichols Plot
    Nyquist Plot
Chapter 7: M File Programming
    Scripts
    Comments
    The Input Function
    Control Flow
    Loops and Branches
    Error Messages
    Debugging M Files
Chapter 8: Advanced Topics
    Numerical Manipulation
    Advanced File I/O
    Object Oriented Programming
    Applications and Examples
    Toolboxes and Extensions
Chapter 9: Bonus chapters
    MATLAB Benefits and Caveats
    Alternatives to MATLAB
    [MATLAB_Programming/GNU_Octave|What is Octave= (8) hsrmonic functions]
    Octave/MATLAB differences

## Introduction to the Symbolic Math Toolbox

The symbolic toolbox is a bit difficult to use but it is of great utility in applications in which symbolic expressions are necessary for reasons of accuracy in calculations. The toolbox simply calls the MAPLE kernel with whatever symbolic expressions you have declared, and then returns a (usually symbolic) expression back to MATLAB. It is important to remember that MAPLE is not a numeric engine, which means that there are certain things it doesn't let you do that MATLAB can do. Rather, it is useful as a supplement to provide functions which MATLAB, as a numerical engine, has difficulty with.
The symbolic math toolbox takes some time to initialize, so if nothing happens for a few seconds after you declare your first symbolic variable of the session, it doesn't mean you did anything wrong.
The MATLAB student version comes with a copy of the symbolic math toolbox.

## Symbolic Variables

You can declare a single symbolic variable using the 'sym' function as follows.
You can create arrays of symbolic expressions like everything else:
Symbolic variables can also be declared many at a time using the 'syms' function. By default, the symbolic variables created have the same names as the arguments of the 'syms' function. The following creates three symbolic variables, a b and c.

## Symbolic Numbers

Symbolic numbers allow exact representations of fractions, intended to help avoid rounding errors and representation errors. This section helps explain how to declare them.
If you try to add a number into a symbolic array it will automatically turn it into a symbolic number.
Symbolic numbers can also be declared using the syntax a(3) = sym('number'). The difference between symbolic numbers and normal MATLAB numbers is that, if possible, MAPLE will keep the symbolic number as a fraction, which is an exact representation of the answer. For example, to represent the number 0.5 as a fraction, you can use:
Here, of course, MATLAB would normally return 0.5. To make MATLAB change this back into a 'double', type:
Other class conversions are possible as well; for instance, to change it into a string use the 'char' function. There is no function to directly change a symbolic variable into a function handle, unfortunately.
A caveat: Making a symbolic variable of negative exponentials can create problems if you don't use the correct syntax. You cannot do this:
Instead, you must do this:
MAPLE is thus more picky about what operators you can use than MATLAB.

## Symbolic Functions

You can create functions of symbolic variables, not just the variables themselves. This is probably the most intuitive way to do it:
If you do it this way, you can then subsequently perform substitution, differentiation, and so on with respect to any one of these variables.
If you want to create an actual function, it's not much harder:

### Substituting Values into Symbolic Variables

Substitutions can be made into functions of symbolic variables. Suppose you defined the function f = a + b + c and wish to substitute a = 3 into f. You can do this with the following syntax:
Notice the form of this function call. The first argument is the name of the function you wish to substitute into. The second can be either the name of the symbolic variable you want to plug in for or its present value, but if you want to avoid confusion they should be the same anyway. The third argument is the value you want to plug in for that variable.
The value you're plugging in need not be a number. You can also plug in other variables (including those already present in the function) by using strings. Using the same f:
If x is already a symbolic variable you can omit the quotes (but if it's not you'll get an undefined variable error):
Multiple substitutions are allowed; to do it, just declare each of them as an array. For example, to plug in 1 for a and 2 for b use:
Finally, if you substitute for all of the symbolic values in a function MATLAB automatically changes the value back into a double so that you can manipulate it in the MATLAB workspace.

### Using Functions with Symbolic Matrices as Inputs

Unfortunately, symfun does not seem to allow using a symbolic matrix as a function input. You can manually add each element of the matrix to the list of input variables, or even use subs to iteratively substitute the input values if you're just using a symbolic expression, but both solutions would require typing out all of the indexing. Instead, the simplest path is to declare an anonymous function:

### Algebraic Function Manipulations

The symbolic math toolbox allows you several different ways to manipulate functions. First off you can factor a function using 'factor' and multiply it out using 'expand':
The 'collect' function does the same thing as the 'expand' function but only affects polynomial terms. 'Expand' can also be used to expand trigonometric and logarithmic/exponential functions with the appropriate identities.
The Horner (nested) representation for a function is given by 'horner':
This representation has a relatively low number of operations required for its evaluation compared to the expanded version and is therefore helpful in making calculations more efficient.
A common problem with symbolic calculations is that the answer returned is often not in its simplest form. MATLAB's function 'simple' will perform all of the possible function manipulations and then return the one that is the shortest. To do this do something like:

## Algebraic Equations

The symbolic math toolbox is able to solve an algebraic expression for any variable, provided that it is mathematically possible to do so. It can also solve both single equations and algebraic systems.

### Solving Algebraic Equations With a Single Variable

MATLAB uses the 'solve' function to solve an algebraic equation. The syntax is solve(f, var) where f is the function you wish to solve and var is the variable to solve for. If f is a function of a single variable you will get a number, while if it is multiple variables you will get a symbolic expression.
First, let us say we want to solve the quadratic equation x^2 = 16 for x. The solutions are x = -4 and x = 4. To do this, you can put the function into 'solve' directly, or you can define a function in terms of x to solve and pass that into the 'solve' function. The first method is rather intuitive:
Either of these two syntax works. The first must be in quotes or you get an 'invalid assignment' error. In the second, x must be defined as a symbolic variable beforehand or you get an 'undefined variable' error.
For the second method you assign a dummy variable to the equation you want to solve like this:
Note that since MATLAB assumes that y = 0 when you're solving the equation, you must subtract 16 from both sides to put the equation into normal form.

### Solving Symbolic Functions for Particular Variables

The format for doing this is similar to that for solving for a single variable, but you will get a symbolic function rather than a number as output. There are a couple of things to look out for though.
As an example, suppose that you want to solve the equation y = 2x + 4 for x. The expected solution is x = (y-4)/2. Lets see how we can get MATLAB to do this. First let's look at how NOT to do it:
What has happened here? The MATLAB toolbox assumes that the 'y' you declared is 0 for the purposes of solving the equation! So it solved the equation 2x + 4 = 0 for x. In order to do what you intended to do you have to put your original equation, y = 2x + 4, into normal form, which is 2x + 4 - y = 0. Once this is done, you need to assign a 'dummy' variable like this:
This is, of course, the same thing as what we expected. You could also just pass the function into the 'solve' function like this, as done with functions of a single variable:
The first method is preferable, because once it is set up it is much more flexible to changes in the function. In the second method you would have to change every call to 'solve' if you changed the function at all, whereas in the first you only need to change the original definition of S.

### Solving Algebraic Systems

The 'solve' command also allows you to solve systems of algebraic equations, and will attempt to return all solutions to these systems. As a first example, let us consider the linear system
which has the solution (a,b) = (0,3). You can tell MATLAB to solve it as follows:
If only one output variable is specified but there are multiple equations, MATLAB will return the solutions in a struct array:
The good thing about this is that the fields have the same names as the original variables, whereas in the other form it is easy to get confused which variable is going into which spot in the array. In addition, the struct array is more convenient for large systems.
Now let us look at a slightly more complex example:
This has solutions (a,b) = (0,1) and (a,b) = (1,0). Now putting this into MATLAB gives:
Here both solutions are given, a(1) corresponds to b(1) and a(2) corresponds to b(2). To get one of the solutions into a single array together you can use normal array indexing, as in:

## Analytic Calculus

MATLAB's symbolic toolbox, in addition to its algebraic capabilities, can also perform many common calculus tasks, including analytical integration, differentiation, partial differentiation, integral transforms, and solving ordinary differential equations, provided the given tasks are mathematically possible.

### Differentiation and Integration with One Variable

Differentiation of functions with one or more variables is achieved using the 'diff' function. As usual, you can either define the function before the differentiation (recommended for M files) or you can manually write it in as an argument (recommended for command-line work). If there is only one symbolic variable in the expression, MATLAB assumes that is the variable you are differentiating with respect to. The syntax is simply:
Integration, similarly, is achieved using the 'int' function. Only specifying the function results in an indefinite integral, or the antiderivative of the function.
Note that if you only specify one output argument (or none at all), the 'int' function omits the integration constant. You just have to know it's there.
To do a definite integral on a one-variable function, simply specify the beginning and end points.

### Differentiation and Integration of Multivariable Functions

A convenient way of representing the derivatives of multivariate functions (partial derivatives) is with the Jacobian, which is performed by the 'jacobian' function. To use it, you should define an array of symbolic functions and then just pass it to the function (note the difference between the use of this function, which requires all equations to be in the same array, and the use of 'solve', which requires you to separately pass each equation):
Note that the first row is the gradient of f(1) and the second the gradient of f(2).
If you only want a specific partial derivative, not the entire Jacobian, you can call the 'diff' function with the function you want to differentiate and the variable you wish to differentiate with respect to. If none is specified, differentiation occurs with respect to the variable closest to 'x' in the alphabet.
It is worth noting that to complete an implicit differentiation, one can explicitly state the implicit assumption by multiplying the differentiated function by ${\frac {d}{dx_{i}}}$, where $x_{i}$ is the ith variable in a multivariable equation.
Indefinite integration of multivariate functions works the same as for single functions; pass the function and MATLAB will return the indefinite integral with respect to the variable closest to x:
This is the integral with respect to b. To avoid confusion, you can specify the variable of integration with a second argument, as with differentiation.
Definite integration (as far as I can tell) can only be done with respect to one variable at a time, and this is done by specifying the variable, then the bounds:

### Analytic Solutions to ODEs

MATLAB can solve some simple forms of ODEs. Unlike with the integration and algebraic solving techniques, the syntax for the differential equation solver required that you put the function in manually in a specific manner. The derivatives can be specified using the symbol 'DNV', where N is the order of the derivative and V is the variable that is changing. However, newer versions of MATLAB support entering differential equations in the form of symbolic equalities. For example, suppose you seek the solution to the equation ${\frac {d^{2}}{dt^{2}}}x=-x$, the solutions to which are of the form x(t) = A*cos(t) + B*sin(t). You would put this equation into the 'dsolve' function as follows:
Unlike the 'int' function, dsolve includes the integration constants. To specify initial conditions, just pass extra arguments to the 'dsolve' function as strings. If x'(0) = 2 and x(0) = 4 these are inserted as follows:
Note that the initial conditions can also be passed as strings.
MATLAB can also solve systems of differential equations. An acceptable syntax is to pass each equation as a separate string, and then pass each initial condition as a separate string:
The 'dsolve' function, like 'solve', thus returns the solution as a structure array, with field names the same as the variables you used. Also like 'solve', you can place the variables in separate arrays by specifying more than one output variable.

### Integral Transforms

MATLAB's symbolic math toolbox lets you find integral transforms (in particular, the Laplace, Fourier, and Z-transform) and their inverses when they exist. The syntax is similar to the other symbolic math functions: declare a function and pass it to the appropriate functions to obtain the transform (or inverse).
* ↑ http://en.wikipedia.org/wiki/MuPAD