(* 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[ 10794, 295] NotebookOptionsPosition[ 10326, 274] NotebookOutlinePosition[ 10671, 289] CellTagsIndexPosition[ 10628, 286] WindowFrame->Normal*) (* Beginning of Notebook Content *) Notebook[{ Cell[BoxData[ RowBox[{ RowBox[{"(*", RowBox[{"Input", " ", "data"}], "*)"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"Gauss", "[", RowBox[{"AInput_", ",", "bInput_"}], "]"}], ":=", RowBox[{"(", "\[IndentingNewLine]", RowBox[{ RowBox[{"A", "=", "AInput"}], ";", "\[IndentingNewLine]", RowBox[{"b", "=", "bInput"}], ";", "\[IndentingNewLine]", RowBox[{"n", "=", RowBox[{"Length", "[", "A", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"(*", RowBox[{"Forward", " ", "elimination"}], "*)"}], "\[IndentingNewLine]", RowBox[{"For", "[", RowBox[{ RowBox[{"i", "=", "1"}], ",", " ", RowBox[{"i", "\[LessEqual]", "n"}], ",", " ", RowBox[{"i", "++"}], ",", "\[IndentingNewLine]", RowBox[{"(*", RowBox[{ "Find", " ", "the", " ", "index", " ", "of", " ", "the", " ", "row", " ", "with", " ", "maximal", " ", "element"}], "*)"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"maxIndex", "=", "i"}], ";", "\[IndentingNewLine]", RowBox[{"For", "[", RowBox[{ RowBox[{"j", "=", RowBox[{"i", "+", "1"}]}], ",", RowBox[{"j", "\[LessEqual]", "n"}], ",", RowBox[{"j", "++"}], ",", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"Abs", "[", RowBox[{"A", "[", RowBox[{"[", RowBox[{"j", ",", "i"}], "]"}], "]"}], "]"}], ">", RowBox[{"Abs", "[", RowBox[{"A", "[", RowBox[{"[", RowBox[{"maxIndex", ",", "i"}], "]"}], "]"}], "]"}]}], ",", "\[IndentingNewLine]", RowBox[{"maxIndex", "=", "j"}]}], "]"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"(*", RowBox[{ "Change", " ", "the", " ", "rows", " ", "with", " ", "indices", " ", "i", " ", "and", " ", "maxIndex"}], "*)"}], "\[IndentingNewLine]", RowBox[{"For", "[", RowBox[{ RowBox[{"j", "=", "i"}], ",", " ", RowBox[{"j", "\[LessEqual]", "n"}], ",", " ", RowBox[{"j", "++"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"temp", "=", RowBox[{"A", "[", RowBox[{"[", RowBox[{"i", ",", "j"}], "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"A", "[", RowBox[{"[", RowBox[{"i", ",", "j"}], "]"}], "]"}], "=", RowBox[{"A", "[", RowBox[{"[", RowBox[{"maxIndex", ",", "j"}], "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"A", "[", RowBox[{"[", RowBox[{"maxIndex", ",", "j"}], "]"}], "]"}], "=", "temp"}], ";"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"temp", "=", RowBox[{"b", "[", RowBox[{"[", "i", "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"b", "[", RowBox[{"[", "i", "]"}], "]"}], "=", RowBox[{"b", "[", RowBox[{"[", "maxIndex", "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"b", "[", RowBox[{"[", "maxIndex", "]"}], "]"}], "=", "temp"}], ";", "\[IndentingNewLine]", RowBox[{"(*", RowBox[{ RowBox[{ "Eliminate", " ", "the", " ", "elements", " ", "under", " ", "the", " ", "main", " ", "diagonal", " ", "in", " ", "the", " ", "i"}], "-", RowBox[{"th", " ", "column"}]}], "*)"}], "\[IndentingNewLine]", RowBox[{"For", "[", RowBox[{ RowBox[{"j", "=", RowBox[{"i", "+", "1"}]}], ",", RowBox[{"j", "\[LessEqual]", "n"}], ",", RowBox[{"j", "++"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"l", "=", FractionBox[ RowBox[{"A", "[", RowBox[{"[", RowBox[{"j", ",", "i"}], "]"}], "]"}], RowBox[{"A", "[", RowBox[{"[", RowBox[{"i", ",", "i"}], "]"}], "]"}]]}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"A", "[", RowBox[{"[", RowBox[{"j", ",", "i"}], "]"}], "]"}], "=", "0"}], ";", "\[IndentingNewLine]", RowBox[{"For", "[", RowBox[{ RowBox[{"k", "=", RowBox[{"i", "+", "1"}]}], ",", RowBox[{"k", "\[LessEqual]", "n"}], ",", RowBox[{"k", "++"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"A", "[", RowBox[{"[", RowBox[{"j", ",", "k"}], "]"}], "]"}], "=", RowBox[{ RowBox[{"A", "[", RowBox[{"[", RowBox[{"j", ",", "k"}], "]"}], "]"}], "-", RowBox[{"l", "*", RowBox[{"A", "[", RowBox[{"[", RowBox[{"i", ",", "k"}], "]"}], "]"}]}]}]}], ";"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"b", "[", RowBox[{"[", "j", "]"}], "]"}], "=", RowBox[{ RowBox[{"b", "[", RowBox[{"[", "j", "]"}], "]"}], "-", RowBox[{"l", "*", RowBox[{"b", "[", RowBox[{"[", "i", "]"}], "]"}]}]}]}], ";"}]}], "\[IndentingNewLine]", "]"}]}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"(*", RowBox[{"Backward", " ", "substitution"}], "*)"}], "\[IndentingNewLine]", RowBox[{"x", "=", RowBox[{"Table", "[", RowBox[{"0", ",", RowBox[{"{", RowBox[{"i", ",", "n"}], "}"}]}], "]"}]}], " ", ";", RowBox[{"(*", RowBox[{ RowBox[{"We", " ", "create", " ", "a", " ", "list"}], ",", " ", RowBox[{ "in", " ", "which", " ", "we", " ", "shall", " ", "keep", " ", "the", " ", "values", " ", "of", " ", "the", " ", "unknowns"}]}], "*)"}], "\[IndentingNewLine]", RowBox[{"For", "[", RowBox[{ RowBox[{"i", "=", "n"}], ",", RowBox[{"i", "\[GreaterEqual]", "1"}], ",", " ", RowBox[{"i", "--"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"x", "[", RowBox[{"[", "i", "]"}], "]"}], "=", FractionBox[ RowBox[{ RowBox[{"b", "[", RowBox[{"[", "i", "]"}], "]"}], "-", RowBox[{"Sum", "[", RowBox[{ RowBox[{ RowBox[{"A", "[", RowBox[{"[", RowBox[{"i", ",", "j"}], "]"}], "]"}], RowBox[{"x", "[", RowBox[{"[", "j", "]"}], "]"}]}], ",", RowBox[{"{", RowBox[{"j", ",", RowBox[{"i", "+", "1"}], ",", "n"}], "}"}]}], "]"}]}], RowBox[{"A", "[", RowBox[{"[", RowBox[{"i", ",", "i"}], "]"}], "]"}]]}]}], "\[IndentingNewLine]", "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", "x"}], "\[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.6569199724002066`*^9, 3.6569202508235965`*^9}, {3.7179098090312643`*^9, 3.7179098249771767`*^9}, {3.7528541352733684`*^9, 3.7528541998198404`*^9}}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"Gauss", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"1", ",", "1", ",", "1"}], "}"}], ",", RowBox[{"{", RowBox[{"5", ",", "2", ",", "2"}], "}"}], ",", RowBox[{"{", RowBox[{"1", ",", "2", ",", "3"}], "}"}]}], "}"}], ",", RowBox[{"{", RowBox[{"0", ",", "1", ",", "2"}], "}"}]}], "]"}]], "Input", CellChangeTimes->{{3.7528542200627337`*^9, 3.752854255925873*^9}}], Cell[BoxData[ RowBox[{"{", RowBox[{ FractionBox["1", "3"], ",", RowBox[{"-", FractionBox["8", "3"]}], ",", FractionBox["7", "3"]}], "}"}]], "Output", CellChangeTimes->{{3.7528542376477404`*^9, 3.7528542565781293`*^9}}] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"LinearSolve", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"1", ",", "1", ",", "1"}], "}"}], ",", RowBox[{"{", RowBox[{"5", ",", "2", ",", "2"}], "}"}], ",", RowBox[{"{", RowBox[{"1", ",", "2", ",", "3"}], "}"}]}], "}"}], ",", RowBox[{"{", RowBox[{"0", ",", "1", ",", "2"}], "}"}]}], "]"}]], "Input", CellChangeTimes->{{3.752854239125778*^9, 3.7528542608008475`*^9}}], Cell[BoxData[ RowBox[{"{", RowBox[{ FractionBox["1", "3"], ",", RowBox[{"-", FractionBox["8", "3"]}], ",", FractionBox["7", "3"]}], "}"}]], "Output", CellChangeTimes->{{3.7528542487899475`*^9, 3.7528542613703194`*^9}}] }, Open ]] }, WindowSize->{944, 982}, WindowMargins->{{0, Automatic}, {Automatic, 0}}, FrontEndVersion->"11.0 for Microsoft Windows (64-bit) (September 21, 2016)", StyleDefinitions->"Default.nb" ] (* End of Notebook Content *) (* Internal cache information *) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[557, 20, 8289, 198, 939, "Input"], Cell[CellGroupData[{ Cell[8871, 222, 456, 13, 30, "Input"], Cell[9330, 237, 239, 7, 45, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[9606, 249, 462, 13, 30, "Input"], Cell[10071, 264, 239, 7, 78, "Output"] }, Open ]] } ] *)