Why does SOLVE return an incorrect result?

Why does SOLVE return an incorrect result?
Specifically,
> syms I01 I1 V R ts tau I2 I02 V1 V2 di1 di2 L
> f1=R*I1+L*di1-V1;
> f2=R*I2+L*di2-V2;
> [R,L]=solve(f1,f2,'R,L');
> Rsym=simple(R);
> Lsym=simple(L);
After checked by hand, the solution for R and L is exchanged, and thus incorrect although the answer is correct had the it not been exchanged.
Is this a bug?

 Accepted Answer

This is not a bug.
This is an expected answer from SOLVE.
From the help entry of SOLVE:
...solves the system of equations implied by
eq1,eq2,...,eqn in the n variables determined by applying findsym to the system.
and in the help entry of FINDSYM:
FINDSYM(S), where S is a scalar or matrix sym, returns a string
containing all of the symbolic variables appearing in S. The
variables are returned in alphabetical order and are separated by
commas. If no symbolic variables are found, FINDSYM returns the
empty string.
Thus,
>> findsym(f1)
ans =
I1, L, R, V1, di1
>> findsym(f2)
ans =
I2, L, R, V2, di2
The output will follow the same sequence determined by FINDSYM and thus should be alphabetical, although you can name the output anything you like.
The best solution however is to have only one variable for the output of SOLVE. This variable will be of type structure, and each field will have the same name as the variable being solved:
Using the above example:
Solution=solve(f1,f2,'R,L');
and
Solution.L is solution of L and Solution.R is the solution of R.

More Answers (1)

syms x
syms x2A
% 함수식 f %
f = (2*x^2 + 4*x -3)/(x^2 + x - 4)
% 항등식 풀기, 대입 %
x1 = solve((2*x^2 + 4*x -3)/(x^2 + x - 4))
y1 = subs(f,x ,x1)
% 좌극한, 우극한 %
x2 = sym(sqrt(17)/2 - 1/2)
lim_left = limit(f,x,x2,'left')
lim_right = limit(f,x,x2,'right')

Community Treasure Hunt

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

Start Hunting!