Re: Triangles and trigonometry

• To: mathgroup at smc.vnet.net
• Subject: [mg15036] Re: [mg15000] Triangles and trigonometry
• From: BobHanlon at aol.com
• Date: Wed, 9 Dec 1998 04:12:21 -0500
• Sender: owner-wri-mathgroup at wolfram.com

```In a message dated 12/5/98 4:56:25 AM, gabrielle at XXXpsy.uwa.edu.au
writes:

>Does anyone know of a package suitable for solving triginometry
>problems such as
>
>(Upper case are angles, lower case is opposite side)
>
>A=12 Degree
>a=5
>b=17
>C<90 Degree
>
>I've written something (which works) using repeated applications of
>Solve with the Sin and Cos rules which does the job very badly(!).
>However, it doesn't cope with the _generic_ case of _any_ mixture of
>solution in several steps to get it to work properly.
>

Mark,

I haven't tested this notebook very extensively; however, I think it
works.

Bob Hanlon

___________________________

Notebook[{
Cell["Off[General::spell1, Solve::ifun];", "Input"],

Cell[CellGroupData[{

Cell["\<\
soln = Solve[{a == 3, b == 4, c == 5,  \ta^2 == b^2 + c^2 -
2*b*c*Cos[A],
\tb^2 == a^2 + c^2 - 2*a*c*Cos[B],
\tA + B + C == Pi}, {a, b, c, A, B, C}]\ \>", "Input"],

Cell[BoxData[
\({{C \[Rule] \[Pi] - ArcCos[3\/5] - ArcCos[4\/5], a \[Rule] 3,
b \[Rule] 4, c \[Rule] 5, B \[Rule] ArcCos[3\/5],
A \[Rule] ArcCos[4\/5]}}\)], "Output"] }, Open  ]],

Cell[CellGroupData[{

Cell["\<\
soln = Solve[{A == 12. Degree, a == 5., b == 17.,  \ta^2 == b^2 + c^2 -
2*b*c*Cos[A],
\tb^2 == a^2 + c^2 - 2*a*c*Cos[B],
\tA + B + C == Pi}, {a, b, c, A, B, C}]; soln // ColumnForm\
\>", "Input"],

Cell[BoxData[
InterpretationBox[GridBox[{
{
\({C \[Rule] 0.575665907967170209`, a \[Rule] 5.`,
b \[Rule] 17.`, c \[Rule] 13.0919404475054879`,
B \[Rule] 2.35648723538330351`, A \[Rule]
0.20943951023931957`}
\)},
{
\({C \[Rule] 2.14704772514398412`, a \[Rule] 5.`, b \[Rule]
17.`,
c \[Rule] 20.1650779774439037`,
B \[Rule] 0.785105418206489691`, A \[Rule]
0.20943951023931957`}
\)},
{
\({C \[Rule] 3.71725856155696332`, a \[Rule] 5.`, b \[Rule]
17.`,
c \[Rule] 20.1650779774439037`,
B \[Rule] \(-0.785105418206489691`\),
A \[Rule] 0.20943951023931957`}\)},
{
\({C \[Rule] 5.28864037873377679`, a \[Rule] 5.`, b \[Rule]
17.`,
c \[Rule] 13.0919404475054879`,
B \[Rule] \(-2.35648723538330351`\),
A \[Rule] 0.20943951023931957`}\)}
},
GridBaseline->{Baseline, {1, 1}},
ColumnAlignments->{Left}],
ColumnForm[ {{C -> 0.57566590796717021, a -> 5.0, b -> 17.0, c ->
13.091940447505488, B -> 2.3564872353833035, A ->
0.20943951023931956}, {C -> 2.1470477251439841, a -> 5.0, b ->
17.0,
c -> 20.165077977443904, B -> 0.78510541820648971, A ->
0.20943951023931956}, {C -> 3.7172585615569633, a -> 5.0, b ->
17.0,
c -> 20.165077977443904, B -> -0.78510541820648971, A ->
0.20943951023931956}, {C -> 5.2886403787337768, a -> 5.0, b ->
17.0,
c -> 13.091940447505488, B -> -2.3564872353833035, A ->
0.20943951023931956}}],
Editable->False]], "Output"],

Cell["\<\
Note that the last two solutions include negative angles.  \ Excluding
solutions with any negative values:\ \>", "Text"],

Cell[CellGroupData[{

Cell["\<\
Select[soln, Min[({a, b, c, A, B, C} /. #)] > 0&] // \ ColumnForm\
\>", "Input"],

Cell[BoxData[
InterpretationBox[GridBox[{
{
\({C \[Rule] 0.575665907967170209`, a \[Rule] 5.`,
b \[Rule] 17.`, c \[Rule] 13.0919404475054879`,
B \[Rule] 2.35648723538330351`, A \[Rule]
0.20943951023931957`}
\)},
{
\({C \[Rule] 2.14704772514398412`, a \[Rule] 5.`, b \[Rule]
17.`,
c \[Rule] 20.1650779774439037`,
B \[Rule] 0.785105418206489691`, A \[Rule]
0.20943951023931957`}
\)}
},
GridBaseline->{Baseline, {1, 1}},
ColumnAlignments->{Left}],
ColumnForm[ {{C -> 0.57566590796717021, a -> 5.0, b -> 17.0, c ->
13.091940447505488, B -> 2.3564872353833035, A ->
0.20943951023931956}, {C -> 2.1470477251439841, a -> 5.0, b ->
17.0,
c -> 20.165077977443904, B -> 0.78510541820648971, A ->
0.20943951023931956}}],
Editable->False]], "Output"]
}, Open  ]]
}, Open  ]],

Cell["\<\
It is the same problem if we interchange a and b and A and B; \
however,\
\>", "Text"],

Cell[CellGroupData[{

Cell["\<\
Solve[{B == 12. Degree, b == 5., a == 17.,  \ta^2 == b^2 + c^2 -
2*b*c*Cos[A],
\tb^2 == a^2 + c^2 - 2*a*c*Cos[B],
\tA + B + C == Pi}, {a, b, c, A, B, C}]// \tColumnForm\
\>", "Input"],

Cell[BoxData[
\(Solve::"incnst" \( : \ \)
"Inconsistent or redundant transcendental equation. After
reduction, \ the bad equation is \!\(\(-69696\) - \(6600\\
\(Cos[A]\)\^2\) + \(76296\\ \ \(Cos[B]\)\^2\)\) == 0."\)], "Message"],

Cell[BoxData[
InterpretationBox[GridBox[{
{
RowBox[{"{",
RowBox[{\(B == 0.20943951023931957`\), ",",
RowBox[{"b", "==",
StyleBox["5.`",
StyleBoxAutoDelete->True,
PrintPrecision->1]}], ",",
RowBox[{"a", "==",
StyleBox["17.`",
StyleBoxAutoDelete->True,
PrintPrecision->2]}], ",",
\(a\^2 == b\^2 + c\^2 - 2\ b\ c\ Cos[A]\), ",",
\(b\^2 == a\^2 + c\^2 - 2\ a\ c\ Cos[B]\), ",",
\(A + B + C == \[Pi]\)}], "}"}]},
{\({a, b, c, A, B, C}\)}
},
GridBaseline->{Baseline, {1, 1}},
ColumnAlignments->{Left}],
ColumnForm[
Solve[ {
Equal[ B, 0.20943951023931956],
Equal[ b, 5.0],
Equal[ a, 17.0],
Equal[
Power[ a, 2],
Plus[
Power[ b, 2],
Power[ c, 2],
Times[ -2, b, c,
Cos[ A]]]],
Equal[
Power[ b, 2],
Plus[
Power[ a, 2],
Power[ c, 2],
Times[ -2, a, c,
Cos[ B]]]],
Equal[
Plus[ A, B, C], Pi]}, {a, b, c, A, B, C}]],
Editable->False]], "Output"]
}, Open  ]],

