Extracting the base name and size of symbolic variables

Hello,
I have a simple question. Consider the following:
syms a [3 3] real
syms x r [3 1] real
mu(x) = (r.*x).*(1-a*x)
Now, I would like to know the symbolic variables and their sizes in the symbolic function mu(x). If I use symvar(mu) I can only get the
variables in a scalar form and this command does not help to get the size of matrixes and vectors. You might think that it should not be
a big deal to write a code for this. However, ingeneral this is not possible. Foe instance, consider the following example:
syms x beta0 w w0 w1 w2
mu(x) = x+beta0+w+w0+w1+w2
In this example there is not a unique interpretation of the variables in terms of matrices, vectors, and scalars. One interpretation is to consider w = [w1 w2] as a vector. But, another interpretation is to consider al w's as scalar (which is the correct interpretation).
Why I need this?
I am performing a parameter estimation procedure to estimate the parameters of a function like mu(x). When I want to represent the results I do not like to display them scalar-wiae. rather I prefer to display the outcomes based on their actual sizes.
I hope MATLAB has a command to handled this issue?
Thanks,
Babank

Answers (1)

syms a [3 3] real
That is equivalent to
a = sym('a', [3 3], 'real')
mu(x) = (r.*x).*(1-a*x)
When scanning that code, the value of a is substituted, as-if you had written
mu(x) = (r.*x).*(1-sym('a', [3 3], 'real')*x)
The code does not remember that the value came from the variable a
So what you want to do is not possible.

4 Comments

You can potentially use symmatrix()
a = symmatrix('a', [3, 3])
syms x
r = symmatrix('r', [3, 1])
mu(x) = (r.*repmat(x,[3 1])) .* (repmat(1, 3,3) - a*repmat(x,3,3))
but
symvar(mu)
Error using symbolic.mixin.symbolicmatrix/symvar (line 862)
Convert input of class 'symmatrix' to 'sym' using 'symmatrix2sym' before applying function 'symvar'.
At the moment I do not know of any way to get the size of symmatrix, except by using whos()
Thanks Walter,
Do you, like me, think that it is needed to have a built-in matlab command to save such information. I believe it can simply be done in a more advanced symvar (perhaps symvar can be a structure where its fields can be the size of the corresponding symbolic variable).
In different areas of math/statistics we are concerned with estimating a function which can have many parameters where some of such oarameters can be matrices or vectors (or even tensors). In the following case, for instance, we have
syms a [3 3] real
syms x r s [3 1] real
mu(x) = (r.*x).*(1-a*x);
Now, imagine someone is going to make a MATLAB package (I am doing this now). At the end the user expect to se an elegant representation as follows:
Estimated paramers as as follows:
r is estimated to be : xxx
a is estimated to be: xxx
But, now imagine that we are going to do parameter estimation for the following function:
syms a [3 3] real
syms r1 r2 r3 real
mu(x) = (1-a*x)+r1+r2+r3;
Then the user expects the following output:
a is estimated to be:
r1 is estimated to be:
r2 is estimated to be:
r3 is estimated to be:
So, the package should respect the way a user likes to define his/her function (vectors, matrices, scalars). There are more complicated examples.
Best,
Babak
The substitution of the value for the variable is inherent in how symbolic mathematics works. There is no chance that you would be able to recover variable a from mu(x) = (r.*x).*(1-a*x);
You are right.
My point was about displaying the results in an elegant way which corresponds with the way the function is defined.

Sign in to comment.

Edited:

on 20 Sep 2024

Community Treasure Hunt

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

Start Hunting!