Transcription
Up to now, we've been doing numeric calculations which are required in many real-world applications.
Take the simple example of finding the solutions to a quadratic equation. We can quickly find
the solutions numerically when we have the coefficients. However, sometimes we get imaginary
roots. How can we determine when the solutions are real?
If we look at the symbolic solution, we see that the expression inside the square root
must be positive to have real roots. Let's see how we get to this solution using
MATLAB and the Symbolic Math Toolbox. The first step is to define symbolic variables
using the syms command. Symbolic variables differ from the numeric
variables we've used so far because we don't have to assign value to them. We simply declare
their existence and we can use them in calculations. Like here, we enter the generic quadratic
equation, and assign the expression to the variable 'y'. Notice that 'y' automatically
becomes a symbolic variable even though we didn't declare it as one.
Now, to find the symbolic solution, we use the SOLVE function while setting 'y' equal
to 0, and specifying 'x' as the variable to solve for. And bingo! The SOLVE function returns
a variable with the two solutions that match the quadratic formula.
But wait a second. There's something different about this last command. Because a single
equal sign is the assignment operator, we used two equal signs to create a symbolic
equation. Now what if we need to apply our solution
to a specific example? In other words, we have to replace the symbolic variables 'a',
'b', and 'c' with fixed values. No problem. The SUBS function replaces symbolic variables
with numbers or other symbolic expressions. Here's how it works. We need three inputs:
the expression we wish to evaluate, the symbolic variables to replace, and the values to substitute
in for the symbolic variables. The result is kept as an exact symbolic solution, rather
than a numeric approximation. Perhaps you've noticed, our solution is not
fully simplified. Let's fix that using the SIMPLIFY function. There we go, an exact solution
to our quadratic equation. In many cases, we still need a numeric representation
of our symbolic results. Again, no problem. We use the variable precision arithmetic,
or VPA, function to view the exact solution as a number. The 2nd input to the VPA function
specifies the number of digits to include in the output, and the output is still a symbolic
variable. We have now worked through the essentials
of solving an equation symbolically, and you can use these techniques to solve a wide variety
of problems. Before we close, here are a few examples from
other applications that often arise, like finding integrals, doing series expansions,
and plotting equations of one or more variables. Now put away the pen and paper, and try solving
a few problems in MATLAB yourself.