sym
Creates symbolic variables, numbers and arrays.
Category: Symbolic Computations in MATLAB > Symbolic Variables, Expressions, Functions, and Preferences
Syntax
x = sym('x')
Creates the symbolic variable x.
n = sym(num)
Converts a number to an exact symbolic value; sym(1/3) gives 1/3.
n = sym(num, flag)
Chooses how a floating-point value is converted: 'r' rational (default), 'd' decimal, 'e' estimate with error, 'f' exact binary fraction.
A = sym('A', [m n])
Creates an m-by-n matrix of variables A1_1, A1_2, ...
Description
sym is the entry point to the Symbolic Math Toolbox. A string argument creates a variable with that name; a numeric argument is turned into an exact value that arithmetic will keep exact. Floating-point conversion uses the nearest simple rational by default, so sym(0.1) is 1/10 and sym(pi) stays the symbol pi.
To create several variables at once, syms x y z is shorter. Mixing a sym with a double in an expression converts the double with sym first, so sym(1)/3 is the exact 1/3 while 1/3 alone is a rounded double.
Input arguments
x- Name of the variable, a valid identifier.
num- Numeric array to convert exactly.
[m n]- Size of the symbolic matrix to generate.
Output arguments
S- Symbolic variable, number or array.
Example
Variables, exact numbers and matrices
x = sym('x');
x^2 + 1
sym(1)/3
sym(0.1)
A = sym('A', [2 2])
ans = x^2 + 1
ans = 1/3
ans = 1/10
A =
[A1_1, A1_2]
[A2_1, A2_2]
Notes
In MatLivre the flags 'f' and 'e' give the same rational result as 'r', so sym(0.1, 'f') is 1/10 and not the exact binary fraction. The pattern form sym('a%d', [1 3]) is not expanded. Assumptions given as sym('y', 'positive') are not tracked, and assumptions returns nothing for them; use syms y real for the real assumption.