MATLAB: Solving Symbolic system of equations returns incorrect solutions.

Extended Symbolic Math ToolboxsolvesymbolicSymbolic Math Toolboxsymssymvarsystem of equations

Matlab is not returning the correct solution to this system of symbolic equations.
syms a1 a2 a3 dx
[Sa1 Sa2 Sa3]= solve(a1+a2+a3==0,a2+2*a3==1/dx,0.5*a2+2*a3==0)
The correct answer should be a1 = -3/2*dx ; a2 = 2/dx ; a3 = -1/2*dx;
Any insight into what I am doing wrong would be greatly appreciated!

Best Answer

MATLAB is solving for a1, a2, and dx in terms of a3. Tell MATLAB you want to solve for a1, a2, and a3 in terms of dx by passing the list of variables as the last inputs. I also recommend calling solve with one output, not three, as then the field names of that struct will match the variables for which you're solving.
>> syms a1 a2 a3 dx
>> S = solve(a1+a2+a3==0, a2+2*a3==1/dx, 0.5*a2+2*a3==0, a1, a2, a3);
>> [S.a1, S.a2, S.a3]
ans =
[ -3/(2*dx), 2/dx, -1/(2*dx)]
>> eq1 = S.a1 + S.a2 + S.a3
eq1 =
0
>> eq2 = S.a2 + 2*S.a3
eq2 =
1/dx
>> eq3 = 0.5*S.a2 + 2*S.a3
eq3 =
0