(************** Content-type: application/mathematica ************** CreatedBy='Mathematica 5.2' Mathematica-Compatible Notebook This notebook can be used with any Mathematica-compatible application, such as Mathematica, MathReader or Publicon. The data for the notebook starts with the line containing stars above. To get the notebook into a Mathematica-compatible application, do one of the following: * Save the data starting with the line of stars above into a file with a name ending in .nb, then open the file inside the application; * Copy the data starting with the line of stars above to the clipboard, then use the Paste menu command inside the application. Data for notebooks contains only printable 7-bit ASCII and can be sent directly in email or through ftp in text mode. Newlines can be CR, LF or CRLF (Unix, Macintosh or MS-DOS style). NOTE: If you modify the data for this notebook not in a Mathematica- compatible application, you must delete the line below containing the word CacheID, otherwise Mathematica-compatible applications may try to use invalid cache data. For more information on notebooks and Mathematica-compatible applications, contact Wolfram Research: web: http://www.wolfram.com email: info@wolfram.com phone: +1-217-398-0700 (U.S.) Notebook reader applications are available free of charge from Wolfram Research. *******************************************************************) (*CacheID: 232*) (*NotebookFileLineBreakTest NotebookFileLineBreakTest*) (*NotebookOptionsPosition[ 14543, 441]*) (*NotebookOutlinePosition[ 15234, 465]*) (* CellTagsIndexPosition[ 15190, 461]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["\<\ First define each of the axis-aligned rotation matrices as a \ function of some angle:\ \>", "Subsection"], Cell[BoxData[{ RowBox[{ RowBox[{\(rx[\[Theta]_]\), ":=", RowBox[{"(", GridBox[{ {"1", "0", "0"}, {"0", \(Cos[\[Theta]]\), \(-Sin[\[Theta]]\)}, {"0", \(Sin[\[Theta]]\), \(Cos[\[Theta]]\)} }], ")"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{\(ry[\[Theta]_]\), ":=", RowBox[{"(", GridBox[{ {\(Cos[\[Theta]]\), "0", \(Sin[\[Theta]]\)}, {"0", "1", "0"}, {\(-Sin[\[Theta]]\), "0", \(Cos[\[Theta]]\)} }], ")"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{\(rz[\[Theta]_]\), ":=", RowBox[{"(", GridBox[{ {\(Cos[\[Theta]]\), \(-Sin[\[Theta]]\), "0"}, {\(Sin[\[Theta]]\), \(Cos[\[Theta]]\), "0"}, {"0", "0", "1"} }], ")"}]}], ";"}]}], "Input"] }, Open ]], Cell[CellGroupData[{ Cell[TextData[StyleBox["Now, given Euler angles and assuming the rotation \ order X, Y, Z with fixed axes, we can convert from Euler angles to a matrix \ with:", FontFamily->"Helvetica"]], "Subsection"], Cell[BoxData[ \(eulertomatrix[\[Theta]x_, \[Theta]y_, \[Theta]z_] := rz[\[Theta]z] . ry[\[Theta]y] . rx[\[Theta]x]\)], "Input"] }, Open ]], Cell[CellGroupData[{ Cell[TextData[{ "We can now ask ", StyleBox["Mathematica", FontSlant->"Italic"], " to write out the matrix for an arbitrary set of input angles:" }], "Subsection"], Cell[CellGroupData[{ Cell[BoxData[ \(MatrixForm[eulertomatrix[\[Theta]x, \[Theta]y, \[Theta]z]]\)], "Input"], Cell[BoxData[ TagBox[ RowBox[{"(", "\[NoBreak]", GridBox[{ {\(Cos[\[Theta]y]\ Cos[\[Theta]z]\), \(Cos[\[Theta]z]\ Sin[\ \[Theta]x]\ Sin[\[Theta]y] - Cos[\[Theta]x]\ Sin[\[Theta]z]\), \(Cos[\[Theta]x]\ Cos[\ \[Theta]z]\ Sin[\[Theta]y] + Sin[\[Theta]x]\ Sin[\[Theta]z]\)}, {\(Cos[\[Theta]y]\ Sin[\[Theta]z]\), \(Cos[\[Theta]x]\ Cos[\ \[Theta]z] + Sin[\[Theta]x]\ Sin[\[Theta]y]\ Sin[\[Theta]z]\), \(\(-Cos[\ \[Theta]z]\)\ Sin[\[Theta]x] + Cos[\[Theta]x]\ Sin[\[Theta]y]\ Sin[\[Theta]z]\)}, {\(-Sin[\[Theta]y]\), \(Cos[\[Theta]y]\ Sin[\[Theta]x]\), \(Cos[\ \[Theta]x]\ Cos[\[Theta]y]\)} }, RowSpacings->1, ColumnSpacings->1, ColumnAlignments->{Left}], "\[NoBreak]", ")"}], Function[ BoxForm`e$, MatrixForm[ BoxForm`e$]]]], "Output"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["\<\ Using the notation Cx to mean Cos[\[Theta]x], Sz for Sin[\[Theta]z] \ and so forth, we can makethe above parameterized matrix look a little \ cleaner:\ \>", "Subsection"], Cell[CellGroupData[{ Cell[BoxData[ \(MatrixForm[ eulertomatrix[\[Theta]x, \[Theta]y, \[Theta]z] /. {Cos[\[Theta]x] \ \[Rule] Cx, Cos[\[Theta]y] \[Rule] Cy, Cos[\[Theta]z] \[Rule] Cz, \ Sin[\[Theta]x] \[Rule] Sx, Sin[\[Theta]y] \[Rule] Sy, Sin[\[Theta]z] \[Rule] Sz}]\)], "Input"], Cell[BoxData[ TagBox[ RowBox[{"(", "\[NoBreak]", GridBox[{ {\(Cy\ Cz\), \(Cz\ Sx\ Sy - Cx\ Sz\), \(Cx\ Cz\ Sy + Sx\ Sz\)}, {\(Cy\ Sz\), \(Cx\ Cz + Sx\ Sy\ Sz\), \(\(-Cz\)\ Sx + Cx\ Sy\ Sz\)}, {\(-Sy\), \(Cy\ Sx\), \(Cx\ Cy\)} }, RowSpacings->1, ColumnSpacings->1, ColumnAlignments->{Left}], "\[NoBreak]", ")"}], Function[ BoxForm`e$, MatrixForm[ BoxForm`e$]]]], "Output"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["\<\ Also, show what the matrix looks like for \[Theta]y= \[PlusMinus]90\ \[Degree]\ \>", "Subsection"], Cell["\<\ Note that this also shows how gimbal lock kills one degree of \ freedom.\ \>", "Text"], Cell[CellGroupData[{ Cell[BoxData[{ \(MatrixForm[ FullSimplify[ eulertomatrix[\[Theta]x, \[Theta]y, \[Theta]z] /. {Cos[\[Theta]y] \ \[Rule] 0, Sin[\[Theta]y] \[Rule] 1}]]\), "\[IndentingNewLine]", \(MatrixForm[ FullSimplify[ eulertomatrix[\[Theta]x, \[Theta]y, \[Theta]z] /. {Cos[\[Theta]y] \ \[Rule] 0, Sin[\[Theta]y] \[Rule] \(-1\)}]]\)}], "Input"], Cell[BoxData[ TagBox[ RowBox[{"(", "\[NoBreak]", GridBox[{ { "0", \(Sin[\[Theta]x - \[Theta]z]\), \(Cos[\[Theta]x - \ \[Theta]z]\)}, { "0", \(Cos[\[Theta]x - \[Theta]z]\), \(-Sin[\[Theta]x - \ \[Theta]z]\)}, {\(-1\), "0", "0"} }, RowSpacings->1, ColumnSpacings->1, ColumnAlignments->{Left}], "\[NoBreak]", ")"}], Function[ BoxForm`e$, MatrixForm[ BoxForm`e$]]]], "Output"], Cell[BoxData[ TagBox[ RowBox[{"(", "\[NoBreak]", GridBox[{ { "0", \(-Sin[\[Theta]x + \[Theta]z]\), \(-Cos[\[Theta]x + \ \[Theta]z]\)}, { "0", \(Cos[\[Theta]x + \[Theta]z]\), \(-Sin[\[Theta]x + \ \[Theta]z]\)}, {"1", "0", "0"} }, RowSpacings->1, ColumnSpacings->1, ColumnAlignments->{Left}], "\[NoBreak]", ")"}], Function[ BoxForm`e$, MatrixForm[ BoxForm`e$]]]], "Output"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["\<\ Now we can use the above to work out formulae for the Euler angles \ if we have the matrix:\ \>", "Subsection"], Cell["\<\ Note that I think ArcTan takes arguments in reverse order from the \ C function atan2.\ \>", "Text"], Cell[BoxData[ \(matrixtoeuler[m_] := Block[{x, y, z, cy, \[Epsilon] = 0.000001}, \[IndentingNewLine]y = ArcSin[\(-m\[LeftDoubleBracket]3, 1\[RightDoubleBracket]\)]; \[IndentingNewLine]If[\((Abs[ m\[LeftDoubleBracket]3, 1\[RightDoubleBracket]] - 1)\) < \(-\[Epsilon]\), \[IndentingNewLine]z = ArcTan[m\[LeftDoubleBracket]1, 1\[RightDoubleBracket], m\[LeftDoubleBracket]2, 1\[RightDoubleBracket]]; \[IndentingNewLine]x = ArcTan[m\[LeftDoubleBracket]3, 3\[RightDoubleBracket], m\[LeftDoubleBracket]3, 2\[RightDoubleBracket]];\[IndentingNewLine], \ \[IndentingNewLine]x = ArcTan[\(-m\[LeftDoubleBracket]1, 3\[RightDoubleBracket]\)/ m\[LeftDoubleBracket]3, 1\[RightDoubleBracket], \(-m\[LeftDoubleBracket]1, 2\[RightDoubleBracket]\)/ m\[LeftDoubleBracket]3, 1\[RightDoubleBracket]]; \[IndentingNewLine]z = 0;\[IndentingNewLine]]; \[IndentingNewLine]{x, y, z}\[IndentingNewLine]]\)], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Test for some random value", "Subsection"], Cell[CellGroupData[{ Cell[BoxData[{ \(\(tm = eulertomatrix[ .4, .9, \(-1.2\)];\)\), "\[IndentingNewLine]", \(MatrixForm[tm]\), "\[IndentingNewLine]", \({tx, ty, tz} = matrixtoeuler[tm]\), "\[IndentingNewLine]", \(MatrixForm[tmm = eulertomatrix[tx, ty, tz]]\), "\[IndentingNewLine]", \(Norm[tm - tmm, \[Infinity]]\), "\[IndentingNewLine]", \(\)}], "Input"], Cell[BoxData[ TagBox[ RowBox[{"(", "\[NoBreak]", GridBox[{ {"0.2252451922628743`", "0.968999132784982`", \(-0.10151494483283646`\)}, {\(-0.5793647866551066`\), "0.049442650964680535`", \(-0.8135673716722344`\)}, {\(-0.7833269096274834`\), "0.24206632340649498`", "0.57254069525748`"} }, RowSpacings->1, ColumnSpacings->1, ColumnAlignments->{Left}], "\[NoBreak]", ")"}], Function[ BoxForm`e$, MatrixForm[ BoxForm`e$]]]], "Output"], Cell[BoxData[ \({0.4`, 0.9`, \(-1.2`\)}\)], "Output"], Cell[BoxData[ TagBox[ RowBox[{"(", "\[NoBreak]", GridBox[{ {"0.2252451922628743`", "0.968999132784982`", \(-0.10151494483283646`\)}, {\(-0.5793647866551066`\), "0.049442650964680535`", \(-0.8135673716722344`\)}, {\(-0.7833269096274834`\), "0.24206632340649498`", "0.57254069525748`"} }, RowSpacings->1, ColumnSpacings->1, ColumnAlignments->{Left}], "\[NoBreak]", ")"}], Function[ BoxForm`e$, MatrixForm[ BoxForm`e$]]]], "Output"], Cell[BoxData[ \(0.`\)], "Output"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["\<\ Test for some random value where middle axis is at \ 90\[Degree]\ \>", "Subsection"], Cell[CellGroupData[{ Cell[BoxData[{ \(\(tm = eulertomatrix[1.4, \[Pi]/2, \(- .9\)];\)\), "\[IndentingNewLine]", \(MatrixForm[tm]\), "\[IndentingNewLine]", \({tx, ty, tz} = matrixtoeuler[tm]\), "\[IndentingNewLine]", \(MatrixForm[tmm = eulertomatrix[tx, ty, tz]]\), "\[IndentingNewLine]", \(Norm[tm - tmm, \[Infinity]]\), "\[IndentingNewLine]", \(\)}], "Input"], Cell[BoxData[ TagBox[ RowBox[{"(", "\[NoBreak]", GridBox[{ {"0.`", "0.7457052121767203`", \(-0.6662760212798242`\)}, {"0.`", \(-0.6662760212798242`\), \(-0.7457052121767203`\)}, {\(-1.`\), "0.`", "0.`"} }, RowSpacings->1, ColumnSpacings->1, ColumnAlignments->{Left}], "\[NoBreak]", ")"}], Function[ BoxForm`e$, MatrixForm[ BoxForm`e$]]]], "Output"], Cell[BoxData[ \({2.3`, 1.5707963267948966`, 0}\)], "Output"], Cell[BoxData[ TagBox[ RowBox[{"(", "\[NoBreak]", GridBox[{ {"6.123031769111886`*^-17", "0.7457052121767203`", \(-0.6662760212798241`\)}, {"0.`", \(-0.6662760212798241`\), \(-0.7457052121767203`\)}, {\(-1.`\), "4.565976704550378`*^-17", \(-4.07962924529383`*^-17\)} }, RowSpacings->1, ColumnSpacings->1, ColumnAlignments->{Left}], "\[NoBreak]", ")"}], Function[ BoxForm`e$, MatrixForm[ BoxForm`e$]]]], "Output"], Cell[BoxData[ \(1.7225262015363452`*^-16\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[{ \(\(tm = eulertomatrix[\(- .6\), \(-\[Pi]\)/ 2, \(-1.9\)];\)\), "\[IndentingNewLine]", \(MatrixForm[tm]\), "\[IndentingNewLine]", \({tx, ty, tz} = matrixtoeuler[tm]\), "\[IndentingNewLine]", \(MatrixForm[tmm = eulertomatrix[tx, ty, tz]]\), "\[IndentingNewLine]", \(Norm[tm - tmm, \[Infinity]]\), "\[IndentingNewLine]", \(\)}], "Input"], Cell[BoxData[ TagBox[ RowBox[{"(", "\[NoBreak]", GridBox[{ {"0.`", "0.5984721441039565`", "0.8011436155469337`"}, {"0.`", \(-0.8011436155469337`\), "0.5984721441039565`"}, {"1.`", "0.`", "0.`"} }, RowSpacings->1, ColumnSpacings->1, ColumnAlignments->{Left}], "\[NoBreak]", ")"}], Function[ BoxForm`e$, MatrixForm[ BoxForm`e$]]]], "Output"], Cell[BoxData[ \({\(-2.5`\), \(-1.5707963267948966`\), 0}\)], "Output"], Cell[BoxData[ TagBox[ RowBox[{"(", "\[NoBreak]", GridBox[{ {"6.123031769111886`*^-17", "0.5984721441039565`", "0.8011436155469337`"}, {"0.`", \(-0.8011436155469337`\), "0.5984721441039565`"}, { "1.`", \(-3.664463951277033`*^-17\), \ \(-4.9054278096150343`*^-17\)} }, RowSpacings->1, ColumnSpacings->1, ColumnAlignments->{Left}], "\[NoBreak]", ")"}], Function[ BoxForm`e$, MatrixForm[ BoxForm`e$]]]], "Output"], Cell[BoxData[ \(8.569891760892068`*^-17\)], "Output"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["\<\ Test for some more values using a set of loops\ \>", "Subsection"], Cell[BoxData[ \(\(Do[\[IndentingNewLine]Do[\[IndentingNewLine]Do[\[IndentingNewLine]\ \[IndentingNewLine]tm\ = \ eulertomatrix[\[Theta]x, \[Theta]y, \[Theta]z]; \ \[IndentingNewLine]{tx, ty, tz} = matrixtoeuler[tm]; \[IndentingNewLine]tmm = eulertomatrix[tx, ty, tz]; \[IndentingNewLine]\[IndentingNewLine]\ err\ = \ Norm[tm - tmm, \[Infinity]]; \[IndentingNewLine]\[IndentingNewLine]If[ err\ > \ 0.0001, \ Print["\", err, "\<) \>", \[Theta]x, "\< \>", \[Theta]y, \ "\< \>", \ \ \[Theta]z]];\[IndentingNewLine]\[IndentingNewLine], {\[Theta]z, 0. , 2 \[Pi], 2 \[Pi]/32}]\[IndentingNewLine], {\[Theta]y, 0. , 2 \[Pi], 2 \[Pi]/32}]\[IndentingNewLine], {\[Theta]x, 0. , 2 \[Pi], 2 \[Pi]/32}];\)\)], "Input"] }, Open ]] }, FrontEndVersion->"5.2 for Macintosh", ScreenRectangle->{{0, 1440}, {0, 874}}, WindowSize->{973, 772}, WindowMargins->{{Automatic, 68}, {37, Automatic}}, PrintingCopies->1, PrintingPageRange->{1, Automatic} ] (******************************************************************* Cached data follows. If you edit this Notebook file directly, not using Mathematica, you must remove the line containing CacheID at the top of the file. The cache data will then be recreated when you save this file from within Mathematica. *******************************************************************) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[1776, 53, 116, 3, 38, "Subsection"], Cell[1895, 58, 874, 21, 163, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[2806, 84, 204, 3, 56, "Subsection"], Cell[3013, 89, 138, 2, 27, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[3188, 96, 173, 5, 38, "Subsection"], Cell[CellGroupData[{ Cell[3386, 105, 91, 1, 27, "Input"], Cell[3480, 108, 883, 19, 69, "Output"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[4412, 133, 180, 4, 56, "Subsection"], Cell[CellGroupData[{ Cell[4617, 141, 290, 5, 43, "Input"], Cell[4910, 148, 491, 12, 69, "Output"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[5450, 166, 108, 3, 38, "Subsection"], Cell[5561, 171, 96, 3, 30, "Text"], Cell[CellGroupData[{ Cell[5682, 178, 366, 8, 43, "Input"], Cell[6051, 188, 503, 15, 69, "Output"], Cell[6557, 205, 502, 15, 69, "Output"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[7108, 226, 121, 3, 38, "Subsection"], Cell[7232, 231, 110, 3, 30, "Text"], Cell[7345, 236, 1178, 21, 187, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[8560, 262, 48, 0, 38, "Subsection"], Cell[CellGroupData[{ Cell[8633, 266, 364, 6, 107, "Input"], Cell[9000, 274, 577, 14, 69, "Output"], Cell[9580, 290, 57, 1, 27, "Output"], Cell[9640, 293, 577, 14, 69, "Output"], Cell[10220, 309, 37, 1, 27, "Output"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[10306, 316, 94, 3, 38, "Subsection"], Cell[CellGroupData[{ Cell[10425, 323, 377, 7, 107, "Input"], Cell[10805, 332, 451, 11, 69, "Output"], Cell[11259, 345, 64, 1, 27, "Output"], Cell[11326, 348, 543, 13, 75, "Output"], Cell[11872, 363, 58, 1, 29, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[11967, 369, 400, 8, 107, "Input"], Cell[12370, 379, 442, 11, 69, "Output"], Cell[12815, 392, 74, 1, 27, "Output"], Cell[12892, 395, 541, 14, 75, "Output"], Cell[13436, 411, 57, 1, 29, "Output"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[13542, 418, 76, 2, 38, "Subsection"], Cell[13621, 422, 906, 16, 251, "Input"] }, Open ]] } ] *) (******************************************************************* End of Mathematica Notebook file. *******************************************************************)