(*********************************************************************** Mathematica-Compatible Notebook This notebook can be used on any computer system with Mathematica 3.0, MathReader 3.0, or any compatible application. The data for the notebook starts with the line of 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[ 9167, 371]*) (*NotebookOutlinePosition[ 9804, 394]*) (* CellTagsIndexPosition[ 9760, 390]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["Solving linear systems", "Title", TextAlignment->Center], Cell[TextData[{ "Matrices are input as lists of lists. Each row is a list of \ comma-separated numbers (or symbols), and there is a comma between each row. \ There is a function ", StyleBox["MatrixForm", FontWeight->"Bold"], " that", StyleBox[" ", FontWeight->"Bold"], "prints matrices very nicely.\n" }], "Text", FontSize->14], Cell[CellGroupData[{ Cell[BoxData[ \(M = \ {{2, 1, 1}, {4, \(-6\), 0}, {\(-2\), 7, 2}}\)], "Input"], Cell[BoxData[ \({{2, 1, 1}, {4, \(-6\), 0}, {\(-2\), 7, 2}}\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(MatrixForm[M]\)], "Input"], Cell[BoxData[ TagBox[ RowBox[{"(", GridBox[{ {"2", "1", "1"}, {"4", \(-6\), "0"}, {\(-2\), "7", "2"} }], ")"}], (MatrixForm[ #]&)]], "Output"] }, Open ]], Cell["\<\ We can do this for vectors too. By default these are column vectors, which is the same thing as a matrix whose rows have only one element.\ \>", "Text", FontSize->14], Cell[CellGroupData[{ Cell[BoxData[{ \(rhs\ = \ {5, \(-2\), 9}\), \(MatrixForm[rhs]\)}], "Input"], Cell[BoxData[ \({5, \(-2\), 9}\)], "Output"], Cell[BoxData[ InterpretationBox[ RowBox[{"(", GridBox[{ {"5"}, {\(-2\)}, {"9"} }], ")"}], MatrixForm[ {5, -2, 9}]]], "Output"] }, Open ]], Cell["Here is a matrix-vector (dot) product", "Text", FontSize->14], Cell[CellGroupData[{ Cell[BoxData[ \(M.rhs\)], "Input"], Cell[BoxData[ \({17, 32, \(-6\)}\)], "Output"] }, Open ]], Cell["Here is a vector of unknowns", "Text", FontSize->14], Cell[CellGroupData[{ Cell[BoxData[ \(u\ = \ {x, y, z}\)], "Input"], Cell[BoxData[ \({x, y, z}\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(MatrixForm[M.\ u]\)], "Input"], Cell[BoxData[ InterpretationBox[ RowBox[{"(", GridBox[{ {\(2\ x + y + z\)}, {\(4\ x - 6\ y\)}, {\(\(-2\)\ x + 7\ y + 2\ z\)} }], ")"}], MatrixForm[ { Plus[ Times[ 2, x], y, z], Plus[ Times[ 4, x], Times[ -6, y]], Plus[ Times[ -2, x], Times[ 7, y], Times[ 2, z]]}]]], "Output"] }, Open ]], Cell["\<\ Our goal is to find x,y,z such that M.x=rhs. There are many ways \ to do this.\ \>", "Text", FontSize->14], Cell[CellGroupData[{ Cell[BoxData[ \(Solve[M.u\ == \ rhs]\)], "Input"], Cell[BoxData[ \({{x \[Rule] 1, y \[Rule] 1, z \[Rule] 2}}\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(LinearSolve[M, rhs]\)], "Input"], Cell[BoxData[ \({1, 1, 2}\)], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(Inverse[M]\ .\ rhs\)], "Input"], Cell[BoxData[ \({1, 1, 2}\)], "Output"] }, Open ]], Cell[TextData[{ "\n", StyleBox["Solve ", FontWeight->"Bold"], "is a very general tool. You can use is to solve nonlinear problems such \ as" }], "Text", FontSize->14], Cell[CellGroupData[{ Cell[BoxData[ \(Solve[\((x - 1)\)^2\ == \ 1]\)], "Input"], Cell[BoxData[ \({{x \[Rule] 0}, {x \[Rule] 2}}\)], "Output"] }, Open ]], Cell[TextData[{ StyleBox["LinearSolve ", FontWeight->"Bold"], "solve only linear algebraic equations. As we will see, the only way there \ can be a unique solution of the linear system is if the inverse of the matrix \ exists. A matrix N is the inverse of M if and only if N.M=M.N = I, where I \ is the identity matrix." }], "Text", FontSize->14], Cell[CellGroupData[{ Cell[BoxData[{ \(IdentityMatrix[3]\), \(MatrixForm[%]\)}], "Input"], Cell[BoxData[ \({{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}\)], "Output"], Cell[BoxData[ TagBox[ RowBox[{"(", GridBox[{ {"1", "0", "0"}, {"0", "1", "0"}, {"0", "0", "1"} }], ")"}], (MatrixForm[ #]&)]], "Output"] }, Open ]], Cell["\<\ Remember, the percent sign means to insert that last computed \ expression. It's a convenient history mechanism. If you go on to study \ linear algebra (highly recommended) you will discover that you can actually \ solve linear systems without inverting matrices. In fact, you can solve \ linear systems without even having the whole matrix! All you really need are \ the nonzero entries in the matrix. This is a huge advantage in many problems \ where the matrix is mostly zero. Think of the tridiagonal matrix that arose \ in the spring/mass problem. If we have 1000 masses, more than 99% of the \ entries of this matrix are 0. \ \>", "Text", FontSize->14], Cell["Here is the inverse matrix explicitly.", "Text", FontSize->14], Cell[CellGroupData[{ Cell[BoxData[ \(MatrixForm[Inverse[M]]\)], "Input"], Cell[BoxData[ TagBox[ RowBox[{"(", GridBox[{ {\(3\/4\), \(-\(5\/16\)\), \(-\(3\/8\)\)}, {\(1\/2\), \(-\(3\/8\)\), \(-\(1\/4\)\)}, {\(-1\), "1", "1"} }], ")"}], (MatrixForm[ #]&)]], "Output"] }, Open ]], Cell[TextData[{ "If you want to do any calculation in decimal (as opposed to integer) \ arithmetic, you have two choices: you can enter the numbers in decimal form \ in the first place (one is sufficient to make the whole calculation decimal), \ or you can use the ", StyleBox["B ", FontWeight->"Bold"], "function to get a numerical approximation to a result. For instance" }], "Text", FontSize->14], Cell[CellGroupData[{ Cell[BoxData[ \(N[MatrixForm[Inverse[M]]]\)], "Input"], Cell[BoxData[ TagBox[ RowBox[{"(", GridBox[{ {"0.75`", \(-0.3125`\), \(-0.375`\)}, {"0.5`", \(-0.375`\), \(-0.25`\)}, {\(-1.`\), "1.`", "1.`"} }], ")"}], (MatrixForm[ #]&)]], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(MatrixForm[Inverse[M]] // N\)], "Input"], Cell[BoxData[ TagBox[ RowBox[{"(", GridBox[{ {"0.75`", \(-0.3125`\), \(-0.375`\)}, {"0.5`", \(-0.375`\), \(-0.25`\)}, {\(-1.`\), "1.`", "1.`"} }], ")"}], (MatrixForm[ #]&)]], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(MatrixForm[M]\)], "Input"], Cell[BoxData[ TagBox[ RowBox[{"(", GridBox[{ {"2", "1", "1"}, {"4", \(-6\), "0"}, {\(-2\), "7", "2"} }], ")"}], (MatrixForm[ #]&)]], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(M1 = \ {{2., 1, 1}, {4, \(-6\), 0}, {\(-2\), 7, 2}}\)], "Input"], Cell[BoxData[ RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{ StyleBox["2.`", StyleBoxAutoDelete->True, PrintPrecision->1], ",", "1", ",", "1"}], "}"}], ",", \({4, \(-6\), 0}\), ",", \({\(-2\), 7, 2}\)}], "}"}]], "Output"] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ \(MatrixForm[Inverse[M1]]\)], "Input"], Cell[BoxData[ TagBox[ RowBox[{"(", GridBox[{ {"0.75`", \(-0.3125`\), \(-0.375`\)}, {"0.5`", \(-0.375`\), \(-0.25`\)}, {\(-1.`\), "1.`", "1.`"} }], ")"}], (MatrixForm[ #]&)]], "Output"] }, Open ]], Cell["\<\ See? One \".\" was sufficient to make the whole calculation \ non-integer.\ \>", "Text", FontSize->14] }, Open ]] }, FrontEndVersion->"X 3.0", ScreenRectangle->{{0, 1280}, {0, 1024}}, WindowSize->{549, 786}, WindowMargins->{{Automatic, 316}, {Automatic, 23}} ] (*********************************************************************** 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[1731, 51, 64, 1, 104, "Title"], Cell[1798, 54, 349, 11, 90, "Text"], Cell[CellGroupData[{ Cell[2172, 69, 82, 1, 27, "Input"], Cell[2257, 72, 77, 1, 27, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[2371, 78, 46, 1, 27, "Input"], Cell[2420, 81, 206, 7, 62, "Output"] }, Open ]], Cell[2641, 91, 183, 6, 71, "Text"], Cell[CellGroupData[{ Cell[2849, 101, 85, 2, 43, "Input"], Cell[2937, 105, 48, 1, 27, "Output"], Cell[2988, 108, 190, 7, 62, "Output"] }, Open ]], Cell[3193, 118, 69, 1, 33, "Text"], Cell[CellGroupData[{ Cell[3287, 123, 38, 1, 27, "Input"], Cell[3328, 126, 50, 1, 27, "Output"] }, Open ]], Cell[3393, 130, 60, 1, 33, "Text"], Cell[CellGroupData[{ Cell[3478, 135, 50, 1, 27, "Input"], Cell[3531, 138, 43, 1, 27, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[3611, 144, 50, 1, 27, "Input"], Cell[3664, 147, 432, 16, 62, "Output"] }, Open ]], Cell[4111, 166, 119, 4, 33, "Text"], Cell[CellGroupData[{ Cell[4255, 174, 54, 1, 27, "Input"], Cell[4312, 177, 75, 1, 27, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[4424, 183, 52, 1, 27, "Input"], Cell[4479, 186, 43, 1, 27, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[4559, 192, 51, 1, 27, "Input"], Cell[4613, 195, 43, 1, 27, "Output"] }, Open ]], Cell[4671, 199, 179, 7, 52, "Text"], Cell[CellGroupData[{ Cell[4875, 210, 62, 1, 27, "Input"], Cell[4940, 213, 64, 1, 27, "Output"] }, Open ]], Cell[5019, 217, 358, 8, 71, "Text"], Cell[CellGroupData[{ Cell[5402, 229, 76, 2, 43, "Input"], Cell[5481, 233, 67, 1, 27, "Output"], Cell[5551, 236, 200, 7, 62, "Output"] }, Open ]], Cell[5766, 246, 678, 11, 166, "Text"], Cell[6447, 259, 70, 1, 33, "Text"], Cell[CellGroupData[{ Cell[6542, 264, 55, 1, 27, "Input"], Cell[6600, 267, 254, 7, 76, "Output"] }, Open ]], Cell[6869, 277, 413, 9, 90, "Text"], Cell[CellGroupData[{ Cell[7307, 290, 58, 1, 27, "Input"], Cell[7368, 293, 248, 7, 62, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[7653, 305, 60, 1, 27, "Input"], Cell[7716, 308, 248, 7, 62, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[8001, 320, 46, 1, 27, "Input"], Cell[8050, 323, 206, 7, 62, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[8293, 335, 84, 1, 27, "Input"], Cell[8380, 338, 297, 8, 27, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[8714, 351, 56, 1, 27, "Input"], Cell[8773, 354, 248, 7, 62, "Output"] }, Open ]], Cell[9036, 364, 115, 4, 33, "Text"] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)