| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Symbolic Math Toolbox |
| Contents | Index |
| Learn more about Symbolic Math Toolbox |
Symbolic Math Toolbox provides a set of simplification functions allowing you to manipulate an output of a symbolic expression. For example, the following polynomial of the golden ratio rho
rho = sym('(1 + sqrt(5))/2');
f = rho^2 - rho - 1returns
f = (5^(1/2)/2 + 1/2)^2 - 5^(1/2)/2 - 3/2
You can simplify this answer by entering
simplify(f)
and get a very short answer:
ans = 0
Symbolic simplification is not always so straightforward. There is no universal simplification function, because the meaning of a simplest representation of a symbolic expression cannot be defined clearly. Different problems require different forms of the same mathematical expression. Knowing what form is more effective for solving your particular problem, you can choose the appropriate simplification function.
For example, to show the order of a polynomial or symbolically differentiate or integrate a polynomial, use the standard polynomial form with all the parenthesis multiplied out and all the similar terms summed up. To rewrite a polynomial in the standard form, use the expand function:
syms x; f = (x ^2- 1)*(x^4 + x^3 + x^2 + x + 1)*(x^4 - x^3 + x^2 - x + 1); expand(f)
ans = x^10 - 1
The factor simplification function shows the polynomial roots. If a polynomial cannot be factored over the rational numbers, the output of the factor function is the standard polynomial form. For example, to factor the third-order polynomial, enter:
syms x; g = x^3 + 6*x^2 + 11*x + 6; factor(g)
ans = (x + 3)*(x + 2)*(x + 1)
The nested (Horner) representation of a polynomial is the most efficient for numerical evaluations:
syms x; h = x^5 + x^4 + x^3 + x^2 + x; horner(h)
ans = x*(x*(x*(x*(x + 1) + 1) + 1) + 1)
For a list of Symbolic Math Toolbox simplification functions, see Simplifications.
You can substitute a numeric value for a symbolic variable or replace one symbolic variable with another using the subs command. For example, to substitute the value x = 2 in the symbolic expression
syms x; f = 2*x^2 - 3*x + 1;
enter the command
subs(f, 2)
ans =
3
When your expression contains more than one variable, you can specify the variable for which you want to make the substitution. For example, to substitute the value x = 3 in the symbolic expression
syms x y; f = x^2*y + 5*x*sqrt(y);
enter the command
subs(f, x, 3)
ans = 9*y + 15*y^(1/2)
You also can substitute one symbolic variable for another symbolic variable. For example to replace the variable y with the variable x, enter
subs(f, y, x)
ans = x^3 + 5*x^(3/2)
You can also substitute a matrix into a symbolic polynomial with numeric coefficients. There are two ways to substitute a matrix into a polynomial: element by element and according to matrix multiplication rules.
Element-by-Element Substitution. To substitute a matrix at each element, use the subs command:
A = [1 2 3;4 5 6]; syms x; f = x^3 - 15*x^2 - 24*x + 350; subs(f,A)
ans =
312 250 170
78 -20 -118You can do element-by-element substitution for rectangular or square matrices.
Substitution in a Matrix Sense. If you want to substitute a matrix into a polynomial using standard matrix multiplication rules, a matrix must be square. For example, you can substitute the magic square A into a polynomial f:
Create the polynomial:
syms x; f = x^3 - 15*x^2 - 24*x + 350;
Create the magic square matrix:
A = magic(3)
A =
8 1 6
3 5 7
4 9 2Get a row vector containing the numeric coefficients of the polynomial f:
b = sym2poly(f)
b =
1 -15 -24 350Substitute the magic square matrix A into the polynomial f. Matrix A replaces all occurrences of x in the polynomial. The constant times the identity matrix eye(3) replaces the constant term of f:
A^3 - 15*A^2 - 24*A + 350*eye(3)
ans =
-10 0 0
0 -10 0
0 0 -10The polyvalm command provides an easy way to obtain the same result:
polyvalm(sym2poly(f),A)
ans =
-10 0 0
0 -10 0
0 0 -10To substitute a set of elements in a symbolic matrix, also use the subs command. Suppose you want to replace some of the elements of a symbolic circulant matrix A
syms a b c; A = [a b c; c a b; b c a]
A = [ a, b, c] [ c, a, b] [ b, c, a]
To replace the (2, 1) element of A with beta and the variable b throughout the matrix with variable alpha, enter
alpha = sym('alpha');
beta = sym('beta');
A(2,1) = beta;
A = subs(A,b,alpha)The result is the matrix:
A = [ a, alpha, c] [ beta, a, alpha] [ alpha, c, a]
For more information on the subs command see Substitutions.
The sym command converts a numeric scalar or matrix to symbolic form. By default, the sym command returns a rational approximation of a numeric expression. For example, you can convert the standard double-precision variable into a symbolic object:
t = 0.1; sym(t)
ans = 1/10
The technique for converting floating-point numbers is specified by the optional second argument, which can be 'f', 'r', 'e' or 'd'. The default option is 'r' that stands for rational approximationConverting to Rational Symbolic Form.
The 'f' option to sym converts a double-precision floating-point number to a sum of two binary numbers. All values are represented as rational numbers N*2^e, where e and N are integers, and N is nonnegative. For example,
sym(t, 'f')
returns the symbolic floating-point representation:
ans = 3602879701896397/36028797018963968
If you call sym command with the 'r' option
sym(t, 'r')
you get the results in the rational form:
ans = 1/10
This is the default setting for the sym command. If you call this command without any option, you get the result in the same rational form:
sym(t)
ans = 1/10
If you call the sym command with the option 'e', it returns the rational form of t plus the difference between the theoretical rational expression for t and its actual (machine) floating-point value in terms of eps (the floating-point relative accuracy):
sym(t, 'e')
ans = eps/40 + 1/10
If you call the sym command with the option 'd', it returns the decimal expansion of t up to the number of significant digits:
sym(t, 'd')
ans = 0.10000000000000000555111512312578
By default, the sym(t,'d') command returns a number with 32 significant digits. To change the number of significant digits, use the digits command:
digits(7); sym(t, 'd')
ans = 0.1
With the Symbolic Math Toolbox software, you can find
Derivatives of single-variable expressions
Partial derivatives
Second and higher order derivatives
Mixed derivatives
For in-depth information on taking symbolic derivatives see Differentiation.
To differentiate a symbolic expression, use the diff command. The following example illustrates how to take a first derivative of a symbolic expression:
syms x; f = sin(x)^2; diff(f)
ans = 2*cos(x)*sin(x)
For multivariable expressions, you can specify the differentiation variable. If you do not specify any variable, MATLAB chooses a default variable by the proximity to the letter x:
syms x y; f = sin(x)^2 + cos(y)^2; diff(f)
ans = 2*cos(x)*sin(x)
For the complete set of rules MATLAB applies for choosing a default variable, see Finding a Default Symbolic Variable.
To differentiate the symbolic expression f with respect to a variable y, enter:
syms x y; f = sin(x)^2 + cos(y)^2; diff(f, y)
ans = (-2)*cos(y)*sin(y)
To take a second derivative of the symbolic expression f with respect to a variable y, enter:
syms x y; f = sin(x)^2 + cos(y)^2; diff(f, y, 2)
ans = 2*sin(y)^2 - 2*cos(y)^2
You get the same result by taking derivative twice: diff(diff(f, y)). To take mixed derivatives, use two differentiation commands. For example:
syms x y; f = sin(x)^2 + cos(y)^2; diff(diff(f, y), x)
ans = 0
You can perform symbolic integration including:
Indefinite and definite integration
Integration of multivariable expressions
For in-depth information on the int command including integration with real and complex parameters, see Integration.
Suppose you want to integrate a symbolic expression. The first step is to create the symbolic expression:
syms x; f = sin(x)^2;
To find the indefinite integral, enter
int(f)
ans = x/2 - sin(2*x)/4
If the expression depends on multiple symbolic variables, you can designate a variable of integration. If you do not specify any variable, MATLAB chooses a default variable by the proximity to the letter x:
syms x y n; f = x^n + y^n; int(f)
ans = x*y^n + (x*x^n)/(n + 1)
For the complete set of rules MATLAB applies for choosing a default variable, see Finding a Default Symbolic Variable.
You also can integrate the expression f = x^n + y^n with respect to y
syms x y n; f = x^n + y^n; int(f, y)
ans = x^n*y + (y*y^n)/(n + 1)
If the integration variable is n, enter
syms x y n; f = x^n + y^n; int(f, n)
ans = x^n/log(x) + y^n/log(y)
To find a definite integral, pass the limits of integration as the final two arguments of the int function:
syms x y n; f = x^n + y^n; int(f, 1, 10)
ans = piecewise([n = -1, log(10) + 9/y],... [n <> -1, (10^(n + 1) - 1)/(n + 1) + 9*y^n])
If the int function cannot compute an integral, MATLAB issues a warning and returns the int function as an answer:
syms x y n; f = exp(x)^n + exp(y)^n; int(f, n, 1, 10)
Warning: Explicit integral could not be found. ans = int(exp(x)^n + exp(y)^n, n = 1..10)
You can solve different types of symbolic equations including:
Algebraic equations with one symbolic variable
Algebraic equations with several symbolic variables
Systems of algebraic equations
For in-depth information on solving symbolic equations including differential equations, see Solving Equations.
You can find the values of variable x for which the following expression is equal to zero:
syms x; solve(x^3 - 6*x^2 + 11*x - 6)
ans = 1 2 3
By default, the solve command assumes that the right-side of the equation is equal to zero. If you want to solve an equation with a nonzero right part, use quotation marks around the equation:
syms x;
solve('x^3 - 6*x^2 + 11*x - 5 = 1')ans = 1 2 3
If an equation contains several symbolic variables, you can designate a variable for which this equation should be solved. For example, you can solve the multivariable equation:
syms x y; f = 6*x^2 - 6*x^2*y + x*y^2 - x*y + y^3 - y^2;
with respect to a symbolic variable y:
solve(f, y)
ans =
1
2*x
(-3)*xIf you do not specify any variable, you get the solution of an equation for the alphabetically closest to x variable. For the complete set of rules MATLAB applies for choosing a default variable see Finding a Default Symbolic Variable.
You also can solve systems of equations. For example:
syms x y z;
[x, y, z] = solve('z = 4*x', 'x = y', 'z = x^2 + y^2')x = 0 2 y = 0 2 z = 0 8
When performing substitution, differentiation, or integration, if you do not specify a variable to use, MATLAB uses a default variable. The default variable is basically the one closest alphabetically to x. To find which variable is chosen as a default variable, use the symvar(expression, 1) command. For example:
syms s t; g = s + t; symvar(g, 1)
ans = t
syms sx tx; g = sx + tx; symvar(g, 1)
ans = tx
For more information on choosing the default symbolic variable, see the symvar command.
You can create different types of graphs including:
Plots of explicit functions
Plots of implicit functions
3-D parametric plots
Surface plots
See Pedagogical and Graphical Applications for in-depth coverage of Symbolic Math Toolbox graphics and visualization tools.
The simplest way to create a plot is to use the ezplot command:
syms x; ezplot(x^3 - 6*x^2 + 11*x - 6); hold on;
The hold on command retains the existing plot allowing you to add new elements and change the appearance of the plot. For example, now you can change the names of the axes and add a new title and grid lines. When you finish working with the current plot, enter the hold off command:
xlabel('x axis');
ylabel('no name axis');
title('Explicit function: x^3 - 6*x^2 + 11*x - 6');
grid on;
hold off

You can plot implicitly defined functions. For example, create a plot for the following implicit function over the domain –1 < x < 1:
syms x y;
f = (x^2 + y^2)^4 - (x^2 - y^2)^2;
ezplot(f, [-1 1]);
hold on;
xlabel('x axis');
ylabel('y axis');
title('Implicit function: f = (x^2 + y^2)^4 - (x^2 - y^2)^2');
grid on;
hold off

3-D graphics is also available in Symbolic Math Toolbox . To create a 3-D plot, use the ezplot3 command. For example:
syms t; ezplot3(t^2*sin(10*t), t^2*cos(10*t), t);

If you want to create a surface plot, use the ezsurf command. For example, to plot a paraboloid z = x2 + y2, enter:
syms x y;
ezsurf(x^2 + y^2);
hold on;
zlabel('z');
title('z = x^2 + y^2');
hold off

![]() | Creating Symbolic Variables and Expressions | Assumptions for Symbolic Objects | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |