Filter löschen
Filter löschen

Using symbolic matrix operations correctly?

4 Ansichten (letzte 30 Tage)
Eric Zhang
Eric Zhang am 27 Jul. 2016
Beantwortet: Andrei Bobrov am 27 Jul. 2016
I am new to symbolic computations in MATLAB and am trying out matrix differentiations.
If we take derivative of trace(X*A) w.r.t. X, we should get A^T.
But MATLAB tells me
>> syms X A;
>> diff(trace(X*A), X)
ans =
A
Where am I wrong?

Akzeptierte Antwort

John D'Errico
John D'Errico am 27 Jul. 2016
As you have defined them, X and A are SCALAR symbolic objects, not general matrices. So the trace operation is a no-op, essentially ignored as the trace of a scalar, and the differentiation does not see them as matrices, since A and X are indeed scalars.
However, if A and X are symbolic matrices, it appears that diff does not allow differentiation with respect to a symbolic matrix. So:
>> A = sym('A',[2,2])
A =
[ A1_1, A1_2]
[ A2_1, A2_2]
>> X = sym('X',[2,2])
X =
[ X1_1, X1_2]
[ X2_1, X2_2]
>> diff(trace(A*X),X)
Error using sym/diff (line 69)
The second argument must be a variable or a nonnegative integer specifying the number of differentiations.
So as you can see, this produces an error as you wish to do the operation. Yet, if you compute the derivatives wrt the scalar elements of X, we see the proper elements of the transpose of A are indeed produced.
>> diff(trace(A*X),X(1,2))
ans =
A2_1
>> diff(trace(A*X),X(2,1))
ans =
A1_2

Weitere Antworten (2)

Andrei Bobrov
Andrei Bobrov am 27 Jul. 2016
reshape(jacobian(trace(A.*X),X(:)),size(A))

Azzi Abdelmalek
Azzi Abdelmalek am 27 Jul. 2016
A is not defined as a symbolic matrix.
A=sym('a',5)
syms x
diff(trace(x*A),x)

Community Treasure Hunt

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

Start Hunting!

Translated by