2 Variables and 2 Unknowns Returning Incorrect Results
Show older comments
I am trying to solve a problem involving collisions, oblique collisions to be exact.
First attempt:
VA1=2
VB1=3
m1=0.5
m2=0.4
ThetaA1=30
ThetaB1=50
VA1x = m1*VA1*cosd(ThetaA1);
VB1x = m2*VB1*cosd(ThetaB1);
syms VA2x VB2x
equ1 = m1*VA1x + m2*VB1x == m1*VA2x + m2*VB2x;
equ2 = -0.5*(VB1x - VA1x) == (VB2x - VA2x);
sol = solve ([equ1,equ2],[VA2x,VB2x]);
solVA2x = sol.VA2x;
fprintf('solution is %i \n', solVA2x)
For my second attempt:
VA1=2
VB1=3
m1=0.5
m2=0.4
ThetaA1=30
ThetaB1=50
VA1x = m1*VA1*cosd(ThetaA1);
VB1x = m2*VB1*cosd(ThetaB1);
syms VA2x VB2x
sol=solve([m1*VA1x + m2*VB1x == m1*VA2x + m2*VB2x , 0.5 == -(VB2x - VA2x)/(VB1x - VA1x)],[VA2x,VB2x]);
C=sol.VA2x;
E=sol.VB2x;
fprintf('Value for VA2x %i \n',C);
fprintf('Value for VB2x %i \n',E);
I am pretty unexperienced and willing to learn so any additional references would be appreciated. I have read that I should add vpasolve however I have already tried that and the results are the same.
1 Comment
Accepted Answer
More Answers (1)
John D'Errico
on 9 Mar 2021
Edited: John D'Errico
on 9 Mar 2021
This is a linear system of two equations in two unknowns. The solution is unique, since the sysem is full rank. If we look at your first attempt, we see:
VA1=2;
VB1=3;
m1=0.5;
m2=0.4;
ThetaA1=30;
ThetaB1=50;
VA1x = m1*VA1*cosd(ThetaA1);
VB1x = m2*VB1*cosd(ThetaB1);
syms VA2x VB2x
equ1 = m1*VA1x + m2*VB1x == m1*VA2x + m2*VB2x;
equ2 = -0.5*(VB1x - VA1x) == (VB2x - VA2x);
sol = solve ([equ1,equ2],[VA2x,VB2x]);
sol.VA2x
sol.VB2x
format long g
[double(sol.VA2x),double(sol.VB2x)]
Your second attempt is no different, you did nothing significant but manipulate one of the equations.
Is this a solution? We can verify that.
simplify(subs(equ1,sol))
simplify(subs(equ2,sol))
I could also have solved it as:
[A,b] = equationsToMatrix(equ1,equ2,[VA2x,VB2x])
As we see there, the problem is clearly non-singular,, but we can verify that as:
rank(A)
And now the solution is simple.
sol2 = double(A\b)
Categories
Find more on Code Performance 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!
