(* 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[ 9337, 251] NotebookOptionsPosition[ 9022, 235] NotebookOutlinePosition[ 9366, 250] CellTagsIndexPosition[ 9323, 247] WindowFrame->Normal*) (* Beginning of Notebook Content *) Notebook[{ Cell[CellGroupData[{ Cell["Cholesky", "Section", CellChangeTimes->{{3.7528526993294487`*^9, 3.7528527021568923`*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"Cholesky", "[", "A_", "]"}], ":=", RowBox[{"(", "\[IndentingNewLine]", RowBox[{ RowBox[{"n", "=", RowBox[{"Length", "[", "A", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"L", "=", RowBox[{"Table", "[", RowBox[{"0", ",", RowBox[{"{", RowBox[{"i", ",", "n"}], "}"}], ",", RowBox[{"{", RowBox[{"j", ",", "n"}], "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"For", "[", RowBox[{ RowBox[{"k", "=", "1"}], ",", " ", RowBox[{"k", "\[LessEqual]", "n"}], ",", " ", RowBox[{"k", "++"}], ",", " ", RowBox[{"(*", RowBox[{"Iterate", " ", "over", " ", "the", " ", "columns"}], "*)"}], " ", "\[IndentingNewLine]", RowBox[{"(*", RowBox[{"Find", " ", "the", " ", "diagonal", " ", "element"}], "*)"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"sum", "=", "0"}], ";", "\[IndentingNewLine]", RowBox[{"For", "[", RowBox[{ RowBox[{"p", "=", "1"}], ",", RowBox[{"p", "\[LessEqual]", RowBox[{"k", "-", "1"}]}], ",", RowBox[{"p", "++"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"sum", "+=", RowBox[{ RowBox[{"L", "[", RowBox[{"[", RowBox[{"k", ",", "p"}], "]"}], "]"}], "^", "2"}]}], ";"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"L", "[", RowBox[{"[", RowBox[{"k", ",", "k"}], "]"}], "]"}], "=", RowBox[{"\[Sqrt]", RowBox[{"(", RowBox[{ RowBox[{"A", "[", RowBox[{"[", RowBox[{"k", ",", "k"}], "]"}], "]"}], "-", "sum"}], ")"}]}]}], ";", "\[IndentingNewLine]", RowBox[{"For", "[", RowBox[{ RowBox[{"j", "=", RowBox[{"k", "+", "1"}]}], ",", RowBox[{"j", "\[LessEqual]", "n"}], ",", RowBox[{"j", "++"}], ",", " ", RowBox[{"(*", RowBox[{ "Find", " ", "all", " ", "the", " ", "elements", " ", "below", " ", "the", " ", "main", " ", "diagonal"}], "*)"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"sum", "=", "0"}], ";", "\[IndentingNewLine]", RowBox[{"For", "[", RowBox[{ RowBox[{"i", "=", "1"}], ",", RowBox[{"i", "\[LessEqual]", RowBox[{"k", "-", "1"}]}], ",", RowBox[{"i", "++"}], ",", "\[IndentingNewLine]", RowBox[{"sum", "+=", RowBox[{ RowBox[{"L", "[", RowBox[{"[", RowBox[{"k", ",", "i"}], "]"}], "]"}], RowBox[{"L", "[", RowBox[{"[", RowBox[{"j", ",", "i"}], "]"}], "]"}]}]}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"L", "[", RowBox[{"[", RowBox[{"j", ",", "k"}], "]"}], "]"}], "=", RowBox[{ FractionBox["1", RowBox[{"L", "[", RowBox[{"[", RowBox[{"k", ",", "k"}], "]"}], "]"}]], RowBox[{"(", RowBox[{ RowBox[{"A", "[", RowBox[{"[", RowBox[{"k", ",", "j"}], "]"}], "]"}], "-", "sum"}], ")"}]}]}]}]}], "\[IndentingNewLine]", "]"}]}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", "L"}], "\[IndentingNewLine]", ")"}]}], "\[IndentingNewLine]", RowBox[{ RowBox[{"LUSolve", "[", RowBox[{"L_", ",", "U_", ",", "b_"}], "]"}], ":=", RowBox[{"(", "\[IndentingNewLine]", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"x", ",", "y"}], "}"}], ",", " ", RowBox[{"(*", RowBox[{ "Define", " ", "x", " ", "and", " ", "y", " ", "to", " ", "be", " ", "local", " ", "variables"}], "*)"}], "\[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[{"sum", "=", "0"}], ";", "\[IndentingNewLine]", RowBox[{"For", "[", RowBox[{ RowBox[{"j", "=", "1"}], ",", RowBox[{"j", "\[LessEqual]", RowBox[{"i", "-", "1"}]}], ",", RowBox[{"j", "++"}], ",", "\[IndentingNewLine]", RowBox[{"sum", "+=", RowBox[{ RowBox[{"L", "[", RowBox[{"[", RowBox[{"i", ",", "j"}], "]"}], "]"}], RowBox[{"y", "[", RowBox[{"[", "j", "]"}], "]"}]}]}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"y", "[", RowBox[{"[", "i", "]"}], "]"}], "=", RowBox[{ FractionBox["1", RowBox[{"L", "[", RowBox[{"[", RowBox[{"i", ",", "i"}], "]"}], "]"}]], RowBox[{"(", RowBox[{ RowBox[{"b", "[", RowBox[{"[", "i", "]"}], "]"}], "-", "sum"}], ")"}]}]}]}]}], "\[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[{"sum", "=", "0"}], ";", "\[IndentingNewLine]", RowBox[{"For", "[", RowBox[{ RowBox[{"j", "=", RowBox[{"i", "+", "1"}]}], ",", RowBox[{"j", "\[LessEqual]", "n"}], ",", RowBox[{"j", "++"}], ",", "\[IndentingNewLine]", RowBox[{"sum", "+=", RowBox[{ RowBox[{"U", "[", RowBox[{"[", RowBox[{"i", ",", "j"}], "]"}], "]"}], RowBox[{"x", "[", RowBox[{"[", "j", "]"}], "]"}]}]}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"x", "[", RowBox[{"[", "i", "]"}], "]"}], "=", RowBox[{ FractionBox["1", RowBox[{"U", "[", RowBox[{"[", RowBox[{"i", ",", "i"}], "]"}], "]"}]], RowBox[{"(", RowBox[{ RowBox[{"y", "[", RowBox[{"[", "i", "]"}], "]"}], "-", "sum"}], ")"}]}]}]}]}], "\[IndentingNewLine]", "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", "x"}]}], "\[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.6558002898616323`*^9, 3.6558003467928886`*^9}, {3.655801062470823*^9, 3.6558010922085238`*^9}, {3.6558012685996127`*^9, 3.655801272430832*^9}, { 3.65607152594147*^9, 3.6560716019998207`*^9}, {3.656071642224121*^9, 3.6560721417216907`*^9}, {3.6560722196281466`*^9, 3.656072224283413*^9}, 3.656072399894457*^9, {3.656073073059471*^9, 3.65607307514359*^9}, { 3.688455147842004*^9, 3.68845518106205*^9}, {3.688455223272109*^9, 3.688455233862124*^9}, {3.754031528371827*^9, 3.7540316013002625`*^9}, { 3.7872855405608215`*^9, 3.7872856342171783`*^9}}] }, Open ]] }, WindowSize->{1366, 685}, WindowMargins->{{-8, Automatic}, {Automatic, -8}}, 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, 97, 1, 70, "Section"], Cell[679, 25, 8327, 207, 1061, "Input"] }, Open ]] } ] *)