## Documentation Center |

On this page… |
---|

Substitutions in Symbolic Expressions Estimate Precision of Numeric to Symbolic Conversions Differentiate Symbolic Expressions |

Symbolic Math Toolbox™ provides a set of simplification functions
allowing you to manipulate the output of a symbolic expression. For
example, the following polynomial of the golden ratio `phi`

phi = sym('(1 + sqrt(5))/2'); f = phi^2 - phi - 1

returns

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 parentheses 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 symbolic variable with a numeric value
by using the `subs` function. For example, evaluate
the symbolic expression `f` at the point `x` = 1/3:

syms x f = 2*x^2 - 3*x + 1; subs(f, 1/3)

ans = 2/9

The `subs` function does not change the original
expression `f`:

f

f = 2*x^2 - 3*x + 1

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:

syms x f = x^3 - 15*x^2 - 24*x + 350; A = [1 2 3; 4 5 6]; subs(f,A)

ans = [ 312, 250, 170] [ 78, -20, -118]

You 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 2

Get a row vector containing the numeric coefficients of the polynomial

`f`:b = sym2poly(f)

b = 1 -15 -24 350

Substitute 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 -10

The

`polyvalm`command provides an easy way to obtain the same result:polyvalm(b,A)

ans = -10 0 0 0 -10 0 0 0 -10

To 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, see Substitution.

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'`, which stands for rational
approximation Conversion to Rational Symbolic Form.

The `'f'` option to `sym` converts
double-precision floating-point numbers to exact numeric values `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 precision):

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 its 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 Find 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 Find 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*10^n - 1)/(n + 1) + 9*y^n])

If the `int` function cannot compute an integral,
it returns an unresolved integral:

syms x y n f = sin(x)^(1/sqrt(n)); int(f, n, 1, 10)

ans = int(sin(x)^(1/n^(1/2)), 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 Equation Solving.

Use the double equal sign (==) to define an equation. Then you
can `solve` the equation by
calling the solve function. For example, solve this equation:

syms x solve(x^3 - 6*x^2 == 6 - 11*x)

ans = 1 2 3

If you do not specify the right side of the equation, `solve` assumes
that it is zero:

syms x solve(x^3 - 6*x^2 + 11*x - 6)

ans = 1 2 3

If an equation contains several symbolic variables, you can
specify a variable for which this equation should be solved. For example,
solve this multivariable equation with respect to `y`:

syms x y solve(6*x^2 - 6*x^2*y + x*y^2 - x*y + y^3 - y^2 == 0, y)

ans = 1 2*x -3*x

If 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 Find 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

You can create different types of graphs including:

Plots of explicit functions

Plots of implicit functions

3-D parametric plots

Surface plots

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

Using `ezplot`, you can also plot equations.
For example, plot the following equation over *–1 < x <
1*:

syms x y ezplot((x^2 + y^2)^4 == (x^2 - y^2)^2, [-1 1]) hold on xlabel('x axis') ylabel('y axis') 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 = x*^{2} + *y*^{2},
enter:

syms x y ezsurf(x^2 + y^2) hold on zlabel('z') title('z = x^2 + y^2') hold off

Was this topic helpful?