(* 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[ 10498, 342] NotebookOptionsPosition[ 9313, 296] NotebookOutlinePosition[ 9740, 313] CellTagsIndexPosition[ 9697, 310] WindowFrame->Normal ContainsDynamic->False*) (* Beginning of Notebook Content *) Notebook[{ Cell[CellGroupData[{ Cell["\<\ Math 481A California State University, Northridge Horner's Method\ \>", "Section"], Cell["\[Copyright]2005 BE Shapiro", "Subsection"] }, Open ]], Cell[CellGroupData[{ Cell["Implementation of Horner", "Section"], Cell[BoxData[ RowBox[{ RowBox[{"HornersMethod", "[", RowBox[{ RowBox[{"A_", "?", "ListQ"}], ",", "x0_"}], "]"}], ":=", " ", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"z", ",", "y", ",", "a"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"a", "=", "A"}], ";", "\[IndentingNewLine]", RowBox[{"y", "=", RowBox[{"z", "=", RowBox[{"Last", "[", "a", "]"}]}]}], ";", "\[IndentingNewLine]", RowBox[{"a", "=", RowBox[{"Most", "[", "a", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"While", "[", RowBox[{ RowBox[{ RowBox[{"Length", "[", "a", "]"}], ">", "1"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"y", "=", RowBox[{ RowBox[{"x0", "*", "y"}], "+", RowBox[{"Last", "[", "a", "]"}]}]}], ";", "\[IndentingNewLine]", RowBox[{"z", "=", RowBox[{ RowBox[{"x0", "*", "z"}], "+", "y"}]}], ";", "\[IndentingNewLine]", RowBox[{"a", "=", RowBox[{"Most", "[", "a", "]"}]}], ";"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"y", "=", RowBox[{ RowBox[{"x0", "*", "y"}], "+", RowBox[{"Last", "[", "a", "]"}]}]}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{"\"\\"", RowBox[{"A", ".", RowBox[{"(", RowBox[{"\"\\"", "^", RowBox[{"Range", "[", RowBox[{"0", ",", RowBox[{ RowBox[{"Length", "[", "A", "]"}], "-", "1"}]}], "]"}]}], ")"}]}]}], "]"}], ";", " ", "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{ "\"\\"", ",", "x0", ",", "\"\<)=\>\"", ",", "y", ",", "\"\<\\n\>\"", ",", "\"\\"", ",", "x0", ",", "\"\<)=\>\"", ",", "z"}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"Return", "[", RowBox[{"{", RowBox[{"y", ",", "z"}], "}"}], "]"}]}]}], "\[IndentingNewLine]", "]"}]}]], "Input", CellChangeTimes->{{3.3986013757078457`*^9, 3.398601379111836*^9}}] }, Open ]], Cell[CellGroupData[{ Cell["Horner's Method Examples", "Section"], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"HornersMethod", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"-", "4"}], ",", "3", ",", RowBox[{"-", "3"}], ",", "0", ",", "2"}], "}"}], ",", " ", RowBox[{"-", "2"}]}], "]"}]], "Input", CellChangeTimes->{{3.3986013839242983`*^9, 3.3986013860731916`*^9}}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"\<\"P(x)=\"\>", " ", RowBox[{"(", RowBox[{ RowBox[{"-", "4"}], "+", RowBox[{"3", " ", "\<\"x\"\>"}], "-", RowBox[{"3", " ", SuperscriptBox["\<\"x\"\>", "2"]}], "+", RowBox[{"2", " ", SuperscriptBox["\<\"x\"\>", "4"]}]}], ")"}]}]], "Print", CellChangeTimes->{3.398601402297917*^9}], Cell[BoxData[ InterpretationBox[ RowBox[{"\<\"P(\"\>", "\[InvisibleSpace]", RowBox[{"-", "2"}], "\[InvisibleSpace]", "\<\")=\"\>", "\[InvisibleSpace]", "10", "\[InvisibleSpace]", "\<\"\\n\"\>", "\[InvisibleSpace]", "\<\"P'(\"\>", "\[InvisibleSpace]", RowBox[{"-", "2"}], "\[InvisibleSpace]", "\<\")=\"\>", "\[InvisibleSpace]", RowBox[{"-", "49"}]}], SequenceForm["P(", -2, ")=", 10, "\n", "P'(", -2, ")=", -49], Editable->False]], "Print", CellChangeTimes->{3.3986014023015547`*^9}] }, Open ]], Cell[BoxData[ RowBox[{"{", RowBox[{"10", ",", RowBox[{"-", "49"}]}], "}"}]], "Output", CellChangeTimes->{3.398601402304538*^9}] }, Open ]], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"HornersMethod", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"-", "5"}], ",", "0", ",", RowBox[{"-", "2"}], ",", "1"}], "}"}], ",", "1"}], "]"}]], "Input", CellChangeTimes->{3.3986013942450027`*^9}], Cell[CellGroupData[{ Cell[BoxData[ RowBox[{"\<\"P(x)=\"\>", " ", RowBox[{"(", RowBox[{ RowBox[{"-", "5"}], "-", RowBox[{"2", " ", SuperscriptBox["\<\"x\"\>", "2"]}], "+", SuperscriptBox["\<\"x\"\>", "3"]}], ")"}]}]], "Print", CellChangeTimes->{3.39860140465376*^9}], Cell[BoxData[ InterpretationBox[ RowBox[{"\<\"P(\"\>", "\[InvisibleSpace]", "1", "\[InvisibleSpace]", "\<\")=\"\>", "\[InvisibleSpace]", RowBox[{"-", "6"}], "\[InvisibleSpace]", "\<\"\\n\"\>", "\[InvisibleSpace]", "\<\"P'(\"\>", "\[InvisibleSpace]", "1", "\[InvisibleSpace]", "\<\")=\"\>", "\[InvisibleSpace]", RowBox[{"-", "1"}]}], SequenceForm["P(", 1, ")=", -6, "\n", "P'(", 1, ")=", -1], Editable->False]], "Print", CellChangeTimes->{3.3986014046569366`*^9}] }, Open ]], Cell[BoxData[ RowBox[{"{", RowBox[{ RowBox[{"-", "6"}], ",", RowBox[{"-", "1"}]}], "}"}]], "Output", CellChangeTimes->{3.398601404660075*^9}] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Combine Horner's Method with Newton's Method", "Section"], Cell[BoxData[ RowBox[{ RowBox[{"Horner2", "[", RowBox[{ RowBox[{"A_", "?", "ListQ"}], ",", "x0_"}], "]"}], ":=", " ", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"z", ",", "y", ",", "a"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"a", "=", "A"}], ";", RowBox[{"y", "=", RowBox[{"z", "=", RowBox[{"Last", "[", "a", "]"}]}]}], ";", RowBox[{"a", "=", RowBox[{"Most", "[", "a", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"While", "[", RowBox[{ RowBox[{ RowBox[{"Length", "[", "a", "]"}], ">", "1"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"y", "=", RowBox[{ RowBox[{"x0", "*", "y"}], "+", RowBox[{"Last", "[", "a", "]"}]}]}], ";", "\[IndentingNewLine]", RowBox[{"z", "=", RowBox[{ RowBox[{"x0", "*", "z"}], "+", "y"}]}], ";", "\[IndentingNewLine]", RowBox[{"a", "=", RowBox[{"Most", "[", "a", "]"}]}], ";"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"y", "=", RowBox[{ RowBox[{"x0", "*", "y"}], "+", RowBox[{"Last", "[", "a", "]"}]}]}], ";", "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{ "\"\\"", ",", "x0", ",", "\"\<)=\>\"", ",", "y", ",", "\"\<\\n\>\"", ",", "\"\\"", ",", "x0", ",", "\"\<)=\>\"", ",", "z"}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"Return", "[", RowBox[{"{", RowBox[{"y", ",", "z"}], "}"}], "]"}]}]}], "\[IndentingNewLine]", "]"}]}]], "Input"], Cell[BoxData[ RowBox[{ RowBox[{"NewtonHorner", "[", RowBox[{ RowBox[{"A_", "?", "ListQ"}], ",", " ", "p0_", ",", RowBox[{"\[Epsilon]_:", "0.001"}], ",", RowBox[{"Nmax_:", "10"}]}], "]"}], ":=", "\[IndentingNewLine]", " ", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"\[Delta]", ",", " ", "i", ",", "p", ",", "px0", ",", "ppxo"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"{", RowBox[{"px0", ",", "ppx0"}], "}"}], "=", RowBox[{"Horner2", "[", RowBox[{"A", ",", "p0"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"p", "=", "p0"}], ";", " ", RowBox[{"\[Delta]", "=", RowBox[{"px0", "/", "ppx0"}]}], ";", " ", RowBox[{"i", "=", "0"}], ";", "\[IndentingNewLine]", RowBox[{"While", "[", RowBox[{ RowBox[{ RowBox[{ RowBox[{"Abs", "[", "\[Delta]", "]"}], ">", "\[Epsilon]"}], " ", "\[And]", " ", RowBox[{"(", RowBox[{ RowBox[{"i", "++"}], "<", "Nmax"}], ")"}]}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"p", "=", RowBox[{"p", "-", "\[Delta]"}]}], ";", "\[IndentingNewLine]", RowBox[{"Print", "[", RowBox[{ "\"\\"", ",", "i", ",", " ", "\"\< p=\>\"", ",", "p"}], "]"}], ";", RowBox[{ RowBox[{"{", RowBox[{"px0", ",", "ppx0"}], "}"}], "=", RowBox[{"Horner2", "[", RowBox[{"A", ",", "p"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"\[Delta]", "=", RowBox[{"px0", "/", "ppx0"}]}], ";"}]}], "\[IndentingNewLine]", "]"}], ";", "\[IndentingNewLine]", RowBox[{"Return", "[", RowBox[{"{", RowBox[{"i", ",", "p"}], "}"}], "]"}], ";"}]}], " ", "\[IndentingNewLine]", "]"}]}]], "Input"] }, Open ]] }, WindowSize->{1335, 1085}, WindowMargins->{{Automatic, 249}, {18, Automatic}}, Magnification->1, 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[CellGroupData[{ Cell[590, 23, 92, 4, 112, "Section"], Cell[685, 29, 49, 0, 34, "Subsection"] }, Open ]], Cell[CellGroupData[{ Cell[771, 34, 43, 0, 72, "Section"], Cell[817, 36, 2155, 57, 248, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[3009, 98, 43, 0, 72, "Section"], Cell[CellGroupData[{ Cell[3077, 102, 304, 8, 37, "Input"], Cell[CellGroupData[{ Cell[3406, 114, 343, 10, 26, "Print"], Cell[3752, 126, 514, 11, 36, "Print"] }, Open ]], Cell[4281, 140, 136, 4, 37, "Output"] }, Open ]], Cell[CellGroupData[{ Cell[4454, 149, 244, 7, 37, "Input"], Cell[CellGroupData[{ Cell[4723, 160, 273, 8, 26, "Print"], Cell[4999, 170, 490, 10, 36, "Print"] }, Open ]], Cell[5504, 183, 153, 5, 37, "Output"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[5706, 194, 63, 0, 72, "Section"], Cell[5772, 196, 1645, 45, 188, "Input"], Cell[7420, 243, 1877, 50, 188, "Input"] }, Open ]] } ] *) (* End of internal cache information *)