(* 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[ 10144, 356]
NotebookOptionsPosition[ 8878, 307]
NotebookOutlinePosition[ 9305, 324]
CellTagsIndexPosition[ 9262, 321]
WindowFrame->Normal
ContainsDynamic->False*)
(* Beginning of Notebook Content *)
Notebook[{
Cell[CellGroupData[{
Cell["\<\
Math 481A
California State University, Northridge
Lagrange Interpolating Polynomials\
\>", "Section"],
Cell["\[Copyright]2007 BE Shapiro", "Subsection",
CellChangeTimes->{{3.398601147762739*^9, 3.398601148354417*^9}}]
}, Open ]],
Cell[CellGroupData[{
Cell[TextData[{
"Lagrange Interpolating Function: Calculates ",
Cell[BoxData[
FormBox[
RowBox[{
SubscriptBox["L",
RowBox[{"n", ",", "0"}]], ",",
SubscriptBox["L",
RowBox[{"n", ",", "1"}]], ",", "...", ",",
SubscriptBox["L",
RowBox[{"n", ",", "n"}]]}], TraditionalForm]]]
}], "Section"],
Cell[BoxData[
RowBox[{
RowBox[{"LagrangeInterpolatingFunctions", "[",
RowBox[{
RowBox[{"{", "xj__", "}"}], ",", "x_"}], "]"}], ":=", " ",
RowBox[{"Module", "[", "\[IndentingNewLine]",
RowBox[{
RowBox[{"{",
RowBox[{
"i", ",", "n", ",", "xi", ",", " ", "xjc", ",", "L", ",", "xgrid", ",",
"num", ",", "den"}], "}"}], ",", "\[IndentingNewLine]",
RowBox[{
RowBox[{"xgrid", " ", "=", " ",
RowBox[{"{", "xj", "}"}]}], ";", "\[IndentingNewLine]",
RowBox[{"n", "=",
RowBox[{"Length", "[", "xgrid", "]"}]}], ";", "\[IndentingNewLine]",
RowBox[{"L", "=",
RowBox[{"{", "}"}]}], ";", "\[IndentingNewLine]",
RowBox[{"For", "[",
RowBox[{
RowBox[{"i", "=", "1"}], ",",
RowBox[{"i", "\[LessEqual]", " ", "n"}], ",", " ",
RowBox[{"i", "++"}], ",", "\[IndentingNewLine]",
RowBox[{
RowBox[{"xi", "=",
RowBox[{"xgrid", "[",
RowBox[{"[", "i", "]"}], "]"}]}], ";", "\[IndentingNewLine]",
RowBox[{"xjc", "=", " ",
RowBox[{"Complement", "[",
RowBox[{"xgrid", ",",
RowBox[{"{", "xi", "}"}]}], "]"}]}], ";", " ",
"\[IndentingNewLine]",
RowBox[{"den", " ", "=", " ",
RowBox[{"Times", "@@",
RowBox[{"(",
RowBox[{
RowBox[{
RowBox[{"(",
RowBox[{"xi", "-", "#"}], ")"}], "&"}], "/@", "xjc"}],
")"}]}]}], ";", "\[IndentingNewLine]",
RowBox[{"num", " ", "=", " ",
RowBox[{"Times", "@@",
RowBox[{"(",
RowBox[{
RowBox[{
RowBox[{"(",
RowBox[{"x", "-", "#"}], ")"}], "&"}], "/@", "xjc"}], ")"}]}]}],
";", "\[IndentingNewLine]",
RowBox[{"L", "=",
RowBox[{"Append", "[",
RowBox[{"L", ",", " ",
RowBox[{"Expand", "[",
RowBox[{"num", "/", "den"}], "]"}]}], "]"}]}], ";"}]}], " ",
"\[IndentingNewLine]", "]"}], ";", " ", "\[IndentingNewLine]",
RowBox[{"Return", "[", "L", "]"}], ";"}]}], "\[IndentingNewLine]",
"]"}]}]], "Input"]
}, Open ]],
Cell[CellGroupData[{
Cell[TextData[{
"Lagrange Interpolating Polynomial: Calculates a Polynomial that fits \
through the points ",
Cell[BoxData[
FormBox[
RowBox[{
RowBox[{"(",
RowBox[{
SubscriptBox["x", "0"], ",",
SubscriptBox["f", "0"]}], ")"}], ",", "\[Ellipsis]", ",",
RowBox[{"(",
RowBox[{
SubscriptBox["x", "n"], ",",
SubscriptBox["f", "n"]}], ")"}]}], TraditionalForm]]]
}], "Section"],
Cell[BoxData[
RowBox[{
RowBox[{
RowBox[{
RowBox[{"LagrangeInterpolatingPolynomial", "[",
RowBox[{"points_", ",", " ", "x_"}], "]"}], ":=", " ",
RowBox[{"Module", "[",
RowBox[{
RowBox[{"{",
RowBox[{"xvalues", ",", " ", "fvalues", ",", " ", "p", ",", "L"}],
"}"}], ",", "\[IndentingNewLine]",
RowBox[{
RowBox[{"If", "[",
RowBox[{
RowBox[{"!",
RowBox[{"ListQ", "[", "points", "]"}]}], ",", " ",
RowBox[{
RowBox[{
"Print", "[", "\"\\"", "]"}], ";",
RowBox[{"Abort", "[", "]"}], ";"}]}], "]"}], ";", " ",
"\[IndentingNewLine]",
RowBox[{"If", "[",
RowBox[{
RowBox[{
RowBox[{"Union", "[",
RowBox[{"Length", "/@", "points"}], "]"}], "\[NotEqual]", " ",
RowBox[{"{", "2", "}"}]}], ",", " ",
RowBox[{
RowBox[{
"Print", "[",
"\"\\"",
"]"}], ";",
RowBox[{"Abort", "[", "]"}], ";"}]}], "\[IndentingNewLine]", "]"}],
";", " ", "\[IndentingNewLine]",
RowBox[{
RowBox[{"{",
RowBox[{"xvalues", ",", " ", "fvalues"}], "}"}], "=",
RowBox[{"Transpose", "[", "points", "]"}]}], ";", " ",
"\[IndentingNewLine]",
RowBox[{"L", "=",
RowBox[{"LagrangeInterpolatingFunctions", "[",
RowBox[{"xvalues", ",", "x"}], "]"}]}], ";", " ",
"\[IndentingNewLine]",
RowBox[{"p", "=",
RowBox[{
RowBox[{"(",
RowBox[{"fvalues", ".", "L"}], ")"}], "//", " ", "Expand"}]}], ";",
"\[IndentingNewLine]",
RowBox[{"Return", "[", "p", "]"}], ";"}]}], "\[IndentingNewLine]",
"]"}]}], ";"}], "\[IndentingNewLine]", "\[IndentingNewLine]"}]], "Input",\
CellChangeTimes->{{3.3992899307946987`*^9, 3.399289931388554*^9}}]
}, Open ]],
Cell[CellGroupData[{
Cell["Reproduce Example 1 of text", "Section"],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{"LagrangeInterpolatingFunctions", "[",
RowBox[{
RowBox[{"{",
RowBox[{"2", ",",
RowBox[{"5", "/", "2"}], ",", " ", "4"}], "}"}], ",", " ", "x"}],
"]"}]], "Input"],
Cell[BoxData[
RowBox[{"{",
RowBox[{
RowBox[{"10", "-",
FractionBox[
RowBox[{"13", " ", "x"}], "2"], "+",
SuperscriptBox["x", "2"]}], ",",
RowBox[{
RowBox[{"-",
FractionBox["32", "3"]}], "+",
RowBox[{"8", " ", "x"}], "-",
FractionBox[
RowBox[{"4", " ",
SuperscriptBox["x", "2"]}], "3"]}], ",",
RowBox[{
FractionBox["5", "3"], "-",
FractionBox[
RowBox[{"3", " ", "x"}], "2"], "+",
FractionBox[
SuperscriptBox["x", "2"], "3"]}]}], "}"}]], "Output",
CellChangeTimes->{3.39860112458241*^9}]
}, Open ]],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{"LagrangeInterpolatingFunctions", "[",
RowBox[{
RowBox[{"{",
RowBox[{"2", ",",
RowBox[{"5", "/", "2"}], ",", " ", "4"}], "}"}], ",", " ",
RowBox[{"1", "/", "3"}]}], "]"}]], "Input"],
Cell[BoxData[
RowBox[{"{",
RowBox[{
FractionBox["143", "18"], ",",
RowBox[{"-",
FractionBox["220", "27"]}], ",",
FractionBox["65", "54"]}], "}"}]], "Output",
CellChangeTimes->{3.398601126626142*^9}]
}, Open ]],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{"LagrangeInterpolatingPolynomial", "[",
RowBox[{
RowBox[{"{",
RowBox[{
RowBox[{"{",
RowBox[{"2", ",",
RowBox[{"1", "/", "2"}]}], "}"}], ",",
RowBox[{"{",
RowBox[{
RowBox[{"5", "/", "2"}], ",",
RowBox[{"2", "/", "5"}]}], "}"}], ",",
RowBox[{"{",
RowBox[{"4", ",",
RowBox[{"1", "/", "4"}]}], "}"}]}], "}"}], ",", " ", "x"}],
"]"}]], "Input"],
Cell[BoxData[
RowBox[{
FractionBox["23", "20"], "-",
FractionBox[
RowBox[{"17", " ", "x"}], "40"], "+",
FractionBox[
SuperscriptBox["x", "2"], "20"]}]], "Output",
CellChangeTimes->{3.3986011294942837`*^9}]
}, Open ]],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{
RowBox[{"1.", "*", "%"}], "//", "Expand"}]], "Input"],
Cell[BoxData[
RowBox[{"1.15`", "\[InvisibleSpace]", "-",
RowBox[{"0.425`", " ", "x"}], "+",
RowBox[{"0.05`", " ",
SuperscriptBox["x", "2"]}]}]], "Output",
CellChangeTimes->{3.398601132398098*^9}]
}, Open ]],
Cell[CellGroupData[{
Cell[BoxData[
RowBox[{"LagrangeInterpolatingPolynomial", "[",
RowBox[{
RowBox[{"{",
RowBox[{
RowBox[{"{",
RowBox[{"2", ",",
RowBox[{"1", "/", "2"}]}], "}"}], ",",
RowBox[{"{",
RowBox[{
RowBox[{"5", "/", "2"}], ",",
RowBox[{"2", "/", "5"}]}], "}"}], ",",
RowBox[{"{",
RowBox[{"4", ",",
RowBox[{"1", "/", "4"}]}], "}"}], ",",
RowBox[{"{",
RowBox[{"5", ",",
RowBox[{"1", "/", "5"}]}], "}"}]}], "}"}], ",", " ", "3"}],
"]"}]], "Input"],
Cell[BoxData[
FractionBox["33", "100"]], "Output",
CellChangeTimes->{3.398601135737607*^9}]
}, Open ]]
}, Open ]]
},
WindowSize->{772, 927},
WindowMargins->{{569, Automatic}, {Automatic, 70}},
Magnification->1.5,
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, 111, 4, 168, "Section"],
Cell[704, 29, 115, 1, 51, "Subsection"]
}, Open ]],
Cell[CellGroupData[{
Cell[856, 35, 324, 11, 140, "Section"],
Cell[1183, 48, 2152, 55, 356, "Input"]
}, Open ]],
Cell[CellGroupData[{
Cell[3372, 108, 424, 14, 167, "Section"],
Cell[3799, 124, 1979, 52, 425, "Input"]
}, Open ]],
Cell[CellGroupData[{
Cell[5815, 181, 46, 0, 108, "Section"],
Cell[CellGroupData[{
Cell[5886, 185, 204, 6, 55, "Input"],
Cell[6093, 193, 572, 20, 82, "Output"]
}, Open ]],
Cell[CellGroupData[{
Cell[6702, 218, 225, 6, 79, "Input"],
Cell[6930, 226, 219, 7, 78, "Output"]
}, Open ]],
Cell[CellGroupData[{
Cell[7186, 238, 445, 15, 79, "Input"],
Cell[7634, 255, 221, 7, 82, "Output"]
}, Open ]],
Cell[CellGroupData[{
Cell[7892, 267, 79, 2, 55, "Input"],
Cell[7974, 271, 207, 5, 59, "Output"]
}, Open ]],
Cell[CellGroupData[{
Cell[8218, 281, 536, 18, 102, "Input"],
Cell[8757, 301, 93, 2, 78, "Output"]
}, Open ]]
}, Open ]]
}
]
*)
(* End of internal cache information *)