The discrepancy you're observing between q(A) and qA when A is a matrix stems from how matrix operations, specifically matrix exponentiation and polynomial evaluation, are handled in MATLAB's Symbolic Toolbox.
When you define q(x) = x^2 + 2*x + 15; and then evaluate q(A) with A being a matrix, MATLAB internally uses a function similar to polyvalm for polynomial matrix evaluation. This process is different from simply substituting the matrix A into the polynomial equation as you did with qA = A^2 + 2*A + 15;. The reason for this difference lies in how matrix multiplication (and thus matrix exponentiation) works, which doesn't distribute over addition in the same way scalar multiplication does.
For scalars, x^2, 2*x, and constants like 15 behave as you would expect because scalar operations are commutative and distributive. However, for matrices, operations are not commutative (A*B ≠ B*A in general), and special care must be taken when performing polynomial evaluations.
Technical Explanation
- Matrix Polynomial Evaluation (q(A)): When you evaluate a polynomial at a matrix using MATLAB's symbolic toolbox, it doesn't simply substitute the matrix into the polynomial. Instead, it computes the result in a manner that's aware of matrix algebra rules. This involves using techniques that ensure the polynomial is evaluated correctly in the context of matrix operations, taking into account the non-commutative nature of matrix multiplication.
- Direct Substitution (qA): When you manually compute A^2 + 2*A + 15, you're performing matrix operations directly. A^2 is the matrix A multiplied by itself, 2*A is the matrix A scaled by 2, and 15 is added to each element of the resulting matrix because of how MATLAB handles the addition of a scalar to a matrix. This direct approach doesn't account for the nuances of evaluating a polynomial at a matrix, which is why you get a different result from q(A).
Why They Differ
The key point is that evaluating a polynomial at a matrix (i.e., computing q(A)) is not the same as substituting the matrix into the polynomial expression (i.e., computing qA). For this case former is equivalent to element-wise operation and another one is doing matrix multiplication.
Conclusion
The difference you're seeing is expected due to the nature of matrix algebra. If you want to evaluate a polynomial at a matrix and get the result that matches the manual substitution (qA), you need to continue using the direct substitution method for matrices or change the equation to elementwise multiplication. For scalar inputs, both methods yield the same result because scalar operations do not have the non-commutative property of matrix operations.