For loop of symbolic variables extracting symbolic coefficients
Mostrar comentarios más antiguos
I want to create a certain number of symbolic variables without having to define them individually. I want to basically do something as the follows
for i = 1:length(m)+1 %m is some matrix input to a larger problem
syms x(i)
end
so that lets say if m has length 2 i can create 3 symbolic variables x1 x2 x3. Again these problems are larger than 2x2 and can grow and I would like to be able to automate the creation of the symbolic variables if possible.
The other questions I have is once I have created these symbolic variables and done some computations and create a matrix of symbolic equations is there a way to extract the coefficients of the symbolic matrix to another matrix?
Respuestas (5)
Walter Roberson
el 23 de Feb. de 2012
This is seldom a good idea, for reasons similar to those described in http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
Have you considered symbolic arrays? R2010b and later gives an easy way to create those, http://www.mathworks.com/help/toolbox/symbolic/sym.html
A = sym('A',[m n]) creates a m-by-n matrix of symbolic variables. The dimensions m and n of a matrix must be integers. You can use this syntax to create a 1-by-n or an n-by-1 vector. The sym function does not allow you to use symbolic variables without assigned numeric values as dimensions. By default, the generated names of elements of a vector use the form Ak, and the generated names of elements of a matrix use the form Ai_j. The base, A, must be a valid variable name. (To verify whether the name is a valid variable name, use isvarname.) The values of k, i, and j range from 1 to m or 1 to n. To specify other form for generated names of matrix elements, use '%d' in the first input. For example, A = sym('A%d%d', [3 3]) generates the 3-by-3 symbolic matrix A with the elements A11, A12, ..., A33.
If you need a 3D array or you are using an earlier symbolic toolbox, then see http://www.mathworks.com/matlabcentral/answers/2521-creating-vector-of-symbols
For extracting coefficients: see coeff() and coeffs() and map(), especially within MuPAD itself: http://www.mathworks.com/help/toolbox/mupad/stdlib/ You may need to use feval() or evalin() in order to invoke these from MuPAD. For example,
evalin(symengine, 'map', 'coeff', AA, 'x2', 1)
Nick Drake
el 24 de Feb. de 2012
1 voto
Andrew Newell
el 23 de Feb. de 2012
0 votos
Nick Drake
el 24 de Feb. de 2012
1 comentario
m = [2 -3 ; 3 -2];
q = [ -1 -1]';
xv = sym('x1');
lm = length(m); % use size() instead?
Xv = sym('x',[2*lm+1 1]);
xv = Xv(1);
yv = Xv(2:lm+1);
zv = Xv(lm+2:end);
A = [-(m*yv+xv*q);m*yv+xv*q-zv; yv-zv;zv-yv]
Aineq = zeros(size(A,1),numel(Xv));
for ii = 1:size(A,1)
[c,t] = coeffs(A(ii,:));
[tf,locb] = ismember(t,Xv);
Aineq(ii,locb) = c;
end
Aineq
Nick Drake
el 24 de Feb. de 2012
0 votos
Categorías
Más información sobre Common Operations en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
