multi variables in an array and solved by multi-equations
显示 更早的评论
Hi All
I searched almost the entire community for the answer and did not find it. the original code:
syms xx yy zz
eqn1=xx+yy==6;
eqn2=xx/yy==4;
eqn3=zz+yy==3;
AAA=[eqn1 eqn2 eqn3];
B=[xx yy zz];
B=vpasolve([AAA]);
The results is still:
EDU>> B
B =
xx: [1x1 sym]
yy: [1x1 sym]
zz: [1x1 sym]
but if I use the regular form:
[xx yy zz]=vpasolve([AAA]);
the system will give me the right answer.
The question is: How can I solve multi-variables in an array by multi-equations? this program is just a testing trial for another program. I need to find out how to solve the array instead of just typing all variables.
Thank you very much!
1 个评论
Karan Gill
2017-8-30
Karan (Symbolic doc)
回答(1 个)
Walter Roberson
2017-8-30
B=vpasolve([AAA]); is giving you the right answer. It is returning a struct with one field for every variable. If you need to extract individual variable, you can refer to them in the field, such as B.yy . If you need to subs() in all of the values into equations, you can subs() the structure, such as subs(eqns, B) which will be equivalent
fn = fieldnames(B);
vals = struct2cell(B);
temp = [fn(:).'; vals(:).'];
subs(eqns, temp{:})
that is, it would be equivalent to
subs(eqns, 'xx', B.xx, 'yy', B.yy, 'zz', B.zz)
5 个评论
shanghua chen
2017-8-30
编辑:Walter Roberson
2017-8-30
Walter Roberson
2017-8-30
syms xx yy zz
eqn1=xx+yy==6;
eqn2=xx/yy==4;
eqn3=zz+yy==3;
AAA=[eqn1 eqn2 eqn3];
B = vpasolve(AAA);
subs(AAA, B)
The above is all that is needed.
Sorry, my previous code involving fieldnames was not valid. I should have said that
subs(AAA, B)
is equivalent to
fn = fieldnames(B);
vals = struct2cell(B);
subs(AAA, fn, vals)
in this case that would be equivalent to
subs(AAA, {'xx', 'yy', 'zz'}, {B.xx, B.yy, B.zz})
shanghua chen
2017-8-30
编辑:Walter Roberson
2017-8-30
Walter Roberson
2017-8-30
The line
B=[xx yy zz];
is not doing anything useful there. You overwrite B on the next line, which is
B= vpasolve(AAA);
Also,
subs(AAA, B)
is not intended to return exact solutions of xx, yy, or zz: it is intended to use their values in further computation.
To view the values, you might be tempted to use
subs([xx yy zz], B)
However in the general case, the solutions in B might be vectors for each entry rather than scalars for each entry, and then it becomes difficult to read out which value corresponds to which variable. So you could do
for fn = fieldnames(B).'; disp(fn{1}); disp(B.(fn{1})); end
shanghua chen
2017-8-30
类别
在 帮助中心 和 File Exchange 中查找有关 Common Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!