2 Variables and 2 Unknowns Returning Incorrect Results

1 visualización (últimos 30 días)
Erik Toft
Erik Toft el 9 de Mzo. de 2021
Editada: John D'Errico el 9 de Mzo. de 2021
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.

Respuesta aceptada

Walter Roberson
Walter Roberson el 9 de Mzo. de 2021
fprintf('solution is %g \n', solVA2x)
The %i format is only for integers.

Más respuestas (1)

John D'Errico
John D'Errico el 9 de Mzo. de 2021
Editada: John D'Errico el 9 de Mzo. de 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
ans = 
sol.VB2x
ans = 
format long g
[double(sol.VA2x),double(sol.VB2x)]
ans = 1×2
0.802905222344044 0.85024535842434
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))
ans = 
symtrue
simplify(subs(equ2,sol))
ans = 
symtrue
I could also have solved it as:
[A,b] = equationsToMatrix(equ1,equ2,[VA2x,VB2x])
A = 
b = 
As we see there, the problem is clearly non-singular,, but we can verify that as:
rank(A)
ans =
2
And now the solution is simple.
sol2 = double(A\b)
sol2 = 2×1
0.802905222344044 0.85024535842434

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by