Lesson 4.2

Symbolic Variables and Solving for Roots


  • Learn symbolic variables and MATLAB toolboxes.
  • Use solve() to find the roots of a linear equation.
  • Output and convert data type with syms variables.
  • Use subs() to substitute a number or character into an equation.
  • Plot syms variables.

Intro to Symbolic (syms) Toolbox

The symbolic toolbox introduces syms: a new data type. syms allows you to create variables without assigning them a value (number, string, etc.). This is very useful when doing anything beyond basic “2+2” math. Solving equations, calculus, and differential equations are a few examples of how syms are used to solve mathematical problems, and they demonstrate the functionality of the symbolic toolbox.

Example 1: Simple Demonstration of the Symbolic Toolbox
syms x %Defining ‘x’ as symbolic variable x %Outputting ‘x’ to the Command Window Command Window Output
x [Try this code yourself with Octave Online! Click Here]

In Example 1, we see that the output of a symbolic variable is not numeric but simply the symbolic representation. Subsequently, you would be able to insert any numeric value for this symbolic variable.

What Is a Toolbox?

A toolbox, more traditionally referred to as a package or library, is just a set of code (which you do not need to see) that gives you more predefined functions. We have been using functions all along, but have not talked explicitly about a “toolbox” because we have used pre-installed, default functions. One example is the plot() function. MATLAB knows how to plot the data you input to the function because there is an algorithm doing the work for you behind the scenes. We will learn how to write our own functions and reference them in a later section (LINK TO User Defined Functions).

How to Solve for Roots of an Equation

In this section, we will go over using MATLAB to solve for the roots of an equation using solve(). We will only show an example with a linear equation in this lesson as we cover finding the roots of nonlinear equations in the next lesson [LINK TO LESSON]. The roots of an equation are where it is equal to zero (e.g., x^2+9=0). For example, x-2=0 (y=0) has a root at x=2. Note that the number of roots is always equal to the degree (or order) of the polynomial.

Example 2: Solving for Roots Using solve()
syms x %Defining 'x' as symbolic independent variable y = 9*x - 10; %Defining equation/function with symbolic variable 'x' solution = solve(y, x) %Solving for roots of the equation Command Window Output
solution = 10/9 [Try this code yourself with Octave Online! Click Here]

Important Note: The equation given to solve() must be set equal to zero. For example, if we were asked to find the roots of 4*x^3 - 20*x^2 = -9*x in Example 1, the first step would be to set it equal to zero; i.e., 4*x^3 - 20*x^2 + 9*x = 0.

How to Convert From syms Data Type to Other Data Types

Since syms is a data type in MATLAB, it often needs to be converted to a different data type, such as double or char, to display it. See Example 3 for some specific use cases. Using the wrong conversion could result in an error. Some commonly used functions to convert the syms data type to other data types/formats are:

  • double() - Convert symbolic matrix to MATLAB numeric form.
  • char() - Convert symbolic objects to strings.
  • vpa() - Manually adjust precision of an expression.

Full list of symbolic data type conversion functions: Conversion Between Symbolic and Numeric.

Example 3: Converting 'sym' Data Type (continued from Example 1)
solution = double(solution); %Converting syms variable “solution” to double data type str = char(y); %Converting the equation to a string data type fprintf('The solutions to %s are %g, %g, %g\n\n', str, solution(1), solution(2), solution(3)) %Note how the roots are referenced. Roots are stored as a vector if there is a polynomial of %order 2 or greater. Command Window Output
The solutions to 9*x - 20*x^2 + 4*x^3 are 0, 0.5, 4.5 [Try this code yourself with Octave Online! Click Here]

Replacing Symbolic Variables

MATLAB makes changing/substituting the value or the name of a symbolic variable simple. Using the subs() function, you can assign a value for the symbolic variable (e.g., x = 1) or rename it (e.g., x = a). This function will come in handy when we plot equations later in this lesson and in future lessons.

Example 4: Using subs() to Find Value of a Function
syms s a %Defining 's' as symbolic variable f = s^2 + 4*s + 56 %Defining equation with symbolic variable 's' f1 = subs(f,s,1) %Finding f(1) using subs() to substitute a number Command Window Output
f = s^2 + 4*s + 56 f1 = 61 [Try this code yourself with Octave Online! Click Here]
Example 5: Using subs() to Change the Name of a Symbolic Variable
syms s a %Defining 's' and 'a' as symbolic variables f = s^2 + 4*s + 56 %Defining equation with symbolic variable 's' fa = subs(f,s,a) %Replacing 's' with 'a'; i.e., f(s) to f(a) using subs() Command Window Output
f = s^2 + 4*s + 56 fa = a^2 + 4*a + 56 [Try this code yourself with Octave Online! Click Here]

How to Change Output Format of syms

You can change the output type (scientific, floating point, etc.) directly within an fprintf() statement if your number is in decimal form (a numeric . However, MATLAB returns exact solutions by default; that is, it will return “1/3” instead of “0.33333...”. The precision of an answer can be adjusted using the function vpa().

This is sufficient for most purely numerical answers. If you have an answer that contains symbolic variables like x, t, etc., you may need to use the functions pretty() and/or simplify() to help clean up and simplify messy solutions.

Plotting with syms

Plotting with syms has all the same considerations as any other function you wish to plot with the additional step of converting to a data type that is accepted by the function you are using to plot (e.g., plot(), bar(), etc.). That is, you will likely need to convert from syms to a numeric data type in order to plot the data.

In order to get a vector of data points to plot, we can use the subs() function. Although it was used in the previous examples to replace a variable with just a single number or character, it also accepts vector inputs as the replacement value(s).

Example 6: Plotting with 'syms'
%Next 3 lines are from Example 2 in this lesson syms x %Defining 'x' as symbolic independent variable y = 4*x^3 - 20*x^2 + 9*x; %Defining equation/function with symbolic variable 'x' solution = solve(y, x) %Solving for roots of the equation xPoints = [min(solution):0.1:max(solution)]; %Define the domain/range to plot the equation yPoints = subs(y,x,xPoints); %Find the value of the equation at each 'x' point figure %Creating a blank figure plot(soln,zeros(length(solution),1),'*') %Plot the roots. note the y coordinates will be zero. hold on plot(xPoints, yPoints,'r') %Plotting function y(x) %Figure formatting (see lesson m-file for full code) title('Equation and its Roots') xlabel('Independent Variable, x') ylabel('Dependent Variable, y') legend('Function Roots','Function, y(x)') [Try this code yourself with Octave Online! Click Here]

Figure 1: The MATLAB figure output from Example 6.

Next Lesson

In the next lesson, we will cover how to create a numerical model from raw data points using MATLAB.