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 Comment

The doc describes this in the 3rd example here. Is the example unclear?
Karan (Symbolic doc)

Sign in to comment.

Answers (1)

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 Comments

Hi Walter
thank you for your response!
I changed the code according to your reply:
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]);
fn = fieldnames(B);
vals = struct2cell(B);
temp = [fn(:).'; vals(:).'];
subs(AAA, temp{:})
and it gave:
EDU>> subs(AAA, temp{:})
Error using sym.subs
Too many input arguments.
How do I solve it? Thank you!
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})
Hi Walter
I really appreciated your prompt reply.
I tried again on Matlab:
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);
subs(AAA, B);
and it did not return exact solution of 'xx', 'yy' or 'zz'. if I type 'B' in the command window, it returned:
EDU>> B
B =
xx: [1x1 sym]
yy: [1x1 sym]
zz: [1x1 sym]
thank you!
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
thank you! I will find out more.

Sign in to comment.

Asked:

on 30 Aug 2017

Commented:

on 30 Aug 2017

Community Treasure Hunt

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

Start Hunting!