(* Content-type: application/vnd.wolfram.mathematica *) (*** Wolfram Notebook File ***) (* http://www.wolfram.com/nb *) (* CreatedBy='Mathematica 10.1' *) (*CacheID: 234*) (* Internal cache information: NotebookFileLineBreakTest NotebookFileLineBreakTest NotebookDataPosition[ 158, 7] NotebookDataLength[ 6289, 168] NotebookOptionsPosition[ 6044, 155] NotebookOutlinePosition[ 6390, 170] CellTagsIndexPosition[ 6347, 167] WindowFrame->Normal*) (* Beginning of Notebook Content *) Notebook[{ Cell[BoxData[ RowBox[{ RowBox[{"jacobiIteration", "[", RowBox[{"A_", ",", "b_"}], "]"}], ":=", RowBox[{"(", "\[IndentingNewLine]", RowBox[{ RowBox[{"n", "=", RowBox[{"Length", "[", "A", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"\[Epsilon]", "=", "0.0001"}], ";", "\[IndentingNewLine]", RowBox[{"maxIter", "=", "100"}], ";", "\[IndentingNewLine]", RowBox[{"iter", "=", "1"}], ";", "\[IndentingNewLine]", RowBox[{"xOld", "=", RowBox[{"Table", "[", RowBox[{"0", ",", RowBox[{"{", RowBox[{"i", ",", "n"}], "}"}]}], "]"}]}], ";", " ", "\[IndentingNewLine]", RowBox[{"xNew", "=", RowBox[{"Table", "[", RowBox[{"0", ",", RowBox[{"{", RowBox[{"i", ",", "n"}], "}"}]}], "]"}]}], ";", " ", "\[IndentingNewLine]", RowBox[{"For", "[", RowBox[{ RowBox[{"i", "=", "1"}], ",", RowBox[{"i", "\[LessEqual]", "n"}], ",", RowBox[{"i", "++"}], ",", " ", RowBox[{"(*", RowBox[{"Compute", " ", "the", " ", "first", " ", "approximation", " ", SubscriptBox["x", "1"]}], "*)"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"sum", " ", "=", " ", "0"}], ";", "\[IndentingNewLine]", RowBox[{"For", "[", RowBox[{ RowBox[{"j", "=", "1"}], ",", RowBox[{"j", "\[LessEqual]", "n"}], ",", RowBox[{"j", "++"}], ",", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"j", "\[NotEqual]", "i"}], ",", "\[IndentingNewLine]", RowBox[{"sum", "+=", RowBox[{ RowBox[{"A", "[", RowBox[{"[", RowBox[{"i", ",", "j"}], "]"}], "]"}], RowBox[{"xOld", "[", RowBox[{"[", "j", "]"}], "]"}]}]}]}], "\[IndentingNewLine]", "]"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"xNew", "[", RowBox[{"[", "i", "]"}], "]"}], "=", RowBox[{ FractionBox[ RowBox[{"-", "sum"}], RowBox[{"A", "[", RowBox[{"[", RowBox[{"i", ",", "i"}], "]"}], "]"}]], "+", FractionBox[ RowBox[{"b", "[", RowBox[{"[", "i", "]"}], "]"}], RowBox[{"A", "[", RowBox[{"[", RowBox[{"i", ",", "i"}], "]"}], "]"}]]}]}]}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"(*", RowBox[{ "Compute", " ", "the", " ", "successive", " ", "approximations", " ", "until", " ", "the", " ", "desired", " ", "accuracy", " ", "is", " ", "reached", " ", "or", "\[IndentingNewLine]", "the", " ", "maximum", " ", "number", " ", "of", " ", "iterations", " ", "is", " ", "reached"}], "*)"}], "\[IndentingNewLine]", RowBox[{"While", "[", RowBox[{ RowBox[{ RowBox[{ RowBox[{ RowBox[{"Norm", "[", RowBox[{"xNew", "-", "xOld"}], "]"}], "/", RowBox[{"Norm", "[", "xNew", "]"}]}], "\[GreaterEqual]", "\[Epsilon]"}], "&&", RowBox[{"iter", "<", "maxIter"}]}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"xOld", "=", "xNew"}], ";", " ", "\[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]", "n"}], ",", RowBox[{"j", "++"}], ",", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"j", "\[NotEqual]", "i"}], ",", "\[IndentingNewLine]", RowBox[{"sum", "+=", RowBox[{ RowBox[{"A", "[", RowBox[{"[", RowBox[{"i", ",", "j"}], "]"}], "]"}], RowBox[{"xOld", "[", RowBox[{"[", "j", "]"}], "]"}]}]}]}], "\[IndentingNewLine]", "]"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"xNew", "[", RowBox[{"[", "i", "]"}], "]"}], "=", RowBox[{ FractionBox[ RowBox[{"-", "sum"}], RowBox[{"A", "[", RowBox[{"[", RowBox[{"i", ",", "i"}], "]"}], "]"}]], "+", FractionBox[ RowBox[{"b", "[", RowBox[{"[", "i", "]"}], "]"}], RowBox[{"A", "[", RowBox[{"[", RowBox[{"i", ",", "i"}], "]"}], "]"}]]}]}]}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"iter", "++"}]}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"xNew", ",", "iter"}], "}"}]}], "\[IndentingNewLine]", ")"}]}]], "Input", CellChangeTimes->{{3.6597795644573717`*^9, 3.659779564727372*^9}, { 3.659779672121047*^9, 3.6597797636082797`*^9}, {3.659779803876583*^9, 3.6597800507907057`*^9}, {3.6598531753407445`*^9, 3.6598533105899734`*^9}, { 3.66067880389182*^9, 3.6606788100151534`*^9}, {3.754024485730534*^9, 3.7540246231423936`*^9}, {3.7872856795607715`*^9, 3.7872857409722843`*^9}}] }, WindowSize->{1026, 729}, WindowMargins->{{Automatic, 139}, {114, 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[558, 20, 5482, 133, 786, "Input"] } ] *) (* End of internal cache information *)