(* 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[{"\"\ \"", ",", "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[{
"\"\