Cell[TextData[{
"It appears that the canonical order of the variables affects whether
",
StyleBox["Mathematica",
FontSlant->"Italic"],
" can solve the equations.  One approach is to use dummy variables
which \ force the required canonical order." }], "Text"],

Cell["\<\
pythagorum::usage = \"pythagorum[valuesList] solves the Pythagorum
\ttheorem given (in the form of equations) either two sides and \tan
angle, two angles and a side, or three sides.  \tpythagorum[valuesList,
condition] adds a condition on one of \tthe variables.
pythagorum[valuesList, condition, variableList] \talso specifies the
variable names for the sides and \tcorresponding opposite angles.\";

pythagorum[values_List, cond_:True, var_List:{a,b,c,A,B,C}] :=
\tModule[{eqns, soln, i, given, ord,  \tva, vb, vc, vA, vB, vC, vVar,
ordVar}, \tvVar = {va, vb, vc, vA, vB, vC};
\tgiven = Sort[Table[values[[i, 1]], {i, 3}],  \t\tPosition[var,
#1][[1,1]] > 3&];
\tord = Join[{i = Mod[Position[var, given[[1]]][[1,1]] - 1, 3] + 1},
\t\tComplement[{1,2,3}, {i}]];
\tordVar = {var[[ord[[1]]]], var[[ord[[2]]]], var[[ord[[3]]]],
\t\tvar[[ord[[1]]+3]], var[[ord[[2]]+3]], var[[ord[[3]]+3]]}; \teqns =
{va^2 == vb^2 + vc^2 - 2*vb*vc*Cos[vA],  \t\tvb^2 == va^2 + vc^2 -
2*va*vc*Cos[vB],
\t\tvA + vB + vC == Pi};
\tsoln = Chop[Solve[Join[
\t\tvalues /. Inner[Rule, ordVar, vVar, List], eqns], vVar]]; \tsoln =
Select[soln, Min[(vVar /. #)] > 0&]; \tsoln = soln /. Inner[Rule, vVar,
ordVar, List]; \tSort /@ Select[soln, (cond /. #)&]]\ \>", "Input"],

Cell[CellGroupData[{

Cell["pythagorum[{A == 12. Degree, a == 5., b == 17.}]//ColumnForm",
"Input"],

Cell[BoxData[
InterpretationBox[GridBox[{
{
\({a \[Rule] 5.`, A \[Rule] 0.20943951023931957`, b \[Rule]
17.`,
B \[Rule] 2.35648723538330351`, c \[Rule]
13.0919404475054879`,
C \[Rule] 0.575665907967170209`}\)},
{
\({a \[Rule] 5.`, A \[Rule] 0.20943951023931957`, b \[Rule]
17.`,
B \[Rule] 0.785105418206489691`,
c \[Rule] 20.1650779774439037`, C \[Rule]
2.14704772514398412`}
\)}
},
GridBaseline->{Baseline, {1, 1}},
ColumnAlignments->{Left}],
ColumnForm[ {{a -> 5.0, A -> 0.20943951023931956, b -> 17.0, B ->
2.3564872353833035, c -> 13.091940447505488, C ->
0.57566590796717021}, {a -> 5.0, A -> 0.20943951023931956, b ->
17.0,
B -> 0.78510541820648971, c -> 20.165077977443904, C ->
2.1470477251439841}}],
Editable->False]], "Output"]
}, Open  ]],

Cell["Now when the variables are interchanged", "Text"],

Cell[CellGroupData[{

Cell["pythagorum[{B == 12. Degree, b == 5., a == 17.}]//ColumnForm",
"Input"],

Cell[BoxData[
InterpretationBox[GridBox[{
{
\({a \[Rule] 17.`, A \[Rule] 2.35648723538330351`, b \[Rule]
5.`,
B \[Rule] 0.20943951023931957`, c \[Rule]
13.0919404475054879`,
C \[Rule] 0.575665907967170209`}\)},
{
\({a \[Rule] 17.`, A \[Rule] 0.785105418206489691`,
b \[Rule] 5.`, B \[Rule] 0.20943951023931957`,
c \[Rule] 20.1650779774439037`, C \[Rule]
2.14704772514398412`}
\)}
},
GridBaseline->{Baseline, {1, 1}},
ColumnAlignments->{Left}],
ColumnForm[ {{a -> 17.0, A -> 2.3564872353833035, b -> 5.0, B ->
0.20943951023931956, c -> 13.091940447505488, C ->
0.57566590796717021}, {a -> 17.0, A -> 0.78510541820648971, b ->
5.0,
B -> 0.20943951023931956, c -> 20.165077977443904, C ->
2.1470477251439841}}],
Editable->False]], "Output"]
}, Open  ]],

Cell["\<\
If there is more than one solution, a condition can be set to \ select
the desired solution:\
\>", "Text"],

Cell[CellGroupData[{

Cell["\<\
pythagorum[{A == 12. Degree, a == 5., b == 17.}, C <= 90 \ Degree]\
\>", "Input"],

Cell[BoxData[
\({{a \[Rule] 5.`, A \[Rule] 0.20943951023931957`, b \[Rule] 17.`,
B \[Rule] 2.35648723538330351`, c \[Rule] 13.0919404475054879`,
C \[Rule] 0.575665907967170209`}}\)], "Output"] }, Open  ]],

Cell["Exact solutions can also be found:", "Text"],

Cell[CellGroupData[{

Cell["\<\
pythagorum[{A == 12 Degree, a == 5, b == 17}, C <= 90 Degree]\ \>",
"Input"],

Cell[BoxData[
\({{a \[Rule] 5, A \[Rule] 12\ Degree, b \[Rule] 17,
B \[Rule]
ArcCos[\(-\(1\/5\)\)\ \ at \(\(-264\) + 289\ Cos[12\
Degree]\^2\)],
c \[Rule]
17\ Cos[12\ Degree] - \ at \(\(-264\) + 289\ Cos[12\
Degree]\^2\),
C \[Rule]
\(-12\)\ Degree + \[Pi] -
ArcCos[\(-\(1\/5\)\)\ \ at \(\(-264\) + 289\ Cos[12\
Degree]\^2\)]}}
\)], "Output"]
}, Open  ]],

Cell["\<\
Any names for the variables can be specified provided they are \ given
with the sides first and then the corresponding angles.\ \>", "Text"],

Cell[CellGroupData[{

Cell["\<\
pythagorum[{angleB == 12. Degree, sideA == 5., sideB == 17.}, True, \

\t{sideA, sideB, sideC, angleA, angleB, angleC}]\ \>", "Input"],

Cell[BoxData[
\({{angleA \[Rule] 0.0611886724307138063`,
angleB \[Rule] 0.20943951023931957`,
angleC \[Rule] 2.87096447091975992`, sideA \[Rule] 5.`,
sideB \[Rule] 17.`, sideC \[Rule] 21.8589234759147998`}}\)],
"Output"]
}, Open  ]],

Cell["\<\
You can also specify two angles and one side.  However, the \ algorithm
in this case is considerably slower.\ \>", "Text"],

Cell[CellGroupData[{

Cell["pythagorum[{A == 12. Degree, B == 5. Degree, a == 17.}]",
"Input"],

Cell[BoxData[
\({{a \[Rule] 17.`, A \[Rule] 0.20943951023931957`,
b \[Rule] 7.12633147699662661`, B \[Rule]
0.0872664625997164833`,
c \[Rule] 23.9059139038177237`, C \[Rule]
2.84488668075075743`}}\)],
"Output"]
}, Open  ]],

Cell["Specifying three sides", "Text"],

Cell[CellGroupData[{

Cell["pythagorum[{a == 3, b == 4, c == 5}]", "Input"],

Cell[BoxData[
\({{a \[Rule] 3, A \[Rule] ArcCos[4\/5], b \[Rule] 4,
B \[Rule] \[Pi]\/2 - ArcCos[4\/5], c \[Rule] 5, C \[Rule]
\[Pi]\/2}}
\)], "Output"]
}, Open  ]],

Cell[CellGroupData[{

Cell["pythagorum[{a == 3.5, b == 4.7, c == 5.2}]", "Input"],

Cell[BoxData[
\({{a \[Rule] 3.5`, A \[Rule] 0.715903141062576331`,
b \[Rule] 4.70000000000000017`, B \[Rule] 1.07863990696806744`,
c \[Rule] 5.20000000000000017`, C \[Rule]
1.34704960555914965`}}\)],
"Output"]
}, Open  ]]
},
FrontEndVersion->"Macintosh 3.0",
ScreenRectangle->{{0, 1024}, {0, 748}}, CellGrouping->Manual,
WindowSize->{956, 721},
WindowMargins->{{4, Automatic}, {Automatic, 1}}, ShowCellLabel->False,
MacintoshSystemPageSetup->"\<\
00<0001804P000000]P2:?oQon82n at 960dL5:0?l0080001804P000000]P2:001
0000I00000400`<300000BL?00400 at 0000000000000006P801T1T00000000000
00000000000000000000000000000000\>"
]

```

• Prev by Date: Re: Question on Nest[]
• Next by Date: Re: nice Gotcha!
• Previous by thread: Re: Triangles and trigonometry