Documentation |
Create symbolic objects
var = sym('var')
var = sym('var',set)
sym('var','clear')
Num = sym(Num)
Num = sym(Num,flag)
A = sym('A',dim)
A = sym(A,set)
sym(A,'clear')
f(arg1,...,argN)
= sym('f(arg1,...,argN)')
fsym = sym(fh)
var = sym('var') creates the symbolic variable var.
var = sym('var',set) creates the symbolic variable var and states that var belongs to set.
sym('var','clear') clears assumptions previously set on the symbolic variable var.
Num = sym(Num) converts a number or a numeric matrix Num to symbolic form.
Num = sym(Num,flag) converts a number or a numeric matrix Num to symbolic form. The second argument specifies the technique for converting floating-point numbers.
A = sym('A',dim) creates a vector or a matrix of symbolic variables.
A = sym(A,set), where A is an existing symbolic vector or matrix, sets an assumption that each element of A belongs to set. To check assumptions set on A, use the assumptions function. This syntax does not create A. To create a symbolic vector or a symbolic matrix A, use A = sym('A',[m n]) or A = sym('A',n).
sym(A,'clear'), where A is an existing symbolic vector or matrix, clears assumptions previously set on elements of A. This syntax does not create A. To create a symbolic vector or a symbolic matrix A, use A = sym('A',[m n]) or A = sym('A',n).
f(arg1,...,argN) = sym('f(arg1,...,argN)') creates the symbolic function f and specifies that arg1,...,argN are the input arguments of f. This syntax does not create symbolic variables arg1,...,argN. The arguments arg1,...,argN must be existing symbolic variables.
fsym = sym(fh) uses the symbolic form of the body of the function handle fh to create the symbolic expression, vector, or matrix fsym.
var |
String that represents the variable name. It must begin with a letter and can contain only alphanumeric characters. |
set |
Either real or positive. |
Num |
Number, vector, or matrix of numbers. |
flag |
One of these strings: r, d, e, or f.
Default: r |
A |
String that represents the base for generated names of vector or matrix elements. It must be a valid variable name. (To verify if the name is a valid variable name, use isvarname.) Default: The generated names of elements of a vector use the form Ak, and the generated names of elements of a matrix use the form Ai_j. The values of k, i, and j range from 1 to m or 1 to n. To specify another form for generated names of matrix elements, use '%d' in the first input. For example, A = sym('A%d%d', [3 3]) generates the 3-by-3 symbolic matrix A with the elements A11, A12, ..., A33. |
dim |
Integer or vector of two integers specifying dimensions of A. For example, if dim is a vector [m n], then the syntax A = sym('A',[m n]) creates an m-by-n matrix of symbolic variables. If dim is an integer n, then the syntax A = sym('A',n) creates a square n-by-n matrix of symbolic variables. |
f |
Name of a symbolic function. It must begin with a letter and contain only alphanumeric characters. |
arg1,...,argN |
Arguments of a symbolic function. Each argument must be an existing symbolic variable. |
fh |
Function handle. |
Create the symbolic variables x and y:
x = sym('x'); y = sym('y');
Create the symbolic variables x and y assuming that x is real and y is positive:
x = sym('x','real'); y = sym('y','positive');
Check the assumptions on x and y using assumptions:
assumptions
ans = [ in(x, 'real'), 0 < y]
For further computations, clear the assumptions:
sym('x','clear'); sym('y','clear'); assumptions
ans = Empty sym: 1-by-0
The sym function lets you choose the conversion technique by specifying the optional second argument, which can be 'r', 'f', 'd', or 'e'. The default is 'r'. For example, convert the number 1/3 to a symbolic object:
r = sym(1/3) f = sym(1/3, 'f') d = sym(1/3, 'd') e = sym(1/3, 'e')
r = 1/3 f = 6004799503160661/18014398509481984 d = 0.3333333333333333148296162562473909929395 e = 1/3 - eps/12
Create the 3-by-4 symbolic matrix A with the auto-generated elements A1_1, ..., A3_4 :
A = sym('A', [3 4])
A = [ A1_1, A1_2, A1_3, A1_4] [ A2_1, A2_2, A2_3, A2_4] [ A3_1, A3_2, A3_3, A3_4]
Now create the 4-by-4 matrix B with the elements x_1_1, ..., x_4_4:
B = sym('x_%d_%d', [4 4])
B = [ x_1_1, x_1_2, x_1_3, x_1_4] [ x_2_1, x_2_2, x_2_3, x_2_4] [ x_3_1, x_3_2, x_3_3, x_3_4] [ x_4_1, x_4_2, x_4_3, x_4_4]
This syntax does not define elements of a symbolic matrix as separate symbolic objects. To access an element of a matrix, use parentheses:
A(2, 3) B (4, 2)
ans = A2_3 ans = x_4_2
You can use symbolic matrices and vectors generated by the sym function to define other matrices:
A = diag(sym('A',[1 4]))
A = [ A1, 0, 0, 0] [ 0, A2, 0, 0] [ 0, 0, A3, 0] [ 0, 0, 0, A4]
Perform operations on symbolic matrices by using the operators that you use for numeric matrices. For example, find the determinant and the trace of the matrix A:
det(A)
ans = A1*A2*A3*A4
trace(A)
ans = A1 + A2 + A3 + A4
Use the sym function to set assumptions on each element of a symbolic matrix. You cannot create a symbolic matrix and set an assumption on all its elements in one sym function call. Use two separate sym function calls. The first call creates a matrix, and the second call specifies an assumption:
A = sym('A%d%d', [2 2]); A = sym(A, 'positive')
A = [ A11, A12] [ A21, A22]
Now, MATLAB^{®} assumes that all elements of A are positive:
solve(A(1, 1)^2 - 1, A(1, 1))
ans = 1
To check the assumptions set on the elements of A, use assumptions:
assumptions(A)
ans = [ 0 < A21, 0 < A11, 0 < A22, 0 < A12]
To clear all previously set assumptions on elements of a symbolic matrix, also use the sym function:
A = sym(A, 'clear'); assumptions(A)
ans = Empty sym: 1-by-0
Create the symbolic function f whose input arguments are symbolic variables x and y:
x = sym('x'); y = sym('y'); f(x, y) = sym('f(x, y)')
f(x, y) = f(x, y)
Alternatively, you can use the assignment operation to create the symbolic function f:
f(x, y) = x + y
f(x, y) = x + y
To create several symbolic variables in one function call, use syms. When using syms, do not enclose variables in quotes and do not use commas between variable names:
syms var1 var2 var3
syms also lets you create real variables or positive variables. It also lets you clear assumptions set on a variable.
assume and assumeAlso provide more flexibility for setting assumptions on variable.
When creating a symbolic function, use sym to create arg1,...,argN as symbolic variables. Then use the assignment operation to create the symbolic function f, for example:
x = sym('x'); y = sym('y'); f(x, y) = x + y
syms f(x, y) is equivalent to these commands:
x = sym('x'); y = sym('y'); f(x, y) = sym('f(x, y)')