(* Content-type: application/mathematica *) (*** Wolfram Notebook File ***) (* http://www.wolfram.com/nb *) (* CreatedBy='Mathematica 6.0' *) (*CacheID: 234*) (* Internal cache information: NotebookFileLineBreakTest NotebookFileLineBreakTest NotebookDataPosition[ 145, 7] NotebookDataLength[ 8996, 298] NotebookOptionsPosition[ 7886, 255] NotebookOutlinePosition[ 8314, 272] CellTagsIndexPosition[ 8271, 269] WindowFrame->Normal ContainsDynamic->False*) (* Beginning of Notebook Content *) Notebook[{ Cell["\<\ Math 481A California State University Northridge Gaussian Elimination\ \>", "Section"], Cell["\[Copyright] 2007 B.E. Shapiro", "Section"], Cell[CellGroupData[{ Cell["Row Reduction", "Section"], Cell[BoxData[ RowBox[{ RowBox[{"reduce", "[", RowBox[{"A_", ",", " ", "b_"}], "]"}], ":=", " ", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{ "n", ",", " ", "j", ",", " ", "k", ",", " ", "Aprime", ",", " ", "bprime", ",", "m", ",", " ", "row"}], "}"}], ",", " ", "\[IndentingNewLine]", RowBox[{ RowBox[{"n", "=", RowBox[{"Length", "[", "b", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"Aprime", "=", RowBox[{"{", "}"}]}], ";", " ", RowBox[{"bprime", "=", RowBox[{"{", "}"}]}], ";", "\[IndentingNewLine]", RowBox[{"For", "[", RowBox[{ RowBox[{"k", "=", "2"}], ",", " ", RowBox[{"k", "\[LessEqual]", " ", "n"}], " ", ",", " ", RowBox[{"k", "++"}], ",", " ", "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{ RowBox[{"m", "=", RowBox[{ RowBox[{"A", "[", RowBox[{"[", RowBox[{"k", ",", "1"}], "]"}], "]"}], "/", RowBox[{"A", "[", RowBox[{"[", RowBox[{"1", ",", " ", "1"}], "]"}], "]"}]}]}], ";", " ", "\[IndentingNewLine]", RowBox[{"row", "=", RowBox[{"{", "}"}]}], ";", "\[IndentingNewLine]", RowBox[{"For", "[", RowBox[{ RowBox[{"j", "=", "2"}], ",", " ", RowBox[{"j", "\[LessEqual]", " ", "n"}], ",", " ", RowBox[{"j", "++"}], ",", " ", "\[IndentingNewLine]", RowBox[{ RowBox[{"AppendTo", "[", RowBox[{"row", ",", " ", RowBox[{ RowBox[{"A", "[", RowBox[{"[", RowBox[{"k", ",", "j"}], "]"}], "]"}], "-", " ", RowBox[{"m", "*", " ", RowBox[{"A", "[", RowBox[{"[", RowBox[{"1", ",", " ", "j"}], "]"}], "]"}]}]}]}], "]"}], ";"}]}], " ", "\[IndentingNewLine]", "]"}], ";", " ", "\[IndentingNewLine]", RowBox[{"AppendTo", "[", RowBox[{"Aprime", ",", " ", "row"}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"AppendTo", "[", RowBox[{"bprime", ",", " ", RowBox[{ RowBox[{"b", "[", RowBox[{"[", "k", "]"}], "]"}], "-", RowBox[{"m", "*", RowBox[{"b", "[", RowBox[{"[", "1", "]"}], "]"}]}]}]}], "]"}], ";"}]}], " ", "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"Return", "[", RowBox[{"{", RowBox[{"Aprime", ",", " ", "bprime"}], "}"}], "]"}], ";"}]}], "\[IndentingNewLine]", "]"}]}]], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Gaussian Elimination", "Section"], Cell[BoxData[ RowBox[{ RowBox[{"gauss", "[", RowBox[{"A_", ",", " ", "b_"}], "]"}], ":=", " ", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{ "n", ",", " ", "x", ",", " ", "x1", ",", " ", "Aprime", ",", " ", "bprime"}], "}"}], ",", " ", "\[IndentingNewLine]", RowBox[{ RowBox[{"n", "=", RowBox[{"Length", "[", "b", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"x", "=", RowBox[{"{", "}"}]}], ";", " ", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"n", ">", "1"}], ",", " ", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"{", RowBox[{"Aprime", ",", " ", "bprime"}], "}"}], "=", RowBox[{"reduce", "[", RowBox[{"A", ",", " ", "b"}], "]"}]}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{"x", "=", RowBox[{"gauss", "[", RowBox[{"Aprime", ",", " ", "bprime"}], "]"}]}], ";"}]}], "\[IndentingNewLine]", "]"}], ";", " ", "\[IndentingNewLine]", RowBox[{"x1", "=", RowBox[{ RowBox[{"b", "[", RowBox[{"[", "1", "]"}], "]"}], "/", RowBox[{"A", "[", RowBox[{"[", RowBox[{"1", ",", "1"}], "]"}], "]"}]}]}], ";", "\[IndentingNewLine]", RowBox[{"For", "[", RowBox[{ RowBox[{"k", "=", "2"}], ",", " ", RowBox[{"k", "\[LessEqual]", " ", "n"}], ",", " ", RowBox[{"k", "++"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"x1", " ", "=", " ", RowBox[{"x1", "-", RowBox[{ RowBox[{"A", "[", RowBox[{"[", RowBox[{"1", ",", "k"}], "]"}], "]"}], RowBox[{ RowBox[{"x", "[", RowBox[{"[", RowBox[{"k", "-", "1"}], "]"}], "]"}], "/", RowBox[{"A", "[", RowBox[{"[", RowBox[{"1", ",", "1"}], "]"}], "]"}]}]}]}]}], ";"}]}], "\[IndentingNewLine]", "]"}], ";", " ", "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{"x", "=", RowBox[{"Prepend", "[", RowBox[{"x", ",", " ", "x1"}], "]"}]}], ";", " ", "\[IndentingNewLine]", RowBox[{"Return", "[", "x", "]"}], ";"}]}], "\[IndentingNewLine]", "]"}]}]], "Input"] }, Open ]], Cell[CellGroupData[{ Cell["Example", "Section"], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"A", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"i", "*", RowBox[{"Sin", "[", RowBox[{"j", "*", "i", "*", RowBox[{"Pi", "/", "27."}]}], "]"}]}], ",", " ", RowBox[{"{", RowBox[{"i", ",", "1", ",", "3"}], "}"}], ",", " ", RowBox[{"{", RowBox[{"j", ",", "1", ",", "3"}], "}"}]}], "]"}]}]], "Input"], Cell[BoxData[ RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{ "0.11609291412523022`", ",", "0.23061587074244014`", ",", "0.34202014332566866`"}], "}"}], ",", RowBox[{"{", RowBox[{ "0.4612317414848803`", ",", "0.8975983604009242`", ",", "1.2855752193730785`"}], "}"}], ",", RowBox[{"{", RowBox[{ "1.026060429977006`", ",", "1.9283628290596178`", ",", "2.5980762113533156`"}], "}"}]}], "}"}]], "Output", CellChangeTimes->{3.39860107807017*^9}] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"b", "=", RowBox[{"{", RowBox[{"3", ",", " ", "17", ",", " ", "5"}], "}"}]}]], "Input"], Cell[BoxData[ RowBox[{"{", RowBox[{"3", ",", "17", ",", "5"}], "}"}]], "Output", CellChangeTimes->{3.398601080987763*^9}] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"gauss", "[", RowBox[{"A", ",", "b"}], "]"}]], "Input"], Cell[BoxData[ RowBox[{"{", RowBox[{ RowBox[{"-", "33612.943188498524`"}], ",", "27351.858586020026`", ",", RowBox[{"-", "7024.580873509571`"}]}], "}"}]], "Output", CellChangeTimes->{3.3986010834431677`*^9}] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"LinearSolve", "[", RowBox[{"A", ",", " ", "b"}], "]"}]], "Input", CellChangeTimes->{{3.39860108522099*^9, 3.398601088819875*^9}}], Cell[BoxData[ RowBox[{"{", RowBox[{ RowBox[{"-", "33612.94318850195`"}], ",", "27351.85858602287`", ",", RowBox[{"-", "7024.580873510327`"}]}], "}"}]], "Output", CellChangeTimes->{{3.3986010864434557`*^9, 3.3986010901649237`*^9}}] }, Open ]] }, Open ]] }, WindowSize->{520, 740}, WindowMargins->{{44, Automatic}, {Automatic, 104}}, ShowSelection->True, FrontEndVersion->"6.0 for Mac OS X x86 (32-bit) (June 19, 2007)", StyleDefinitions->FrontEnd`FileName[{"Creative"}, "PastelColor.nb", CharacterEncoding -> "UTF-8"] ] (* End of Notebook Content *) (* Internal cache information *) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[568, 21, 96, 4, 112, "Section"], Cell[667, 27, 49, 0, 42, "Section"], Cell[CellGroupData[{ Cell[741, 31, 32, 0, 42, "Section"], Cell[776, 33, 2656, 69, 263, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[3469, 107, 39, 0, 72, "Section"], Cell[3511, 109, 2324, 63, 263, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[5872, 177, 26, 0, 72, "Section"], Cell[CellGroupData[{ Cell[5923, 181, 373, 11, 37, "Input"], Cell[6299, 194, 498, 15, 68, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[6834, 214, 117, 3, 37, "Input"], Cell[6954, 219, 126, 3, 37, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[7117, 227, 81, 2, 37, "Input"], Cell[7201, 231, 218, 5, 37, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[7456, 241, 157, 3, 37, "Input"], Cell[7616, 246, 242, 5, 37, "Output"] }, Open ]] }, Open ]] } ] *) (* End of internal cache information *)