Main Content

Symbolic Objects to Represent Mathematical Objects

To solve mathematical problems with Symbolic Math Toolbox™, symbolic objects are defined to represent various mathematical objects. This example discusses the usage of symbolic numbers, variables, functions, expressions, vectors, and symbolic matrices to perform symbolic computations that solve mathematical problems.

Symbolic Number

Use a symbolic number to represent the argument of an inverse trigonometric function θ=sin1(1/2). Defining 1/2 as a symbolic number instructs MATLAB® to treat the number as an exact form instead of a numeric approximation.

A picture showing symbolic number that represents the argument of an inverse trigonometric function.

Create the symbolic number 1/2 using sym, and assign it to a.

a = 1/sqrt(sym(2))
a =

Find the inverse sine of a. The result is the symbolic number pi/4.

thetaSym = asin(a)
thetaSym =

You can convert a symbolic number to variable-precision arithmetic by using vpa. The result is a decimal number with 32 significant digits.

thetaVpa = vpa(thetaSym)
thetaVpa =

To convert the symbolic number to numeric double data type, use double. For more information about whether to use numeric or symbolic arithmetic, see Choose Numeric or Symbolic Arithmetic.

thetaDouble = double(thetaSym)
thetaDouble =

Symbolic Variable, Function, and Expression

Use a symbolic variable, function, and expression to represent the quadratic function f(x)=x2+x2. Defining variables, functions, and expressions as symbolic objects enables you to perform algebraic operations with those symbolic objects, including simplifying formulas and solving equations.

A picture showing symbolic variable, function, and expression that represent the quadratic function.

Create a symbolic variable x using syms. For more information about whether to use syms or sym, see Choose syms or sym Function. Define a symbolic expression x^2 + x - 2 to represent the right side of the quadratic equation, and assign it to f(x). The identifier f(x) is a symbolic function that represents the quadratic function.

syms x
f(x) = x^2 + x - 2
f(x) =
x^2 + x -2

You can then evaluate the quadratic function by providing its input argument inside the parenthesis. For example, evaluate f(2).

fVal = f(2)
fVal =

Next, solve the quadratic equation f(x)=0. Use solve to find the roots of the quadratic equation. solve returns the two solutions as a vector of two symbolic numbers.

sols = solve(f)
sols =

Symbolic Equation

Use a symbolic equation to solve the trigonometric problem 2sin(t)cos(t)=1.

A picture showing symbolic equation that represents the trigonometric problem.

Create a symbolic function g(t) using syms. Assign the symbolic expression 2*sin(t)*cos(t) to g(t).

syms g(t)
g(t) = 2*sin(t)*cos(t)
g(t) = 
To define the equation, use the == operator and assign the expression g(t) == 1 to eqn. The identifier eqn is a symbolic equation that represents the trigonometric problem.
eqn = g(t) == 1
eqn = 
2*cos(t)*sin(t) == 1

Use solve to find the solution of the trigonometric problem.

sol = solve(eqn)
sol = 

Symbolic Vector and Matrix

Use a symbolic vector and matrix to represent and solve a system of linear equations.


You can cast the system of equations as a vector of two symbolic equations. You can also cast the system of equations as a matrix problem involving a symbolic matrix and a vector.

A picture of symbolic vectors and matrix that represent a system of linear equations and a matrix problem.

Create two symbolic equations eq1 and eq2. Combine the two equations into a vector of symbolic equations. For brevity, any vector of symbolic objects is called a symbolic vector and any matrix of symbolic objects is called a symbolic matrix.

syms u v x y
eq1 = x + 2*y == u;
eq2 = 4*x + 5*y == v;
eqns = [eq1, eq2]
eqns =
[x + 2*y == u, 4*x + 5*y == v]

Use solve to find the solutions of the system of equations eqns. solve returns a structure S with fields named after each of the variables in the equations. You can access the solutions using the dot notation S.x and S.y.

S = solve(eqns);
ans =
(2*v)/3 - (5*u)/3
ans =
(4*u)/3 - v/3

Another alternative to solve the system of linear equations is to convert it to a matrix form. Use equationsToMatrix to convert the system of equations to a matrix form and assign the output to A and b. A is a symbolic matrix and b is a symbolic vector. Solve the matrix problem by using the matrix division \ operator.

[A,b] = equationsToMatrix(eqns,x,y)
A =
[1, 2]
[4, 5]
b =
sols = A\b
sols =
(2*v)/3 - (5*u)/3
    (4*u)/3 - v/3

Comparisons of Symbolic Objects

This table compares various symbolic objects that are available in Symbolic Math Toolbox.

Symbolic ObjectsExamples of MATLAB CommandsSize of Symbolic ObjectsData Type
symbolic number
a = 1/sqrt(sym(2))
theta = asin(a)
a =
theta =
symbolic variable
syms x y u v
symbolic function
syms x
f(x) = x^2 + x - 2
syms g(t)
g(t) = 2*sin(t)*cos(t)
f(x) =
x^2 + x - 2
g(t) =
symbolic equation
syms u v x y
eq1 = x + 2*y == u
eq2 = 4*x + 5*y == v
eq1 = 
x + 2*y == u
eq2 =
4*x + 5*y == v
symbolic expression
syms x
expr = x^2 + x - 2
expr2 = 2*sin(x)*cos(x)
expr = 
x^2 + x - 2
expr2 =
symbolic vector
syms u v
b = [u v]
b = 
[u, v]
1-by-n or m-by-1sym
symbolic matrix
syms A x y
A = [x y; x*y y^2]
A =
[  x,   y]
[x*y, y^2]
symbolic multidimensional array
syms A [2 1 2]
A(:,:,1) =
A(:,:,2) =

See Also

| |

Related Topics