matlab solve gives wrong output

8 views (last 30 days)
Nick Haverals
Nick Haverals on 4 Jun 2015
Commented: Walter Roberson on 5 Jun 2015
I try to solve following equations with this code:
First i declare some parameters:
d35y=5;
d35z=5;
d37y=10;
d37z=10;
d57y=5;
d57z=5;
F3y = 10;
F3z = -20;
Then i declare the variables and solve it:
syms F5y F5z F7y F7z;
a = - F5y + F7y - F3y;
b = F5z + F7z + F3z;
c = -(d57z*F7y) + (d57y*F7z) - (d35z*F3y) - (d35y*F3z);
d = (d35y*F5z) - (d35z*F5y) + (d37y*F7z) + (d37z*F7y);
[F5y, F5z, F7y, F7z] = solve(a,b,c,d);
F5y = double(F5y)
F5z = double(F5z)
F7y = double(F7y)
F7z = double(F7z)
As output it SHOULD give me (when i calculate it by hand it is correct):
F5y = -20
F5z = 40
F5y = -10
F5z = -20
However MATLAB gives me this as output:
F5y = -20
F5z = -10
F7y = 40
F7z = -20
--> which is *not* correct.
Why does MATLAB gives me an incorrect answer?
Thanks in advance !

Answers (1)

Walter Roberson
Walter Roberson on 5 Jun 2015
Try specifying the order of the variables explicitly,
[F5y, F5z, F7y, F7z] = solve(a,b,c,d, F5y, F5z, F7y, F7z);
Alternately, return an output structure
sol = solve(a,b,c,d);
[sol.F5y, sol.F5z, sol.F7y, solve.F7z]
  2 Comments
Nick Haverals
Nick Haverals on 5 Jun 2015
Thanks a lot for your answer.
You are right, it works and gives a correct solution.
But why doesn't it work with my code, when it on another computer of a friend works (same version of matlab, exactly the same code)?
Walter Roberson
Walter Roberson on 5 Jun 2015
Is it possible that one of the two has Maple installed as the symbolic toolbox?
The order you used for the variables is what is documented, which is to say the order used by symvar() which should have sorted the way you wanted. But I never count on that ordering, which is a bit odd.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!