Zubair Ahmed
Zubair Ahmed

Reputation: 41

Matlab system of equations solver returns incorrect answers

It's not clear how the system of equations solver works. I looked at the documentation and attempted to replicate it to get a solution to a physics problem but got the incorrect answer.

The problem I am trying to solve is a statics physics problem. F1 and F3 are vectors that are pointed up and to the right in the first quadrant. 45 degrees represents the angle from the x axis to F1, and g represents the angle from the x axis to F3. F2 is a vector pointed down and to the left in the third quadrant. 45 degrees also represents the angle from the x axis to F2.

The problem is to find the values of F3 and g to make the system stable.

after getting the incorrect answer I checked to see if the matlab answer was maybe just a different answer that also solves the system but after checking it didn't work.

theta = 45;
F1 = 8;
F2 = 16;

syms F3 g;
eq1 = F1*cosd(theta) + F3*cosd(g) == F2*cosd(theta);
eq1 = F1*sind(theta) + F3*sind(g) == F2*sind(theta);

sol = solve([eq1,eq2],[F3,g]);
double(sol.F3)
double(sol.g)

The output was F3 = 45 and g = -135

the answer should be F3 = 8 and g = 45 degrees.

I am certain that the equations that I used are correct because when I put them into desmos and graphed them I got the correct answer. So the problem must have been my syntax in the script.

Upvotes: 1

Views: 59

Answers (1)

rayryeng
rayryeng

Reputation: 104555

Typo here:

syms F3 g;
eq1 = F1*cosd(theta) + F3*cosd(g) == F2*cosd(theta);
eq1 = F1*sind(theta) + F3*sind(g) == F2*sind(theta); % HERE

The second equation should be eq2, not eq1. eq2 is probably cached from previous computations and you used that instead.

Running this now I get:

>> double(sol.F3)

ans =

     8
    -8

>> double(sol.g)

ans =

    45
  -135

You can discard the negative solutions as they don't make sense physically, so you do indeed get 8 and 45 as per your expectations.

Word of advice

Use clearvars in your MATLAB script before you start any work. It prevents caching problems such as above.

Upvotes: 2

Related Questions