MATH/CMSC 206 - Introduction to Matlab

Announcements Syllabus Tutorial Projects Submitting

Symbolic Variables

Contents

Declaring Variables as Symbolic

In the previous section we learned how to create and use Matlab variables for storing numerical values during a Matlab session. Now we'd like to introduce a different kind of variable called a symbolic variable. These are necessary when the objects we are manipulating are not just numbers but rather expressions involving mathematical variables. For example, we would like to be able to use Matlab to manipulate a polynomial like 6*x^2+5*x-4. The variable x that we see in this polynomial is not really a Matlab variable, it is a symbolic variable that is part of the polynomial we are going to be working with. In order for Matlab to manipulate mathematical expressions containing variables, we have to begin by creating these symbolic variables. For example, you could create the symbolic variable x like this:

syms x

You can create more than one symbolic variable at a time, like this:

syms x y z

Note that it is perfectly okay to declare a variable as symbolic more than once.

Now that we've created the symbolic variable x, Matlab will be able to manipulate mathematical expressions that contain x. Below we'll practice using symbolic variables in several useful ways.

Note: In all of the examples that follow we are going to be using the symbolic variable x. To get the examples to work, you must first create the symbolic variable as shown above.

Factoring Polynomials

Suppose you are trying to factor the polynomial 6*x^2+5*x-4 using Matlab. No problem -- just use the factor function like this:

factor(6 * x ^ 2 + 5 * x - 4)
 
ans =
 
(3*x + 4)*(2*x - 1)
 

Recall that Matlab uses * for multiplication and ^ for exponentiation.

Using pretty

If you don't like seeing the * and the ^ in the output, you can pass the result of a command like factor into another command called pretty. Here's how that might look:

pretty(factor(6 * x ^ 2 + 5 * x - 4))
  (3 x + 4) (2 x - 1)

Expanding

The opposite of factoring is expanding. Suppose you have an expression with parentheses like (7*k+2)*(5*k-3)*(k+7) and you want to multiply it out. You can use the expand function as illustrated below. Note that we must first make sure k defined symbolically:

syms k
expand((7 * k + 2) * (5 * k - 3) * (k + 7))
 
ans =
 
35*k^3 + 234*k^2 - 83*k - 42
 

Simplifying

You can ask Matlab to try to simplify any mathematical expression. It doesn't always work! But many times you'll be impressed by how good Matlab is at this. Below are several examples illustrating the simplify function:

Try inventing other examples of your own!

simplify(3 * x + (x + 4)*(x - 7) + 15)
 
ans =
 
x^2 - 13
 
simplify((3 * x + 4) / (6 * x ^ 2 + 5 * x - 4))
 
ans =
 
1/(2*x - 1)
 
simplify(cos(x * 3)^2 + sin(x * 3)^2)
 
ans =
 
1
 
simplify(log(x) + log(2 * x))
 
ans =
 
log(2) + 2*log(x)
 

Self-Test

  1. We are about to manipulate some formulas involving variables a, b, and c. Write the Matlab command that declares these variables as symbolic.
  2. Write the Matlab command that will factor the polynomial a^2-b^2
  3. Write the Matlab command that will multiply a+b+c by a^2-b^2, leaving no parentheses in it's answer.
  4. Write the Matlab command that will simplify the expression: log(6a)-log(3a).

Answers to Self-Test

Next Topic: Solving Equations