Does the Symbolic Toolbox support symbolic matrix operations?

6 views (last 30 days)
I would like to define a variable and have the Symbolic Toolbox understand that it is a matrix and not a scalar.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 18 Oct 2013
To work with matrices in the Symbolic Toolbox, a variable must have each of its elements explicitly defined to be recognized as a matrix. Otherwise, variables are treated as scalars.
For example, this code snippet will define two symbolic matrices:
M = sym(zeros(2,2));
M1 = sym(zeros(2,2));
for row = 1:2;
for col=1:2;
M(row,col) = sym(['a' num2str(row) num2str(col)]);
M1(row,col) = sym(['b' num2str(row) num2str(col)]);
end
end
These two matrices can now be multiplied together:
M*M1
ans =
[ a11*b11+a12*b21, a11*b12+a12*b22]
[ a21*b11+a22*b21, a21*b12+a22*b22]
M1*M
ans =
[ a11*b11+a21*b12, b11*a12+b12*a22]
[ b21*a11+b22*a21, a12*b21+a22*b22]
If your matrix elements are defined by an equation, use MESHGRID to help in defining that matrix. For example, the following code will create a symbolic matrix whose elements are described by the equation 1/(i+j-t), where "i" is the row index and "j" is the column index:
syms t
n = 3;
[J,I] = meshgrid(1:n);
A = sym(1./(I + J - t));
This produces
A
A =
[ 1/(2-t), 1/(3-t), 1/(4-t)]
[ 1/(3-t), 1/(4-t), 1/(5-t)]
[ 1/(4-t), 1/(5-t), 1/(6-t)]
  1 Comment
Walter Roberson
Walter Roberson on 11 Jul 2016
The answer is NO, you cannot use sym or syms to define a variable as being a general matrix, you can only define it as being a matrix with a particular size and contents (possibly symbolic.) Operations then proceed on elements using the normal rules -- so for example A * B will only work if size(A,2) == size(B,1) . The results will not be in terms of A and B but will be in terms of the contents of the matrices.
The information that I find suggests that the symbolic engine itself, MuPAD, does not have a defined way of declaring that something is a matrix of unknown size, and does not have a defined way of doing (algebraic) matrix multiplication on two generic matrices -- only of doing multiplications upon the elements.
This is unlike (for example) Maple; Maple does have ways of creating non-commutative operations on generalized matrices.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!