Missing coefficients in symbolic expressions with coeffs

조회 수: 10 (최근 30일)
Nick Drake
Nick Drake 2012년 2월 24일
편집: Josef 2013년 10월 25일
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
Mech Princess
Mech Princess 2012년 7월 23일
Hi. does any one know how to do this when the expressions in A has constants also? I tried the code and it gets errors because of the constants. Thanks

댓글을 달려면 로그인하십시오.

채택된 답변

Nick Drake
Nick Drake 2012년 3월 1일
This is what I was able to do thanks for all the help. I will post the final solution to the larger problem if anyone wants to see it.
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+zv;-(m*yv+xv*q); yv-zv;zv-yv];
for i=1:length(A)
A(i,:)=sort(A(i,:));
end
xx = cat(1,xv ,yv,zv);
Aineq = zeros(numel(A),numel(xx));
for j=1:length(A)
for i=1:length(xx)
y=xx(i);
[c,t]=coeffs(A(j),y);
if (length(t)<2)
else
insert=c(2);
Aineq(j,i)=insert;
end
end
end
Aineq;
bineq=zeros(length(Aineq),1);
for temp=1:(length(zv))
bineq(temp,1)=1;
end
  댓글 수: 1
Mech Princess
Mech Princess 2012년 7월 24일
편집: Mech Princess 2012년 7월 24일
There is an error in the if-else statement: Anyone has a solution? Thanks
??? The following error occurred converting from sym to double:
Error using ==> mupadmex
Error in MuPAD command: DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use the VPA function instead.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2012년 2월 24일
xx = symvar(A);
Aineq = zeros(numel(A),numel(xx));
for j1 = 1:numel(A)
[v1,v2] = coeffs(A(j1),xx);
Aineq(j1,ismember(xx,v2)) = v1;
end
on Nick Drake comment: try code
xx = cat(1,xv ,yv,zv)
Aineq = zeros(numel(A),numel(xx));
for j1 = 1:numel(A)
[v1,v2] = coeffs(A(j1),xx);
Aineq(j1,ismember(xx,v2)) = v1;
end
on Nick Drake comment2, on PC (W7 32 bit, MATLAB R2011b):
  댓글 수: 2
Nick Drake
Nick Drake 2012년 2월 25일
Could you possibly explain this a little to me I am getting an error when trying to run the code
??? Error using ==> sym.eq at 20
Array dimensions must agree.
Error in ==> symvar at 27
[b,e] = findrun(s==' ');
Nick Drake
Nick Drake 2012년 3월 1일
I am still receiving an error when I ran this part of the code
>> xx = cat(1,xv ,yv,zv)
Aineq = zeros(numel(A),numel(xx));
for j1 = 1:numel(A)
[v1,v2] = coeffs(A(j1),xx);
Aineq(j1,ismember(xx,v2)) = v1;
end
xx =
x1
x2
x3
x4
x5
??? The following error occurred converting from sym to double:
Error using ==> sym.double at 25
DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use the VPA function instead.
>> whos
Name Size Bytes Class Attributes
A 8x1 700 sym
Aineq 8x5 320 double
i 1x1 8 double
j1 1x1 8 double
k 1x1 8 double
lm 1x1 8 double
m 2x2 32 double
q 2x1 16 double
v1 1x1 148 sym
v2 1x1 126 sym
xv 1x1 128 sym
xx 5x1 384 sym
yv 2x1 192 sym
zv 2x1 192 sym

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by