(* 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[ 15103, 437] NotebookOptionsPosition[ 14440, 411] NotebookOutlinePosition[ 14904, 429] CellTagsIndexPosition[ 14861, 426] WindowFrame->Normal*) (* Beginning of Notebook Content *) Notebook[{ Cell[CellGroupData[{ Cell[TextData[{ "Random Riemann\n", StyleBox["creating random Riemann sums", FontSize->18] }], "Title", TextAlignment->Center, FontSize->36], Cell["\<\ by Ruth Dover Illinois Mathematics and Science Academy\ \>", "Subsubtitle", CellChangeTimes->{{3.6931829976559343`*^9, 3.693183009366613*^9}}], Cell[TextData[{ "\tThe basic command for this notebook is ", StyleBox["RandomRiemann[func,{x,xmin,xmax},n]", "Input"], ", where ", StyleBox["n", FontSlant->"Italic"], " is the number of subintervals. These intervals and the values of ", Cell[BoxData[ FormBox[ SubscriptBox["c", "i"], TraditionalForm]]], "are randomly chosen. The value of the Riemann sum ", Cell[BoxData[ FormBox[ RowBox[{ UnderoverscriptBox["\[Sum]", RowBox[{"i", "=", "1"}], "n"], RowBox[{ RowBox[{"f", "(", SubscriptBox["c", "i"], ")"}], " ", SubscriptBox[ StyleBox[ RowBox[{"\[CapitalDelta]", StyleBox["x", FontSlant->"Italic"]}]], "i"]}]}], TraditionalForm]]], " is shown along with the actual value of the integral ", Cell[BoxData[ FormBox[ RowBox[{ SubsuperscriptBox["\[Integral]", StyleBox["xmin", FontSlant->"Italic"], StyleBox["xmax", FontSlant->"Italic"]], RowBox[{ RowBox[{"f", "(", "x", ")"}], " ", RowBox[{"\[DifferentialD]", "x"}]}]}], TraditionalForm]]], ". Graphics options may be added to the command as desired." }], "Text"], Cell[BoxData[ RowBox[{"RandomRiemann", "[", RowBox[{ RowBox[{ SuperscriptBox["x", "3"], "-", RowBox[{"6", "x"}], "+", "2"}], ",", " ", RowBox[{"{", RowBox[{"x", ",", " ", RowBox[{"-", "3"}], ",", " ", "3"}], "}"}], ",", " ", "12"}], "]"}]], "Input", CellChangeTimes->{{3.3950944234064083`*^9, 3.39509442349678*^9}}], Cell[CellGroupData[{ Cell["initialization", "Section"], Cell[BoxData[{ RowBox[{ RowBox[{"Clear", "[", RowBox[{"x", ",", "f", ",", " ", "RandomRiemann"}], "]"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{ RowBox[{"RandomRiemann", "::", "usage"}], "=", "\"\\""}], ";"}], "\[IndentingNewLine]"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"RandomRiemann", "[", RowBox[{"g_", ",", RowBox[{"{", RowBox[{"u_", ",", " ", "xmin_", ",", " ", "xmax_"}], "}"}], ",", " ", "n_", ",", " ", "opts___"}], "]"}], ":=", "\[IndentingNewLine]", RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{ "plfunc", ",", " ", "ylow", ",", " ", "ymin", ",", " ", "endpts", ",", " ", "innerpts", ",", " ", "subints", ",", " ", "cvals", ",", " ", "delx", ",", " ", "rect", ",", " ", "redrects", ",", " ", "vertseg", ",", " ", "verts", ",", " ", "clab", ",", " ", "ctext", ",", " ", "plf", ",", "realint", ",", " ", "area"}], " ", "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"f", "[", "x_", "]"}], ":=", RowBox[{ RowBox[{"Function", "[", RowBox[{"u", ",", " ", RowBox[{"Evaluate", "[", "g", "]"}]}], "]"}], "[", "x", "]"}]}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{"plfunc", "=", RowBox[{"Plot", "[", RowBox[{ RowBox[{"f", "[", "x", "]"}], ",", " ", RowBox[{"{", RowBox[{"x", ",", " ", "xmin", ",", " ", "xmax"}], "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"ylow", "=", RowBox[{ RowBox[{"PlotRange", "[", "plfunc", "]"}], "[", RowBox[{"[", RowBox[{"2", ",", " ", "1"}], "]"}], "]"}]}], ";", " ", RowBox[{"yhi", "=", RowBox[{ RowBox[{"PlotRange", "[", "plfunc", "]"}], "[", RowBox[{"[", RowBox[{"2", ",", "2"}], "]"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"ymin", "=", RowBox[{"Min", "[", RowBox[{"ylow", ",", " ", RowBox[{"0", "-", RowBox[{ RowBox[{"(", RowBox[{"1", "/", "20"}], ")"}], RowBox[{"(", RowBox[{"yhi", "-", "ylow"}], ")"}]}]}]}], "]"}]}], ";", " ", RowBox[{"ymax", "=", RowBox[{"Max", "[", RowBox[{"yhi", ",", " ", RowBox[{"0", "+", RowBox[{ RowBox[{"(", RowBox[{"1", "/", "20"}], ")"}], RowBox[{"(", RowBox[{"yhi", "-", "ylow"}], ")"}]}]}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"endpts", "=", RowBox[{"{", RowBox[{"xmin", ",", " ", "xmax"}], "}"}]}], ";", "\[IndentingNewLine]", RowBox[{"innerpts", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"Random", "[", RowBox[{"Real", ",", RowBox[{"{", RowBox[{"xmin", ",", " ", "xmax"}], "}"}]}], "]"}], ",", " ", RowBox[{"{", RowBox[{"k", ",", " ", "1", ",", " ", RowBox[{"n", "-", "1"}]}], "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"subints", "=", RowBox[{"Union", "[", RowBox[{"endpts", ",", " ", "innerpts"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"cvals", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"Random", "[", RowBox[{"Real", ",", RowBox[{"{", RowBox[{ RowBox[{"subints", "[", RowBox[{"[", "k", "]"}], "]"}], ",", RowBox[{"subints", "[", RowBox[{"[", RowBox[{"k", "+", "1"}], "]"}], "]"}]}], "}"}]}], "]"}], ",", " ", RowBox[{"{", RowBox[{"k", ",", " ", "1", ",", " ", "n"}], "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"delx", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{ RowBox[{"subints", "[", RowBox[{"[", RowBox[{"k", "+", "1"}], "]"}], "]"}], "-", RowBox[{"subints", "[", RowBox[{"[", "k", "]"}], "]"}]}], ",", " ", RowBox[{"{", RowBox[{"k", ",", " ", "1", ",", " ", "n"}], "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"rect", "[", "i_", "]"}], ":=", RowBox[{"{", RowBox[{"Red", ",", " ", RowBox[{"Thickness", "[", ".008", "]"}], ",", " ", RowBox[{"Line", "[", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"subints", "[", RowBox[{"[", "i", "]"}], "]"}], ",", "0"}], "}"}], ",", RowBox[{"{", RowBox[{ RowBox[{"subints", "[", RowBox[{"[", "i", "]"}], "]"}], ",", " ", RowBox[{"f", "[", RowBox[{"cvals", "[", RowBox[{"[", "i", "]"}], "]"}], "]"}]}], "}"}], ",", " ", RowBox[{"{", RowBox[{ RowBox[{"subints", "[", RowBox[{"[", RowBox[{"i", "+", "1"}], "]"}], "]"}], ",", " ", RowBox[{"f", "[", RowBox[{"cvals", "[", RowBox[{"[", "i", "]"}], "]"}], "]"}]}], "}"}], ",", " ", RowBox[{"{", RowBox[{ RowBox[{"subints", "[", RowBox[{"[", RowBox[{"i", "+", "1"}], "]"}], "]"}], ",", " ", "0"}], "}"}], ",", " ", RowBox[{"{", RowBox[{ RowBox[{"subints", "[", RowBox[{"[", "i", "]"}], "]"}], ",", "0"}], "}"}]}], "}"}], "]"}]}], "}"}]}], ";", "\[IndentingNewLine]", RowBox[{"redrects", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"rect", "[", "i", "]"}], ",", " ", RowBox[{"{", RowBox[{"i", ",", " ", "1", ",", " ", "n"}], "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"vertseg", "[", "i_", "]"}], ":=", RowBox[{"{", RowBox[{"Blue", ",", " ", RowBox[{"Thickness", "[", ".008", "]"}], ",", " ", RowBox[{"Line", "[", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"cvals", "[", RowBox[{"[", "i", "]"}], "]"}], ",", " ", "0"}], "}"}], ",", " ", RowBox[{"{", RowBox[{ RowBox[{"cvals", "[", RowBox[{"[", "i", "]"}], "]"}], ",", " ", RowBox[{"f", "[", RowBox[{"cvals", "[", RowBox[{"[", "i", "]"}], "]"}], "]"}]}], "}"}]}], "}"}], "]"}]}], "}"}]}], ";", "\[IndentingNewLine]", RowBox[{"verts", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"vertseg", "[", "i", "]"}], ",", " ", RowBox[{"{", RowBox[{"i", ",", " ", "1", ",", " ", "n"}], "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{ RowBox[{"clab", "[", "i_", "]"}], ":=", RowBox[{"Text", "[", RowBox[{ RowBox[{"Style", "[", RowBox[{ SubscriptBox["\"\\"", "i"], ",", RowBox[{"FontColor", "\[Rule]", "Red"}], ",", " ", RowBox[{"FontSize", "\[Rule]", "18"}]}], "]"}], ",", " ", RowBox[{"{", RowBox[{ RowBox[{"cvals", "[", RowBox[{"[", "i", "]"}], "]"}], ",", " ", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"f", "[", RowBox[{"cvals", "[", RowBox[{"[", "i", "]"}], "]"}], "]"}], ">", "0"}], ",", RowBox[{ RowBox[{"-", RowBox[{"(", RowBox[{"1", "/", "30"}], ")"}]}], RowBox[{"(", RowBox[{"yhi", "-", "ylow"}], ")"}]}], ",", " ", RowBox[{ RowBox[{"(", RowBox[{"1", "/", "30"}], ")"}], RowBox[{"(", RowBox[{"yhi", "-", "ylow"}], ")"}]}]}], "]"}]}], "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"ctext", "=", RowBox[{"Table", "[", RowBox[{ RowBox[{"clab", "[", "i", "]"}], ",", " ", RowBox[{"{", RowBox[{"i", ",", " ", "1", ",", " ", "n"}], "}"}]}], "]"}]}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{"plf", "=", RowBox[{"Plot", "[", RowBox[{ RowBox[{"f", "[", "x", "]"}], ",", " ", RowBox[{"{", RowBox[{"x", ",", " ", RowBox[{"xmin", "-", RowBox[{ RowBox[{"(", RowBox[{"1", "/", "25"}], ")"}], RowBox[{"(", RowBox[{"xmax", "-", "xmin"}], ")"}]}]}], ",", " ", RowBox[{"xmax", "+", RowBox[{ RowBox[{"(", RowBox[{"1", "/", "25"}], ")"}], RowBox[{"(", RowBox[{"xmax", "-", "xmin"}], ")"}]}]}]}], "}"}], ",", " ", RowBox[{"PlotRange", "\[Rule]", " ", RowBox[{"{", RowBox[{"ymin", ",", " ", "ymax"}], "}"}]}], ",", RowBox[{"PlotStyle", "\[Rule]", RowBox[{"Thickness", "[", ".01", "]"}]}]}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"area", "=", RowBox[{ UnderoverscriptBox["\[Sum]", RowBox[{"i", "=", "1"}], "n"], RowBox[{"(", RowBox[{ RowBox[{"f", "[", RowBox[{"cvals", "[", RowBox[{"[", "i", "]"}], "]"}], "]"}], " ", RowBox[{"delx", "[", RowBox[{"[", "i", "]"}], "]"}]}], ")"}]}]}], ";", "\[IndentingNewLine]", RowBox[{"realint", "=", RowBox[{"NumberForm", "[", RowBox[{ RowBox[{"NIntegrate", "[", RowBox[{ RowBox[{"f", "[", "x", "]"}], ",", " ", RowBox[{"{", RowBox[{"x", ",", " ", "xmin", ",", " ", "xmax"}], "}"}]}], "]"}], ",", " ", "4"}], "]"}]}], ";", "\[IndentingNewLine]", StyleBox["\[IndentingNewLine]", FontColor->RGBColor[1, 0, 0]], RowBox[{"Show", "[", RowBox[{ RowBox[{"{", RowBox[{"plf", ",", RowBox[{"Graphics", "[", "verts", "]"}], ",", RowBox[{"Graphics", "[", "redrects", "]"}], ",", " ", RowBox[{"Graphics", "[", "ctext", "]"}]}], "}"}], ",", "\[IndentingNewLine]", RowBox[{"PlotLabel", "\[Rule]", " ", RowBox[{"Style", "[", RowBox[{ RowBox[{"StringForm", "[", RowBox[{ "\"\\"Italic\"]\))\[DifferentialD]\!\(\* StyleBox[\"x\", FontSlant->\"Italic\"]\)\!\(\* StyleBox[\" \", FontSlant->\"Italic\"]\)= ``\>\"", ",", " ", RowBox[{"NumberForm", "[", RowBox[{"area", ",", " ", "4"}], "]"}], ",", " ", "xmin", ",", " ", "xmax", ",", " ", "realint"}], "]"}], ",", " ", RowBox[{"FontSize", "\[Rule]", " ", "24"}], ",", " ", RowBox[{"FontWeight", "\[Rule]", " ", "\"\\""}]}], "]"}]}], ",", "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{"ImageSize", "\[Rule]", "600"}], ",", RowBox[{"Evaluate", "[", RowBox[{"Sequence", " ", "@@", " ", RowBox[{"FilterRules", "[", RowBox[{ RowBox[{"{", "opts", "}"}], ",", " ", RowBox[{"Options", "[", "Plot", "]"}]}], "]"}]}], "]"}]}], "]"}]}]}], "\[IndentingNewLine]", "]"}]}]}], "Input", InitializationCell->True, CellChangeTimes->{{3.3950936911515837`*^9, 3.395093790217258*^9}, { 3.3950938586302137`*^9, 3.395094014007125*^9}, {3.395094156592187*^9, 3.3950941791320257`*^9}, {3.39509433759949*^9, 3.395094377770134*^9}, { 3.395094449903932*^9, 3.395094453992345*^9}}] }, Closed]] }, Open ]] }, AutoGeneratedPackage->None, WindowSize->{925, 751}, WindowMargins->{{14, Automatic}, {Automatic, 0}}, PrivateNotebookOptions->{"VersionedStylesheet"->{"Default.nb"[8.] -> False}}, FrontEndVersion->"11.0 for Mac OS X x86 (32-bit, 64-bit Kernel) (September \ 21, 2016)", StyleDefinitions->"Default.nb" ] (* End of Notebook Content *) (* Internal cache information *) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[567, 22, 146, 6, 109, "Title"], Cell[716, 30, 153, 4, 49, "Subsubtitle"], Cell[872, 36, 1150, 37, 79, "Text"], Cell[2025, 75, 350, 10, 35, "Input"], Cell[CellGroupData[{ Cell[2400, 89, 33, 0, 64, "Section"], Cell[2436, 91, 11976, 316, 704, "Input", InitializationCell->True] }, Closed]] }, Open ]] } ] *)