For loop of symbolic variables extracting symbolic coefficients

27 ビュー (過去 30 日間)
Nick Drake
Nick Drake 2012 年 2 月 23 日
コメント済み: Paul 2023 年 12 月 19 日
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?

回答 (5 件)

Walter Roberson
Walter Roberson 2012 年 2 月 23 日
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
Nick Drake 2012 年 2 月 24 日
m = 6;
mv = sym(zeros(1, m));
for k=1:m
mv(k) = sym(sprintf('m%d', k));
end
This is what I found and it works great. I do not have that new of a version of Matlab. I have tried using the coeff and coeffs but I have struggled to get the syntax correct. I will look more into this I will try and post and example that might be more easily worked on. give me a little more time.

Andrew Newell
Andrew Newell 2012 年 2 月 23 日

Nick Drake
Nick Drake 2012 年 2 月 24 日
Ok, I have been trying to use coeffs here is what I am currently have a problem with, although I have 5 symbolic variables, not all show up in every equation so when I try and gather coeffients I would like to be able to catch even the 0 values. heres what I have so far.
clc
clear
m= [2 -3 ; 3 -2];
q = [ -1 -1]';
xv = sym('x1');
lm = length(m);
yv = sym(zeros(1, lm))';
zv = sym(zeros(1, lm))';
for k=1:lm
yv(k) = sym(sprintf('x%d', k+1));
zv(k) = sym(sprintf('x%d', k+lm+1));
end
A = [-(m*yv+xv*q);m*yv+xv*q-zv; yv-zv;zv-yv];
Aineq=zeros;
temp = zeros;
for i=1:length(A)
temp=coeffs(A(i,:))
for j=1:length(temp)
Aineq(i,j)=temp(j)
end
end
Aineq
the last nested for was my attempt to gather the coefficients. I but I cannot place them in the right way this way. Any suggestions? I have looked at ineval and feval but I do not see the how to use these tools
Oh this is what my output looks like A =
-2*x2+3*x3+x1
-3*x2+2*x3+x1
2*x2-3*x3-x1-x4
3*x2-2*x3-x1-x5
x2-x4
x3-x5
x4-x2
x5-x3
Aineq =
1 -2 3 0
1 -3 2 0
-1 2 -3 -1
-1 3 -2 -1
1 -1 0 0
1 -1 0 0
-1 1 0 0
-1 1 0 0
So this is where I am stuck at this point.
  1 件のコメント
Paul
Paul 2023 年 12 月 19 日
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]
A = 
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
Aineq = 8×5
1 -2 3 0 0 1 -3 2 0 0 -1 2 -3 -1 0 -1 3 -2 0 -1 0 1 0 -1 0 0 0 1 0 -1 0 -1 0 1 0 0 0 -1 0 1

サインインしてコメントする。


Nick Drake
Nick Drake 2012 年 2 月 24 日
Ok so part of the problems that I think that I am having is that in my workspace there is no symbolic variable x1,x2.... defined. So i cant collect the coefficients i on the other hand have xv, yv,zv defined and I cannot seem to use coeffs with those symbolic vectors. Any help would be great.

Community Treasure Hunt

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

Start Hunting!

Translated by