How to generate symbolic variables dynamically at run-time?

2 visualizaciones (últimos 30 días)
Ali
Ali el 21 de En. de 2011
Comentada: Steven Lord el 8 de Abr. de 2019
I am trying to generate a set of equations for user input matrices.
For example, if the user inputs:
[0 1 0 1;
1 0 1 0;
0 0 1 1];
I want to generate equations
Y1 = Q12 + Q44
Y2 = Q21 + Q31
Y3 = Q33 + Q44
and assign these equations to symbolic vars y1, y2, and y3.
.
In addition, at some other point I want to be able to substitute values symbolic values for Q12 = P12 + R1, etc, and have all the equations automatically update all the symbolic values.
.
I have tried to use:
y = sym(sprintf('q%d%d', [m k]))
method described here, but the matrix elements q12, q44, etc are not substitutable. These vars are not symbolic variable in themselves.
.
The 'eval(..)' function and other symbolic manipulation functions cannot be used to simply/substitute other values.
I can do something like this:
syms q11, q12, q13, .. q44 etc
for all positions of the matrix, and then do:
Y = [q11, q12,..; ... q44]
but this is tedious and does not allow for a general function that handles any matrix of any size.
I guess, my question is how to dynamically create symbolic variables. I can generate a character string 'Q12', but then I can't seem to be able to convert it to a symbolic variable.
For example, if vars = {'Q12'}, then
char(vars(1)) = sym(char(vars(1)), 'real')
gives an error
"Conversion to double from cell is not possible."
  1 comentario
Christopher Creutzig
Christopher Creutzig el 24 de En. de 2011
What exactly do you mean by “assign these equations to symbolic vars y1, y2, and y3”? Also note that you can set Y = sym('q', [4 4]) as a shorthand for your tedious definition of Y.

Iniciar sesión para comentar.

Respuesta aceptada

Kenneth Eaton
Kenneth Eaton el 24 de En. de 2011
You can generate your system of equations from a matrix of zeroes and ones using just the SYM function and some basic arithmetic operations like SUM and .* that are overloaded for symbolic objects:
>> C = [0 1 0 1; 1 0 1 0; 0 0 1 1]; % The sample input matrix
>> Q = sym(sym('Q%d%d',size(C)),'real') % Create a matrix of Qxy variables
Q =
[ Q11, Q12, Q13, Q14]
[ Q21, Q22, Q23, Q24]
[ Q31, Q32, Q33, Q34]
>> Y = sum(sym(C).*Q,2) % Perform element-wise multiplication of Q by a
% symbolic version of C, then sum the result
Y = % along the rows
Q12 + Q14
Q21 + Q23
Q33 + Q34
  4 comentarios
Andrew Newell
Andrew Newell el 26 de En. de 2011
Interesting. Any idea why it won't accept Q12 without quotes?
Kenneth Eaton
Kenneth Eaton el 26 de En. de 2011
I guess it doesn't accept Q12 because that isn't a symbolic variable *in the workspace*, just a symbolic variable *within the set of equations* in Y.

Iniciar sesión para comentar.

Más respuestas (2)

Walter Roberson
Walter Roberson el 21 de En. de 2011
Your code,
if vars = {'Q12'}, then char(vars(1)) = sym(char(vars(1)), 'real')
would need to be
if isequal(vars, {'Q12'}), then char(vars(1)) = sym(char(vars{1}), 'real')
in order to be consistent.
Have you consider using
Q = sym('Q', size(UserMatrix));
Your example equations look inconsistent to me, by the way. I do not see how Q44 can occur in two of them.
  2 comentarios
Ali
Ali el 24 de En. de 2011
Hello Walter,
Thank you for your reply.
The equations are inconsistent, it was a typo. It was a quick example I made up to describe what I was doing. The number of equations that I have is much larger.
The use of eval(sprintf(...)) I found to be very helpful in doing what I wanted Matlab to do.
I did try:
char(vars(1)) = sym(char(vars{1}), 'real')
but I still got am error.
But thank you for the reply.
Ali
Walter Roberson
Walter Roberson el 23 de Feb. de 2012
Note: that sym() syntax with a size is R2010b or later.

Iniciar sesión para comentar.


Viki
Viki el 8 de Abr. de 2019
Consider a symbolic matrix created as,
S = sym('s%d%d',[3,3], 'real');
It would fetch you
S =
[ s11, s12, s13]
[ s21, s22, s23]
[ s31, s32, s33]
Now, if you want to have each element of the symbolic matrix S as an independent symbolic variable; use the following code
for i = 1:3
for j = 1:3
feval('syms',S(i,j));
end
end
  1 comentario
Steven Lord
Steven Lord el 8 de Abr. de 2019
Doing this "poofs" variables into the workspace and should be discouraged for the reasons given here.

Iniciar sesión para comentar.

Categorías

Más información sobre Numbers and Precision en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by