ans =
Hi Sam,
syms x
A = [2 4 ; 1 5] ;
% Polynomial
q(x) = x^2 + 2*x + 15 ;
As was noted above by @Voss and @Ayush Anand and @Nihal (though see comment on that answer), when evaluating q(x) with a matrix input, the Symbolic Math Toolbox uses elementwise operators and scalar expansion. Hence
q(A)
is evaluated as
A.^2 + 2*A + 15
or more precisely
sym(A).^2 + 2*sym(A) + 15
My concern is with this line
% What it should be
qA = A^2 + 2*A + 15;
the result of which is
qA
Note that the 15 on the RHS is expanded to match the size of A, i.e., equivalent to
A^2 + 2*A + 15*ones(2)
If that's really "what it should be", then read no further.
However, the question said that this was a problem in linear algebra, so I suspect what's really wanted is
A^2 + 2*A + 15*eye(2)
As best I can tell, there's no simple way to convert the scalar polynomial q(x) to the analogous matrix polynomial Q(X) (maybe there is, and I think it could be done with a bit of effort). However, the matrix polynomial can be constructed manually as
X = symmatrix('X',2);
Q = symfunmatrix(X^2 + 2*X + 15*eye(2),{X})
Then we can evaluate at A
symmatrix2sym(Q(A))
As best I understand the current state of affairs, a symmatrix must have specified dimension, i.e. 2x2 in this case. In other words, I don't think there is any way to define Q(X) for a general n x n matrix X and then evaluate it for any square matrix of interest, should there be a need to do so.