(* Content-type: application/vnd.wolfram.mathematica *) (*** Wolfram Notebook File ***) (* http://www.wolfram.com/nb *) (* CreatedBy='Mathematica 8.0' *) (*CacheID: 234*) (* Internal cache information: NotebookFileLineBreakTest NotebookFileLineBreakTest NotebookDataPosition[ 157, 7] NotebookDataLength[ 16298, 504] NotebookOptionsPosition[ 15149, 459] NotebookOutlinePosition[ 15494, 474] CellTagsIndexPosition[ 15451, 471] WindowFrame->Normal*) (* Beginning of Notebook Content *) Notebook[{ Cell[CellGroupData[{ Cell["LU Decomposition", "Section", CellChangeTimes->{{3.688454768859472*^9, 3.6884547723394766`*^9}}], Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{"LU", "[", "A_", "]"}], ":=", "\[IndentingNewLine]", RowBox[{"(", RowBox[{ RowBox[{"U", "=", "A"}], ";", "\[IndentingNewLine]", RowBox[{"n", "=", RowBox[{"Length", "[", "A", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"L", "=", RowBox[{"Table", "[", RowBox[{"0", ",", RowBox[{"{", "n", "}"}], ",", RowBox[{"{", "n", "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"For", "[", RowBox[{ RowBox[{"i", "=", "1"}], ",", RowBox[{"i", "\[LessEqual]", "n"}], ",", RowBox[{"i", "++"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"L", "[", RowBox[{"[", RowBox[{"i", ",", "i"}], "]"}], "]"}], "=", "1"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"For", "[", RowBox[{ RowBox[{"i", "=", "1"}], ",", " ", RowBox[{"i", "\[LessEqual]", "n"}], ",", " ", RowBox[{"i", "++"}], ",", "\[IndentingNewLine]", RowBox[{"For", "[", RowBox[{ RowBox[{"j", "=", RowBox[{"i", "+", "1"}]}], ",", RowBox[{"j", "\[LessEqual]", "n"}], ",", RowBox[{"j", "++"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"l", "=", FractionBox[ RowBox[{"U", "[", RowBox[{"[", RowBox[{"j", ",", "i"}], "]"}], "]"}], RowBox[{"U", "[", RowBox[{"[", RowBox[{"i", ",", "i"}], "]"}], "]"}]]}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"L", "[", RowBox[{"[", RowBox[{"j", ",", "i"}], "]"}], "]"}], "=", "l"}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"U", "[", RowBox[{"[", RowBox[{"j", ",", "i"}], "]"}], "]"}], "=", "0"}], ";", "\[IndentingNewLine]", RowBox[{"For", "[", RowBox[{ RowBox[{"k", "=", RowBox[{"i", "+", "1"}]}], ",", RowBox[{"k", "\[LessEqual]", "n"}], ",", RowBox[{"k", "++"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"U", "[", RowBox[{"[", RowBox[{"j", ",", "k"}], "]"}], "]"}], "=", RowBox[{ RowBox[{"U", "[", RowBox[{"[", RowBox[{"j", ",", "k"}], "]"}], "]"}], "-", RowBox[{"l", "*", RowBox[{"U", "[", RowBox[{"[", RowBox[{"i", ",", "k"}], "]"}], "]"}]}]}]}], ";"}]}], "\[IndentingNewLine]", "]"}]}]}], "\[IndentingNewLine]", "]"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"L", ",", "U"}], "}"}]}], "\[IndentingNewLine]", ")"}]}], "\[IndentingNewLine]"}]], "Input", CellChangeTimes->{{3.6557929521573105`*^9, 3.6557929531373124`*^9}, { 3.655792992582371*^9, 3.655793106106538*^9}, {3.655793138812117*^9, 3.655793169457162*^9}, {3.6557932145632286`*^9, 3.6557935316707597`*^9}, { 3.655793580854902*^9, 3.6557936143739595`*^9}, 3.655793659345024*^9, { 3.655793741346494*^9, 3.6557937470565023`*^9}, {3.6557939568350487`*^9, 3.655794013999318*^9}, {3.655794130158848*^9, 3.655794131032898*^9}, { 3.6858681419063945`*^9, 3.6858681449285674`*^9}, {3.6869957606070504`*^9, 3.686995836602166*^9}, {3.686995889946247*^9, 3.6869959772033825`*^9}, { 3.68699604089898*^9, 3.6869960634572706`*^9}, {3.6869966456600885`*^9, 3.686996650870096*^9}}], Cell[CellGroupData[{ Cell[BoxData[{ RowBox[{ RowBox[{"A", "=", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"1", ",", "1", ",", "1"}], "}"}], ",", RowBox[{"{", RowBox[{"1", ",", "2", ",", "2"}], "}"}], ",", RowBox[{"{", RowBox[{"1", ",", "2", ",", "3"}], "}"}]}], "}"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"LU", "[", "A", "]"}], "[", RowBox[{"[", "1", "]"}], "]"}], "//", "MatrixForm"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"LU", "[", "A", "]"}], "[", RowBox[{"[", "2", "]"}], "]"}], "//", "MatrixForm"}]}], "Input", CellChangeTimes->{{3.6869958039221168`*^9, 3.6869958103411274`*^9}, { 3.686995985503394*^9, 3.686995992461404*^9}, {3.6869966543661013`*^9, 3.686996654856102*^9}}], Cell[BoxData[ TagBox[ RowBox[{"(", "\[NoBreak]", GridBox[{ {"1", "0", "0"}, {"1", "1", "0"}, {"1", "1", "1"} }, GridBoxAlignment->{ "Columns" -> {{Center}}, "ColumnsIndexed" -> {}, "Rows" -> {{Baseline}}, "RowsIndexed" -> {}}, GridBoxSpacings->{"Columns" -> { Offset[0.27999999999999997`], { Offset[0.7]}, Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> { Offset[0.2], { Offset[0.4]}, Offset[0.2]}, "RowsIndexed" -> {}}], "\[NoBreak]", ")"}], Function[BoxForm`e$, MatrixForm[BoxForm`e$]]]], "Output", CellChangeTimes->{{3.6869958331581597`*^9, 3.6869958424211745`*^9}, { 3.686995966878366*^9, 3.686995993091405*^9}, {3.6869960530596757`*^9, 3.6869960659614134`*^9}, 3.6869966066083784`*^9, {3.686996655356103*^9, 3.686996657576106*^9}}], Cell[BoxData[ TagBox[ RowBox[{"(", "\[NoBreak]", GridBox[{ {"1", "1", "1"}, {"0", "1", "1"}, {"0", "0", "1"} }, GridBoxAlignment->{ "Columns" -> {{Center}}, "ColumnsIndexed" -> {}, "Rows" -> {{Baseline}}, "RowsIndexed" -> {}}, GridBoxSpacings->{"Columns" -> { Offset[0.27999999999999997`], { Offset[0.7]}, Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> { Offset[0.2], { Offset[0.4]}, Offset[0.2]}, "RowsIndexed" -> {}}], "\[NoBreak]", ")"}], Function[BoxForm`e$, MatrixForm[BoxForm`e$]]]], "Output", CellChangeTimes->{{3.6869958331581597`*^9, 3.6869958424211745`*^9}, { 3.686995966878366*^9, 3.686995993091405*^9}, {3.6869960530596757`*^9, 3.6869960659614134`*^9}, 3.6869966066083784`*^9, {3.686996655356103*^9, 3.686996657576106*^9}}] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["LUSolve", "Section", CellChangeTimes->{{3.6884547788194857`*^9, 3.6884547801394873`*^9}}], Cell[BoxData[ RowBox[{ RowBox[{"LUSolve", "[", RowBox[{"L_", ",", "U_", ",", "b_"}], "]"}], ":=", RowBox[{"(", "\[IndentingNewLine]", RowBox[{ RowBox[{"n", "=", RowBox[{"Length", "[", "b", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"y", "=", RowBox[{"Table", "[", RowBox[{"0", ",", RowBox[{"{", RowBox[{"i", ",", "n"}], "}"}]}], "]"}]}], " ", ";", "\[IndentingNewLine]", RowBox[{"For", "[", RowBox[{ RowBox[{"i", "=", "1"}], ",", RowBox[{"i", "\[LessEqual]", "n"}], ",", " ", RowBox[{"i", "++"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"y", "[", RowBox[{"[", "i", "]"}], "]"}], "=", FractionBox[ RowBox[{ RowBox[{"b", "[", RowBox[{"[", "i", "]"}], "]"}], "-", RowBox[{"Sum", "[", RowBox[{ RowBox[{ RowBox[{"L", "[", RowBox[{"[", RowBox[{"i", ",", "j"}], "]"}], "]"}], RowBox[{"y", "[", RowBox[{"[", "j", "]"}], "]"}]}], ",", RowBox[{"{", RowBox[{"j", ",", "1", ",", RowBox[{"i", "-", "1"}]}], "}"}]}], "]"}]}], RowBox[{"L", "[", RowBox[{"[", RowBox[{"i", ",", "i"}], "]"}], "]"}]]}]}], "\[IndentingNewLine]", "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"x", "=", RowBox[{"Table", "[", RowBox[{"0", ",", RowBox[{"{", RowBox[{"i", ",", "n"}], "}"}]}], "]"}]}], " ", ";", "\[IndentingNewLine]", RowBox[{"For", "[", RowBox[{ RowBox[{"i", "=", "n"}], ",", RowBox[{"i", "\[GreaterEqual]", "1"}], ",", " ", RowBox[{"i", "--"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"x", "[", RowBox[{"[", "i", "]"}], "]"}], "=", FractionBox[ RowBox[{ RowBox[{"y", "[", RowBox[{"[", "i", "]"}], "]"}], "-", RowBox[{"Sum", "[", RowBox[{ RowBox[{ RowBox[{"U", "[", RowBox[{"[", RowBox[{"i", ",", "j"}], "]"}], "]"}], RowBox[{"x", "[", RowBox[{"[", "j", "]"}], "]"}]}], ",", RowBox[{"{", RowBox[{"j", ",", RowBox[{"i", "+", "1"}], ",", "n"}], "}"}]}], "]"}]}], RowBox[{"U", "[", RowBox[{"[", RowBox[{"i", ",", "i"}], "]"}], "]"}]]}]}], "\[IndentingNewLine]", "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", "x"}], "\[IndentingNewLine]", ")"}]}]], "Input", CellChangeTimes->{{3.688454461859042*^9, 3.6884545743691998`*^9}, { 3.6884547177694006`*^9, 3.688454753759451*^9}}], Cell[CellGroupData[{ Cell[BoxData[{ RowBox[{ RowBox[{"A", "=", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"1", ",", "7", ",", "5"}], "}"}], ",", RowBox[{"{", RowBox[{"1", ",", "2", ",", "2"}], "}"}], ",", RowBox[{"{", RowBox[{"1", ",", "2", ",", "3"}], "}"}]}], "}"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"luA", "=", RowBox[{"LU", "[", "A", "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"LUSolve", "[", RowBox[{ RowBox[{"luA", "[", RowBox[{"[", "1", "]"}], "]"}], ",", RowBox[{"luA", "[", RowBox[{"[", "2", "]"}], "]"}], ",", RowBox[{"{", RowBox[{"1", ",", "2", ",", "3"}], "}"}]}], "]"}]}], "Input", CellChangeTimes->{{3.688454576679203*^9, 3.6884547044793816`*^9}}], Cell[BoxData[ RowBox[{"{", RowBox[{ FractionBox["8", "5"], ",", RowBox[{"-", FractionBox["4", "5"]}], ",", "1"}], "}"}]], "Output", CellChangeTimes->{{3.6884546272092733`*^9, 3.6884546486193037`*^9}, { 3.6884546900993614`*^9, 3.6884547201794033`*^9}, 3.688454755019452*^9}] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"LinearSolve", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"1", ",", "7", ",", "5"}], "}"}], ",", RowBox[{"{", RowBox[{"1", ",", "2", ",", "2"}], "}"}], ",", RowBox[{"{", RowBox[{"1", ",", "2", ",", "3"}], "}"}]}], "}"}], ",", RowBox[{"{", RowBox[{"1", ",", "2", ",", "3"}], "}"}]}], "]"}]], "Input", CellChangeTimes->{{3.6884546509893064`*^9, 3.6884546621493225`*^9}}], Cell[BoxData[ RowBox[{"{", RowBox[{ FractionBox["8", "5"], ",", RowBox[{"-", FractionBox["4", "5"]}], ",", "1"}], "}"}]], "Output", CellChangeTimes->{3.6884546623993225`*^9, 3.6884547087293873`*^9, 3.688454756439454*^9}] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Inverse Matrix", "Section", CellChangeTimes->{{3.688454791749504*^9, 3.68845479587951*^9}}], Cell[BoxData[ RowBox[{ RowBox[{"InverseMatrix", "[", "A_", "]"}], ":=", RowBox[{"(", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"{", RowBox[{"L", ",", "U"}], "}"}], "=", RowBox[{"LU", "[", "A", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"n", "=", RowBox[{"Length", "[", "A", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"AInverse", "=", RowBox[{"Table", "[", RowBox[{"0", ",", RowBox[{"{", "n", "}"}], ",", RowBox[{"{", "n", "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"For", "[", RowBox[{ RowBox[{"k", "=", "1"}], ",", RowBox[{"k", "\[LessEqual]", "n"}], ",", RowBox[{"k", "++"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"b", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"If", "[", RowBox[{ RowBox[{"l", "\[Equal]", "k"}], ",", "1", ",", "0"}], "]"}], ",", RowBox[{"{", RowBox[{"l", ",", "1", ",", "n"}], "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"AInverse", "[", RowBox[{"[", "k", "]"}], "]"}], "=", RowBox[{"LUSolve", "[", RowBox[{"L", ",", "U", ",", "b"}], "]"}]}], ";"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"Transpose", "[", "AInverse", "]"}]}], "\[IndentingNewLine]", ")"}]}]], "Input", CellChangeTimes->{{3.688454799689515*^9, 3.688454983961774*^9}}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"InverseMatrix", "[", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"1", ",", "7", ",", "5"}], "}"}], ",", RowBox[{"{", RowBox[{"1", ",", "2", ",", "2"}], "}"}], ",", RowBox[{"{", RowBox[{"1", ",", "2", ",", "3"}], "}"}]}], "}"}], "]"}]], "Input", CellChangeTimes->{{3.6884549857117767`*^9, 3.688454994041788*^9}}], Cell[BoxData[ RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"-", FractionBox["2", "5"]}], ",", FractionBox["11", "5"], ",", RowBox[{"-", FractionBox["4", "5"]}]}], "}"}], ",", RowBox[{"{", RowBox[{ FractionBox["1", "5"], ",", FractionBox["2", "5"], ",", RowBox[{"-", FractionBox["3", "5"]}]}], "}"}], ",", RowBox[{"{", RowBox[{"0", ",", RowBox[{"-", "1"}], ",", "1"}], "}"}]}], "}"}]], "Output", CellChangeTimes->{3.6884549947217894`*^9}] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"Inverse", "[", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"1", ",", "7", ",", "5"}], "}"}], ",", RowBox[{"{", RowBox[{"1", ",", "2", ",", "2"}], "}"}], ",", RowBox[{"{", RowBox[{"1", ",", "2", ",", "3"}], "}"}]}], "}"}], "]"}]], "Input", CellChangeTimes->{{3.6884550023618*^9, 3.6884550024718*^9}}], Cell[BoxData[ RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"-", FractionBox["2", "5"]}], ",", FractionBox["11", "5"], ",", RowBox[{"-", FractionBox["4", "5"]}]}], "}"}], ",", RowBox[{"{", RowBox[{ FractionBox["1", "5"], ",", FractionBox["2", "5"], ",", RowBox[{"-", FractionBox["3", "5"]}]}], "}"}], ",", RowBox[{"{", RowBox[{"0", ",", RowBox[{"-", "1"}], ",", "1"}], "}"}]}], "}"}]], "Output", CellChangeTimes->{3.688455003011801*^9}] }, Open ]] }, Open ]] }, WindowSize->{1255, 817}, WindowMargins->{{Automatic, 147}, {43, Automatic}}, FrontEndVersion->"10.1 for Microsoft Windows (64-bit) (March 23, 2015)", StyleDefinitions->"Default.nb" ] (* End of Notebook Content *) (* Internal cache information *) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[579, 22, 103, 1, 70, "Section"], Cell[685, 25, 3676, 88, 494, "Input"], Cell[CellGroupData[{ Cell[4386, 117, 777, 23, 72, "Input"], Cell[5166, 142, 865, 22, 57, "Output"], Cell[6034, 166, 865, 22, 57, "Output"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[6948, 194, 96, 1, 70, "Section"], Cell[7047, 197, 2732, 76, 390, "Input"], Cell[CellGroupData[{ Cell[9804, 277, 753, 23, 72, "Input"], Cell[10560, 302, 293, 7, 46, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[10890, 314, 464, 13, 31, "Input"], Cell[11357, 329, 240, 7, 46, "Output"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[11646, 342, 98, 1, 70, "Section"], Cell[11747, 345, 1500, 39, 212, "Input"], Cell[CellGroupData[{ Cell[13272, 388, 374, 10, 31, "Input"], Cell[13649, 400, 536, 19, 46, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[14222, 424, 362, 10, 31, "Input"], Cell[14587, 436, 534, 19, 103, "Output"] }, Open ]] }, Open ]] } ] *) (* End of internal cache information *)