Operations and differentiation with symmatrix in MATLAB... possible bug?

조회 수: 2 (최근 30일)
dominik
dominik 2024년 4월 4일
편집: Paul 2024년 4월 11일
Hi
I have found matlab to behave unexpectedly when using the symmatrix. I dont understand it, maybe i misunderstood the way operators work, or maybe its a bug?
Any help would be greatly appreciated! Thank you!
I declare some symbolic objects using symmatrix
clear all
n = 2;
A = symmatrix('A', n);
lambda = symmatrix('lambda', [n,1]);
g = symmatrix('g', [n,1]);
phi = symmatrix('phi', [n,1]);
l = symmatrix('l', [n,1]);
item = symmatrix('l', [n,1]);
I declare 2 almost identical equations
arga = (l.'*(((lambda).*(A * g) + (phi.'*lambda) * (A * g))));
argb = (l.'*(((lambda).*(item) + (phi.'*lambda) * (A * g))));
arga and argb should be the interchangable, as both item and A*g are 2x1 parameter vectors.
I differenciate wrt lambda:
sola = diff(arga, lambda)
solb = diff(argb, lambda)
I get
sola =
l.'*(symmatrix(eye(2)) .* A*g + kron(phi.', A*g))
solb =
l.'*(kron(phi.', A*g) + symmatrix(eye(2)) .* l)
ProbIem 1: I cannot evaluate the sum in the parenthesis of sola due to dimensionality issues. the probelm doesnt appear for solb.
Problem 2: Running the numerical example below, sola and solb give two results different whereas this should not be the case
l=[1.2;1.1] ;
g=[1.9876;1.88] ;
phi=[1.0987;1.5192]
A=[132 123;1222 124] ;
item=A * g;
% sola, having removed only the symmatrix comand
l.'*((eye(2)) .* A*g + kron(phi.', A*g))
% solb, having removed only the symmatrix comand
l.'*(kron(phi.', A*g) + (eye(2)) .* l)
I get
ans =
1.0e+03 *
4.4392 5.9196
ans =
1.0e+03 *
3.8694 5.3495

답변 (1개)

Paul
Paul 2024년 4월 4일
이동: Paul 2024년 4월 5일
Hi dominik,
I'm not sure what Problem 1 is refering to.
I think Problem 2 can be explained as follows:
clear all
n = 2;
A = symmatrix('A', n);
lambda = symmatrix('lambda', [n,1]);
g = symmatrix('g', [n,1]);
phi = symmatrix('phi', [n,1]);
l = symmatrix('l', [n,1])
l = 
item = symmatrix('l', [n,1])
item = 
We see that the Matlab variables l and item both reference the same symbolic variable.
arga = (l.'*(((lambda).*(A * g) + (phi.'*lambda) * (A * g))))
arga = 
argb = (l.'*(((lambda).*(item) + (phi.'*lambda) * (A * g))))
argb = 
In argb, we see that the symbolic l shows up in the expression on the right.
sola = diff(arga, lambda)
sola = 
solb = diff(argb, lambda)
solb = 
l=[1.2;1.1] ;
g=[1.9876;1.88] ;
phi=[1.0987;1.5192] ;
A=[132 123;1222 124] ;
Here, item is set to A*g, but item is not a variable in the expression for solb. So setting item here has no affect on the results and because l ~= A*g we get different results for sola and solb.
%item=A * g;
% sola, having removed only the symmatrix comand
Also, I think A*g must be enclosed in parentheses.
l.'*((eye(2)) .* (A*g) + kron(phi.', A*g))
ans = 1x2
1.0e+03 * 4.4603 8.2765
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% solb, having removed only the symmatrix comand
l.'*(kron(phi.', A*g) + (eye(2)) .* l)
ans = 1x2
1.0e+03 * 3.8694 5.3495
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Maybe the intent was for item to be its own variable
%clear all
n = 2;
A = symmatrix('A', n);
lambda = symmatrix('lambda', [n,1]);
g = symmatrix('g', [n,1]);
phi = symmatrix('phi', [n,1]);
l = symmatrix('l', [n,1]);
item = symmatrix('Q', [n,1])
item = 
arga = (l.'*(((lambda).*(A * g) + (phi.'*lambda) * (A * g))));
argb = (l.'*(((lambda).*(item) + (phi.'*lambda) * (A * g))));
sola = diff(arga, lambda)
sola = 
solb = diff(argb, lambda)
solb = 
l=[1.2;1.1] ;
g=[1.9876;1.88] ;
phi=[1.0987;1.5192] ;
A=[132 123;1222 124] ;
%item=A * g;
Q = A * g;
% sola, having removed only the symmatrix comand
l.'*((eye(2)) .* (A*g) + kron(phi.', A*g))
ans = 1x2
1.0e+03 * 4.4603 8.2765
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% solb, having removed only the symmatrix comand
l.'*(kron(phi.', A*g) + (eye(2)) .* Q)
ans = 1x2
1.0e+03 * 4.4603 8.2765
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  댓글 수: 8
Paul
Paul 2024년 4월 11일
편집: Paul 2024년 4월 11일
AFAIK there's no way to do that (but will keep thinking about it), maybe someone else will chime in with workable approach.
It would be cool to be able to use symmatrix with symbolic dimensions, and be able to matlabFucntion the same, something like
syms n m integer
A = symmatrix('A',[n n]);
B = symmatrix('B',[n m]);
C = A*B;
cfunc = matlabFunction(C);
Maybe you should consider submitting an enhancement request. They do improve functionality, e.g., diff on symmatrix arrived in r2023b.
Did you get a reponse on your bug report?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Assumptions에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by