Order of multiple variables in Matlab solve equations

Viewed 184

I read Matlab documentation and still have difficulty understanding order of multiple variables in Matlab solve equations

For example

syms f1(R,L) f2(R,L);
f1 = (80+R)^2 + (120*pi*L)^2 - 232^2;
f2 = R^2 + (120*pi*L)^2 - 176^2;
[R,L] = solve (f1,f2);
fprintf('Resistor is %s Ω\n',round(R,3));
fprintf('Inductance is %s H\n',round(L,3));

If I replace variable L with Z, result will be in inverse order. It seems to be alphabetical order and I have no idea what the exact rule it is.

1 Answers

Per the documentation for solve that you linked:

Y = solve(eqns,vars) solves the system of equations eqns for the variables vars and returns a structure that contains the solutions. If you do not specify vars, solve uses symvar to find the variables to solve for.

You don't specify which variables to solve for, so MATLAB attempts to deduce this by calling symvar. The order of the identifiers returned by symvar doesn't appear to be documented explicitly, so I would not recommend relying on any particular ordering.

If you want to specify which variables solve should solve for, and hence their order in the output, pass them in an array as the second argument of solve:

[R, L] = solve([f1, f2], [R, L]);
Related