Matrix versus Scalar Input Into Functions Using Symbolic Toolbox

4 vues (au cours des 30 derniers jours)
Sam Blake
Sam Blake le 16 Avr 2024
Commenté : Sam Blake le 17 Avr 2024
Hello, I am exploring the trying to do some linear algebra using the symbolic toolbox and I have noticed something strange while trying to subsitute matrices into polynomial functions defined by the symbolic toolbox. In the following code i will define a 2x2 matrix 'A' and try to plug it into a polynomial 'q(x)' to solve for its solution. What I get from utilizing the symbolic toolbox function of calling q(x) when x=A differs from when I just plug A into the polynomial for x like you would do normally. Another thing is if you plug in a scalar value, it performs it normally with no problem, so its a linear algebra thing. Just to clarify, I want the function to yield what qA yields, not q(A). If anyone has any insight on how the symbolic toolbox is performing these operations and why it isnt coming out as I intend I would be very appreciative. Thank you for your time
syms x
s = symengine
s = MuPAD symbolic engine
A = [2 4 ; 1 5] ;
% Polynomial
q(x) = x^2 + 2*x + 15 ;
% What it should be
qA = A^2 + 2*A + 15 ;
isequal(qA,double(q(A)))
ans = logical
0
% Now lets try a scalar x=2
q2 = 2^2 + 2*2 + 15 ;
isequal(q2,double(q(2)))
ans = logical
1

Réponse acceptée

Paul
Paul le 17 Avr 2024
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)
ans = 
is evaluated as
A.^2 + 2*A + 15
ans = 2x2
23 39 18 50
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
or more precisely
sym(A).^2 + 2*sym(A) + 15
ans = 
My concern is with this line
% What it should be
qA = A^2 + 2*A + 15;
the result of which is
qA
qA = 2x2
27 51 24 54
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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)
ans = 2x2
27 51 24 54
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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)
ans = 2x2
27 36 9 54
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
e.g., as would be used in the Cayley-Hamilton Theorem.
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})
Q(X) = 
Then we can evaluate at A
symmatrix2sym(Q(A))
ans = 
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.
  1 commentaire
Sam Blake
Sam Blake le 17 Avr 2024
You are right about the something like CH theorem and the identity matrix on the 15 at the end being in the real application. This is a hollowed out example to show my real problem of matrix input into functions using symbollic toolbox, which you just showed me so thank you! The other answers from Nihal and Ayush are helpful as well. Thank you everyone, I think I have a good grasp on how matlab is performing this and what I need.

Connectez-vous pour commenter.

Plus de réponses (3)

Hornett
Hornett le 16 Avr 2024
Modifié(e) : Hornett le 16 Avr 2024
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
  1. 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.
  2. 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.
  2 commentaires
Sam Blake
Sam Blake le 16 Avr 2024
Well said! Thank you for the explanation. So by calling q(A), i am actually evaluating that polynomial at the matrix A, not just subbing it in for x and solving as is. Thank you for your time, that makes a lot of sense now, i will stick with the manual substitution.
Paul
Paul le 16 Avr 2024
"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."
Is that not contradictory to "For this case former [q(A)] is equivalent to element-wise operation and another one is doing matrix multiplication."
The latter statement is correct, i.e., q(A) expands the expression elementwise, but that's quite different that than following the rules of matrix algebra.
Also, I'm not clear on how polyvalm comes into the dicussion for q(A). q(A) is not similar to using polyvalm with the polynomial coefficients and A.

Connectez-vous pour commenter.


Voss
Voss le 16 Avr 2024
% What it should be
qA = A.^2 + 2*A + 15 ;
%     ^^ element-wise operation

Ayush Anand
Ayush Anand le 16 Avr 2024
Hi,
The discrepancy you observe is due to the difference in mathematical evaluation of matrix algebra (matrix exponentiation and multiplication), and how the symbolic toolbox handles inputs to the polynomial function defined by q(x)(scalar operations).
The operation (A^2) means (A * A) (matrix multiplication), not element-wise squaring. When you define qA above, the operations executed are matrix multiplications, and hence you get the answer you would expect from matrix algebra. However, when you are substituting the matrix A in the polynomial equation defined by the symbolic toolbox, x^2 is instead element-wise squaring, rather than matrix multiplication, hence you get different outputs.
You can read more about how the symboli toolbox handles different cases here: https://www.mathworks.com/help/symbolic/getting-started-with-symbolic-math-toolbox.html

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by