Why Doesn't a Symbolic Matrix Equation Simplify Until After Applying subs() ?

52 views (last 30 days)
Following code is to derive an expression for the derivative of the magnitude of a vector function of time.
clearvars
syms t real
r = symfunmatrix('r(t)',t,[3,1]);
% results that follow are the same regardless of how R is declared
R = symfunmatrix('R(t)',t,[1,1]);
%syms R(t)
eqn = R(t)^2 == (r.'*r)
eqn(t) = 
eqn = diff(eqn)
eqn(t) = 
At this point, it would be nice if @doc:isolate worked for matrix equations, but it doesn't, so proceed by hand
Rx2 = R*2;
eqn = lhs(eqn)/Rx2 == rhs(eqn)/Rx2
eqn(t) = 
Why doesn't the software automatically simplify the lhs to just diff(R) as it would for a scalar equation?
It's almost as if the software does not know that matrix multipliation is associative (see below).
We could proceed by explicitly converting the lhs to symfun, but shouldn't have to.
symfunmatrix2symfun(lhs(eqn)) == rhs(eqn)
ans(t) = 
Also, if the lhs wern't a scalar then I think the conversion to symfun would result in the lhs defined in terms of its elements, which would be annoying at best and unworkable at worst depending on the dimensions of the problem.
Recognizing that r(t)*inv(R(t)) is a unit vector (don't worry about the case where R(t) == 0)
r_hat = symfunmatrix('r_hat(t)',t,[3,1]);
eqn3 = r_hat == r/R
eqn3(t) = 
then substitute
eqn = subs(eqn,rhs(eqn3),lhs(eqn3))
eqn(t) = 
Lo and behold, the substitution simplifies the lhs. I'm happy that the lhs did simplify, but I'd like to understand why it simplified (and why it did not simplify upstream in the development).
Getting back to that idea of the associative property of matrix multiplication, with R defined as symfunmatrix
diff(R)*(R/R) % expected result
ans(t) = 
(diff(R)*R)/R % correct result, but should be handled better?
ans(t) = 
Oddly enough, it appears that the preceding result is related to R(t) being 1 x 1. No problem with a symfunmatrix of larger dimension
R = symfunmatrix('R(t)',t,[2,2]);
(diff(R)*R)/R
ans(t) = 

Accepted Answer

dpb
dpb on 19 Oct 2025 at 11:52
I'd suggest your best chance for a definitive answer plus for there to be any chance of behavior more as you would like you should submit this to Mathworks as an official support request/enhancement request at <Product Support Page>
  2 Comments
Paul
Paul 20 minutes ago
I usually post here first in case there's somehting that I don't understand about the issue and in case anyone else has the same problem.
In any event, I did open a support case and was told that MathWorks will consider implementing a fix in a future release.
dpb
dpb 11 minutes ago
That's typically not a bad course of action, but with the internals of the Symbolic TB, the likelihood of anybody outside Mathworks having any in-depth insight to its internal workings is pretty much nil which is why I made the suggestion directly.
Working with the TB to solve a syntax error or somesuch, there are those who use it enough to have excellent chances to solve an issue; how it decides what and when to do an internal conversion, "not so much".

Sign in to comment.

More Answers (0)

Products


Release

R2025b

Community Treasure Hunt

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

Start Hunting!