I have two triangles sharing side c. Knowns are angles alpha,alpha' and side a and a'. I am looking for b and b'. From the set of 3 cosine rules I can find e.g. b' as shown in the code below. Unfortunately there are 2 solutions after every solve, from which I pick the correct one manually and in the end I have to solve numerically for the solution, which is correct.
Can I somehow deduce which solution after solving is the correct one or put some constraints on solve? Is there a closed solution to my problem?
from mpmath import radians
from sympy import N, symbols, cos, init_printing,Eq,solve,simplify,nsolve
init_printing()
alphaPrime, alpha, a, aPrime, b, bPrime,c = symbols('Ξ±prime Ξ± a aprime b bprime,c')
eq1=Eq( a**2 , b**2+c**2-2*b*c*cos(alpha))
eq2=Eq( aPrime**2 , bPrime**2+c**2-2*bPrime*c*cos(alphaPrime))
eq3=Eq( (a+aPrime)**2 , bPrime**2+b**2-2*b*bPrime*cos(alpha+alphaPrime))
eq1c=solve(eq1,c,simplify=False)
eq2c=solve(eq2,c,simplify=False)
Eq1MinusEq2=eq1c[1]-eq2c[0]
eq1_eq2_b=solve(Eq1MinusEq2,b,simplify=False)
eq3_b=solve(eq3,b,simplify='False')
eq_1n2_Minus_eq3=eq1_eq2_b[1]-eq3_b[1]
display(simplify(eq_1n2_Minus_eq3))
expr=eq_1n2_Minus_eq3.subs([(aPrime,1.6547),(a,1.1),(alphaPrime,radians(27.031)),
(alpha,radians(25.268))])
display(nsolve(expr,bPrime,1)) ##works
gives me: 3.45613973389693+1.86640158050948β 10β8π, which's real part is correct.
These are your equations:
In [156]: eq1
Out[156]:
2 2 2
a = b - 2β
bβ
cβ
cos(Ξ±) + c
In [157]: eq2
Out[157]:
2 2 2
aβ² = bβ² - 2β
bβ²β
cβ
cos(Ξ±β²) + c
In [158]: eq3
Out[158]:
2 2 2
(a + aβ²) = b - 2β
bβ
bβ²β
cos(Ξ± + Ξ±β²) + bβ²
We want to eliminate c
and then solve for a
and a'
so let's eliminate c
first:
In [159]: eq12 = resultant(eq1, eq2, c)
In [160]: eq12
Out[160]:
4 2 2 2 2 2 2 2 2 2 2 4 2 2 βͺ
a - 2β
a β
aβ² - 2β
a β
b + 4β
a β
bβ
bβ²β
cos(Ξ±)β
cos(Ξ±β²) - 4β
a β
bβ² β
cos (Ξ±β²) + 2β
a β
bβ² + aβ² - 4β
aβ² β
b β
βͺ
βͺ 2 2 2 2 2 2 4 3 2 βͺ
βͺ cos (Ξ±) + 2β
aβ² β
b + 4β
aβ² β
bβ
bβ²β
cos(Ξ±)β
cos(Ξ±β²) - 2β
aβ² β
bβ² + b - 4β
b β
bβ²β
cos(Ξ±)β
cos(Ξ±β²) + 4β
b β
b βͺ
βͺ 2 2 2 2 2 2 2 3 4
βͺ β² β
cos (Ξ±) + 4β
b β
bβ² β
cos (Ξ±β²) - 2β
b β
bβ² - 4β
bβ
bβ² β
cos(Ξ±)β
cos(Ξ±β²) + bβ²
Now eq12
and eq3
are two equations for the two unknowns a
and a'
.
We can eliminate say a'
to find an equation only for a
. This equation is a quartic with two double roots that we can simplify down to nice expressions:
In [167]: eq_a = resultant(eq12, eq3, aPrime)
In [178]: r1, r2 = roots(eq_a, a)
In [179]: r2.expand(trig=True).simplify()
Out[179]:
____________________________________________
β± 2 β 2 2β 2
β± b β
βb - 2β
bβ
bβ²β
cos(Ξ± + Ξ±β²) + bβ² β β
sin (Ξ±)
β± ββββββββββββββββββββββββββββββββββββββββββ
β± 2
β²β± (bβ
sin(Ξ±) + bβ²β
sin(Ξ±β²))
In [180]: r1.expand(trig=True).simplify()
Out[180]:
____________________________________________
β± 2 β 2 2β 2
β± b β
βb - 2β
bβ
bβ²β
cos(Ξ± + Ξ±β²) + bβ² β β
sin (Ξ±)
- β± ββββββββββββββββββββββββββββββββββββββββββ
β± 2
β²β± (bβ
sin(Ξ±) + bβ²β
sin(Ξ±β²))
These are the two solutions for a
. Corresponding solutions for a'
can be found similarly and are the same apart from interchanging primes which only affects the final sin(Ξ±)
part of the numerator.
The multiple solutions are to do with the equations being satisfied by negative angles and lengths. If we want to require everything to be positive then the formulae above can simplify because most factors under the square root are squares of positive quantities.
The key technique used above is polynomial resultants:
https://en.wikipedia.org/wiki/Resultant https://docs.sympy.org/latest/modules/polys/reference.html#sympy.polys.polytools.resultant
EDIT: I see that b and b' are the unknowns. Okay so adapted:
In [59]: eq_b = resultant(eq12, eq3, bPrime)
In [60]: from sympy.simplify.fu import TR5
In [61]: eq_b2 = TR5((eq_b).expand(trig=True)).expand()
In [64]: r1, r2 = roots(eq_b2, b)
In [74]: r1
Out[74]:
___________________________________________________________________________________________________________________________________________________________________________________________________________________________________
β± 2 2 β 2 2 2 2 β 2
β± a β
(a + aβ²) β
βa β
sin (Ξ±β²) - 2β
aβ
aβ²β
sin(Ξ±)β
sin(Ξ±β²)β
cos(Ξ± + Ξ±β²) + aβ² β
sin (Ξ±)β β
sin (Ξ±β²)
- β± βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β± 4 4 3 3 2 2 4 2 2 2 3 3 2 2 2 4 2 2 2 2 3 3 4 4
β²β± a β
sin (Ξ±β²) - 4β
a β
aβ²β
sin(Ξ±)β
sin (Ξ±β²)β
cos(Ξ± + Ξ±β²) - 4β
a β
aβ² β
sin (Ξ±)β
sin (Ξ±β²) - 8β
a β
aβ² β
sin (Ξ±)β
sin (Ξ±β²)β
cos(Ξ± + Ξ±β²) - 4β
a β
aβ² β
sin (Ξ±)β
sin (Ξ±β²) + 6β
a β
aβ² β
sin (Ξ±)β
sin (Ξ±β²) - 4β
aβ
aβ² β
sin (Ξ±)β
sin(Ξ±β²)β
cos(Ξ± + Ξ±β²) + aβ² β
sin (Ξ±)
In [75]: r2
Out[75]:
___________________________________________________________________________________________________________________________________________________________________________________________________________________________________
β± 2 2 β 2 2 2 2 β 2
β± a β
(a + aβ²) β
βa β
sin (Ξ±β²) - 2β
aβ
aβ²β
sin(Ξ±)β
sin(Ξ±β²)β
cos(Ξ± + Ξ±β²) + aβ² β
sin (Ξ±)β β
sin (Ξ±β²)
β± βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β± 4 4 3 3 2 2 4 2 2 2 3 3 2 2 2 4 2 2 2 2 3 3 4 4
β²β± a β
sin (Ξ±β²) - 4β
a β
aβ²β
sin(Ξ±)β
sin (Ξ±β²)β
cos(Ξ± + Ξ±β²) - 4β
a β
aβ² β
sin (Ξ±)β
sin (Ξ±β²) - 8β
a β
aβ² β
sin (Ξ±)β
sin (Ξ±β²)β
cos(Ξ± + Ξ±β²) - 4β
a β
aβ² β
sin (Ξ±)β
sin (Ξ±β²) + 6β
a β
aβ² β
sin (Ξ±)β
sin (Ξ±β²) - 4β
aβ
aβ² β
sin (Ξ±)β
sin(Ξ±β²)β
cos(Ξ± + Ξ±β²) + aβ² β
sin (Ξ±)
There might be ways to simplify those.