MATLAB: Does the Symbolic Toolbox support symbolic matrix operations

matrixoperationssymbolicSymbolic Math Toolboxvector

I would like to define a variable and have the Symbolic Toolbox understand that it is a matrix and not a scalar.

Best Answer

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)]