(* 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[ 32118, 1145] NotebookOptionsPosition[ 27512, 1002] NotebookOutlinePosition[ 27926, 1020] CellTagsIndexPosition[ 27883, 1017] WindowFrame->Normal ContainsDynamic->False*) (* Beginning of Notebook Content *) Notebook[{ Cell[CellGroupData[{ Cell["Numbers", "Title"], Cell["\<\ M. E. Mauel Columbia University\ \>", "Subsubtitle"], Cell[CellGroupData[{ Cell["Introduction", "Section"], Cell[TextData[{ "In this notebook, we'll learn how ", StyleBox["Mathematica", FontSlant->"Italic"], " represents numbers.\n", StyleBox["Mathematica", FontSlant->"Italic"], " has three basic types of numbers:\n1. ", StyleBox["Integer", FontWeight->"Bold"], ". These are ", StyleBox["exact", FontSlant->"Italic"], " numbers. \n2. ", StyleBox["Real", FontWeight->"Bold"], " or \"MachinePrecision\". These are approximate, floating-point numbers in \ the representation used by your computer. Usually, desktop computers use \ 64-bit, or \"double-precision\" numbers. The results produced by ", StyleBox["Mathematica", FontSlant->"Italic"], " using Machine-Precision numbers can change from computer-to-computer.\n3. \ ", StyleBox["Arbitrary-Precision Numbers", FontWeight->"Bold"], ". These are approximate, floating-point numbers in a special, \ representation, internal to ", StyleBox["Mathematica", FontSlant->"Italic"], ". With arbitrary-precision numbers of a given precision, ", StyleBox["p", FontSlant->"Italic"], ", the first ", StyleBox["p", FontSlant->"Italic"], " significant digits are known, and ", StyleBox["Mathematica", FontSlant->"Italic"], " considers there to be an \"infinite number\" of unknown digits \ afterwards." }], "Text"], Cell[TextData[{ "The use of these numbers are demonstrated by showing:\n1. Computation with \ Real, MachinePrecision, numbers are ", StyleBox["very", FontSlant->"Italic"], " fast. (Although not as quickly as program produced with an optimizing \ compiler!)\n2. Since ", StyleBox["Mathematica", FontSlant->"Italic"], " uses your computer's processor when calculating MachinePrecision \ expressions, there may be considerable ", StyleBox["numerical error", FontSlant->"Italic"], ". Beware!\n3. ", "Computation with ", "Arbitrary-Precision numbers are ", StyleBox["very", FontSlant->"Italic"], " slow. Beware!" }], "Text"], Cell[TextData[{ "In this course, we will be pushing ", StyleBox["Mathematica", FontSlant->"Italic"], " to its computational limits. For this reason, most (if not all) of your \ calculations will be Real, MachinePrecision, numbers. You will work to make \ your calculations run as quickly as possible--if for no other reason that you \ will not want to wait to see your results!" }], "Text"], Cell[BoxData[ RowBox[{ RowBox[{"Off", "[", RowBox[{"General", "::", "\"\\""}], "]"}], ";"}]], "Input", CellLabel->"In[1]:="] }, Closed]], Cell[CellGroupData[{ Cell["Typing Numbers", "Section"], Cell[CellGroupData[{ Cell["Integer", "Subsection"], Cell[BoxData[ RowBox[{"11", "^", "24"}]], "Input", CellLabel->"In[2]:="], Cell[BoxData[ RowBox[{"Precision", "[", "1", "]"}]], "Input", CellLabel->"In[3]:="], Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{"13", "^", "7"}], "/", RowBox[{"14", "^", "6"}]}], " ", "+", " ", "1", " ", "+", " ", RowBox[{"Sin", "[", "45", "]"}]}]], "Input", CellLabel->"In[4]:="], Cell[TextData[{ "Integers are ", StyleBox["exact", FontSlant->"Italic"], " numbers! ", StyleBox["Mathematica", FontSlant->"Italic"], " ", "will simplify expressions, but it will ", StyleBox["not", FontSlant->"Italic"], " make approximations.", " " }], "Text"] }, Closed]], Cell[CellGroupData[{ Cell["Real (MachinePrecision)", "Subsection"], Cell[BoxData[ RowBox[{"11.0", "^", "24"}]], "Input", CellLabel->"In[5]:="], Cell[BoxData["1.23*^-6"], "Input", CellLabel->"In[6]:="], Cell[BoxData[ RowBox[{"Precision", "[", "1.0", "]"}]], "Input", CellLabel->"In[7]:="], Cell[BoxData[ RowBox[{"?", "*Machine*"}]], "Input", CellLabel->"In[8]:="], Cell[BoxData["$MachinePrecision"], "Input", CellLabel->"In[9]:="], Cell[BoxData[ RowBox[{"{", RowBox[{"$MaxMachineNumber", ",", " ", "$MinMachineNumber"}], "}"}]], "Input", CellLabel->"In[10]:="], Cell[BoxData["$MachineEpsilon"], "Input", CellLabel->"In[11]:="], Cell[BoxData[{ RowBox[{ RowBox[{"i", " ", "=", " ", "1.0"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"While", "[", RowBox[{ RowBox[{ RowBox[{"1.0", " ", "+", " ", "i"}], " ", "!=", " ", "1.0"}], ",", " ", RowBox[{"i", " ", "=", " ", RowBox[{"i", "/", "2"}]}]}], "]"}], ";", " ", "i"}]}], "Input", CellLabel->"In[12]:="], Cell[BoxData[ RowBox[{"{", RowBox[{ RowBox[{"1.0", " ", "\[Equal]", " ", RowBox[{"1.0", " ", "+", " ", "i"}]}], ",", " ", RowBox[{"1.0", " ", "\[Equal]", " ", RowBox[{"1.0", " ", "+", " ", RowBox[{"2", "i"}]}]}]}], "}"}]], "Input", CellLabel->"In[14]:="], Cell[CellGroupData[{ Cell["Example of \"round-off\" error", "Subsubsection"], Cell[BoxData[ RowBox[{"Series", "[", RowBox[{ RowBox[{ RowBox[{"(", RowBox[{"1", " ", "-", " ", RowBox[{"Cos", "[", RowBox[{"x", "^", "2"}], "]"}]}], ")"}], "/", RowBox[{"x", "^", "2"}]}], ",", " ", RowBox[{"{", RowBox[{"x", ",", " ", "0", ",", " ", "8"}], "}"}]}], "]"}]], "Input", CellLabel->"In[15]:="], Cell[BoxData[ RowBox[{"Plot", "[", RowBox[{ RowBox[{"{", RowBox[{ FractionBox[ SuperscriptBox["x", "2"], "2"], ",", FractionBox[ RowBox[{"1", "-", RowBox[{"Cos", "[", SuperscriptBox["x", "2"], "]"}]}], SuperscriptBox["x", "2"]]}], "}"}], ",", RowBox[{"{", RowBox[{"x", ",", RowBox[{"-", "0.0005"}], ",", "0.0005"}], "}"}], ",", RowBox[{"PlotStyle", "\[Rule]", RowBox[{"{", RowBox[{ RowBox[{"{", "Blue", "}"}], ",", RowBox[{"{", RowBox[{"Thickness", "[", "0.01", "]"}], "}"}]}], "}"}]}], ",", RowBox[{"PlotPoints", "\[Rule]", "100"}], ",", RowBox[{ "PlotLabel", "\[Rule]", "\"\<(1 - Cos[\\!\\(\\*SuperscriptBox[\\(x\\), \ \\(2\\)]\\)])/\\!\\(\\*SuperscriptBox[\\(x\\), \\(2\\)]\\)\>\""}]}], "]"}]], "Input", CellChangeTimes->{{3.441563210061473*^9, 3.4415632121421432`*^9}}, CellLabel->"In[66]:="] }, Closed]], Cell[CellGroupData[{ Cell["Using Numerical \"Microscope\"", "Subsubsection"], Cell[TextData[{ "Some functions are not automatically loaded into ", StyleBox["Mathematica", FontSlant->"Italic"], ". The \"Needs\" command tells ", StyleBox["Mathematica", FontSlant->"Italic"], " to load a package if a function within is called. The following package \ helps to illustrate computer precision." }], "Text"], Cell[BoxData[ RowBox[{ RowBox[{"Needs", "[", "\"\\"", "]"}], ";"}]], "Input",\ CellLabel->"In[17]:="], Cell[BoxData[ RowBox[{ RowBox[{"Ulp", "[", "1.0", "]"}], " ", RowBox[{"(*", " ", "\"\\"", " ", "*)"}]}]], "Input", CellLabel->"In[18]:="], Cell[BoxData[ RowBox[{"MachineError", "[", RowBox[{ RowBox[{ RowBox[{"Sin", "[", "x", "]"}], "^", "2"}], ",", " ", RowBox[{"x", " ", "\[Rule]", " ", "1"}]}], "]"}]], "Input", CellLabel->"In[19]:="], Cell[BoxData[ RowBox[{ RowBox[{"Needs", "[", "\"\\"", "]"}], ";", RowBox[{"ComputerArithmetic`MicroscopicErrorPlot", "[", RowBox[{ SuperscriptBox[ RowBox[{"Sin", "[", "x", "]"}], "2"], ",", RowBox[{"{", RowBox[{"x", ",", "1", ",", "15"}], "}"}]}], "]"}]}]], "Input", CellLabel->"In[20]:="], Cell[BoxData[ RowBox[{ RowBox[{"Needs", "[", "\"\\"", "]"}], ";", RowBox[{"ComputerArithmetic`MicroscopicErrorPlot", "[", RowBox[{ RowBox[{"Log", "[", RowBox[{"Tan", "[", SuperscriptBox[ RowBox[{"Sin", "[", "x", "]"}], "2"], "]"}], "]"}], ",", RowBox[{"{", RowBox[{"x", ",", "1", ",", "15"}], "}"}]}], "]"}]}]], "Input", CellLabel->"In[21]:="], Cell[TextData[{ "Subtraction of Machine-Precision numbers ", StyleBox["significantly reduces accuracy", FontSlant->"Italic"], "." }], "Text"], Cell[BoxData[ RowBox[{ RowBox[{"Needs", "[", "\"\\"", "]"}], ";", RowBox[{"ComputerArithmetic`MicroscopePlot", "[", RowBox[{ RowBox[{"x", "-", FractionBox["1", "x"]}], ",", RowBox[{"{", RowBox[{"x", ",", "1", ",", "15"}], "}"}]}], "]"}]}]], "Input", CellLabel->"In[22]:="], Cell[BoxData[ RowBox[{"{", RowBox[{ RowBox[{"MachineError", "[", RowBox[{ RowBox[{"x", " ", "-", " ", RowBox[{"1", "/", "x"}]}], ",", " ", RowBox[{"x", " ", "\[Rule]", " ", RowBox[{"1", " ", "-", " ", RowBox[{"Ulp", "[", "1.0", "]"}]}]}]}], "]"}], ",", " ", RowBox[{"MachineError", "[", RowBox[{ RowBox[{"x", " ", "-", " ", RowBox[{"1", "/", "x"}]}], ",", " ", RowBox[{"x", " ", "\[Rule]", " ", "1"}]}], " ", "]"}], ",", " ", RowBox[{"MachineError", "[", RowBox[{ RowBox[{"x", " ", "-", " ", RowBox[{"1", "/", "x"}]}], ",", " ", RowBox[{"x", " ", "\[Rule]", " ", RowBox[{"1", " ", "+", " ", RowBox[{"Ulp", "[", "1.0", "]"}]}]}]}], "]"}]}], "}"}]], "Input", CellLabel->"In[23]:="], Cell[BoxData[ RowBox[{"{", RowBox[{ RowBox[{"Ulp", "[", "0.0", "]"}], ",", " ", RowBox[{"Ulp", "[", "1.0", "]"}]}], "}"}]], "Input", CellLabel->"In[24]:="], Cell[BoxData[ RowBox[{ RowBox[{"Needs", "[", "\"\\"", "]"}], ";", RowBox[{"ComputerArithmetic`MicroscopicErrorPlot", "[", RowBox[{ RowBox[{"x", "-", FractionBox["1", "x"]}], ",", RowBox[{"{", RowBox[{"x", ",", "1", ",", "15"}], "}"}]}], "]"}]}]], "Input", CellLabel->"In[25]:="], Cell["Addition is not so bad:", "Text"], Cell[BoxData[ RowBox[{"{", RowBox[{ RowBox[{"MachineError", "[", RowBox[{ RowBox[{"x", " ", "+", " ", RowBox[{"1", "/", "x"}]}], ",", " ", RowBox[{"x", " ", "\[Rule]", " ", RowBox[{"1", " ", "-", " ", RowBox[{"Ulp", "[", "1.0", "]"}]}]}]}], "]"}], ",", " ", RowBox[{"MachineError", "[", RowBox[{ RowBox[{"x", " ", "+", " ", RowBox[{"1", "/", "x"}]}], ",", " ", RowBox[{"x", " ", "\[Rule]", " ", "1"}]}], " ", "]"}], ",", " ", RowBox[{"MachineError", "[", RowBox[{ RowBox[{"x", " ", "+", " ", RowBox[{"1", "/", "x"}]}], ",", " ", RowBox[{"x", " ", "\[Rule]", " ", RowBox[{"1", " ", "+", " ", RowBox[{"Ulp", "[", "1.0", "]"}]}]}]}], "]"}]}], "}"}]], "Input", CellLabel->"In[26]:="] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell["Arbitrary-Precision", "Subsection"], Cell[BoxData["1`32"], "Input", CellLabel->"In[27]:="], Cell[BoxData[ RowBox[{"Precision", "[", "1.0`32", "]"}]], "Input", CellLabel->"In[28]:="], Cell[BoxData[{ RowBox[{ RowBox[{"i", " ", "=", " ", "1.0`32"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"While", "[", RowBox[{ RowBox[{ RowBox[{"1.0`32", " ", "+", " ", "i"}], " ", "!=", " ", "1.0`32"}], ",", " ", RowBox[{"i", " ", "=", " ", RowBox[{"i", "/", "2"}]}]}], "]"}], ";", "i"}]}], "Input", CellLabel->"In[29]:="], Cell[BoxData[ RowBox[{"{", RowBox[{ RowBox[{"1.0`32", " ", "\[Equal]", " ", RowBox[{"1.0`32", " ", "+", " ", "i"}]}], ",", " ", RowBox[{"1.0`32", " ", "\[Equal]", " ", RowBox[{"1.0`32", " ", "+", " ", RowBox[{"2", "i"}]}]}]}], "}"}]], "Input", CellLabel->"In[31]:="], Cell[CellGroupData[{ Cell["$MachinePrecision gives the Fastest Calculation", "Subsubsection"], Cell[BoxData[ RowBox[{ RowBox[{"Nest", "[", RowBox[{"Sin", ",", " ", "0.1", ",", " ", RowBox[{"10", "^", "4"}]}], "]"}], " ", "//", " ", "Timing"}]], "Input", CellLabel->"In[32]:="], Cell[BoxData[ RowBox[{ RowBox[{"Nest", "[", RowBox[{"Sin", ",", " ", "0.1`32", ",", " ", RowBox[{"10", "^", "4"}]}], "]"}], " ", "//", " ", "Timing"}]], "Input", CellLabel->"In[33]:="], Cell[BoxData[ RowBox[{ RowBox[{"Nest", "[", RowBox[{"Sin", ",", " ", "0.1", ",", " ", RowBox[{"10", "^", "6"}]}], "]"}], " ", "//", " ", "Timing"}]], "Input", CellLabel->"In[34]:="], Cell[TextData[StyleBox["MachinePrecision is about 500 times faster!", FontWeight->"Bold", FontSlant->"Italic", FontColor->RGBColor[1, 0, 1]]], "Text", CellChangeTimes->{3.441563306992531*^9}], Cell[BoxData[ RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"10", "^", "6"}], "/", RowBox[{"%", "\[LeftDoubleBracket]", "1", "\[RightDoubleBracket]"}]}], ")"}], "/", RowBox[{"(", RowBox[{ RowBox[{"10", "^", "4"}], "/", RowBox[{"%%", "\[LeftDoubleBracket]", "1", "\[RightDoubleBracket]"}]}], ")"}]}]], "Input", CellLabel->"In[35]:="] }, Closed]] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell["Summation Example", "Section"], Cell[TextData[{ "Since ", StyleBox["Mathematica", FontSlant->"Italic"], " can perform algebraic computations, it can often find exact \ simplifications for expressions. However, numerical computation with \ Machine-Precision numbers contain errors. As more calculations are performed, \ these errors increase. Usually, the only way to maintain accuracy for large \ computations is to increase the precision of the representation of \ floating-point numbers. " }], "Text"], Cell["\<\ One simple example of numerical (round-off) error is the computation of the \ following sum:\ \>", "Text"], Cell[BoxData[{ RowBox[{ RowBox[{ RowBox[{"sumUp", "[", "n_", "]"}], " ", ":=", " ", RowBox[{"Sum", "[", RowBox[{ FractionBox["1", "i"], ",", " ", RowBox[{"{", RowBox[{"i", ",", " ", "1", ",", " ", "n"}], "}"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"sumDown", "[", "n_", "]"}], " ", ":=", " ", RowBox[{"Sum", "[", RowBox[{ FractionBox["1", "i"], ",", " ", RowBox[{"{", RowBox[{"i", ",", " ", "n", ",", " ", "1", ",", " ", RowBox[{"-", "1"}]}], "}"}]}], "]"}]}], ";"}]}], "Input", CellLabel->"In[36]:="], Cell["\<\ Notice the order of the two sums, and notice that the sums are equal.\ \>", "Text"], Cell[BoxData[ RowBox[{ RowBox[{"sumUp", "[", "10", "]"}], " ", "\[Equal]", " ", RowBox[{"sumDown", "[", "10", "]"}]}]], "Input", CellLabel->"In[38]:="], Cell[TextData[{ StyleBox["Mathematica", FontSlant->"Italic"], " knows the exact formula for the sum:" }], "Text"], Cell[BoxData[ RowBox[{"sumUp", "[", "n", "]"}]], "Input", CellLabel->"In[39]:="], Cell[TextData[{ "In order to compute these sums using Machine-Precision numbers, we will use \ the numerical value function, ", StyleBox["N[...]", FontWeight->"Bold"], "." }], "Text"], Cell[BoxData[{ RowBox[{ RowBox[{ RowBox[{"sumUpN", "[", "n_", "]"}], " ", ":=", " ", RowBox[{"Sum", "[", RowBox[{ RowBox[{"N", "[", FractionBox["1", "i"], "]"}], ",", " ", RowBox[{"{", RowBox[{"i", ",", " ", "1", ",", " ", "n"}], "}"}]}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"sumDownN", "[", "n_", "]"}], " ", ":=", " ", RowBox[{"Sum", "[", RowBox[{ RowBox[{"N", "[", FractionBox["1", "i"], "]"}], ",", " ", RowBox[{"{", RowBox[{"i", ",", " ", "n", ",", " ", "1", ",", " ", RowBox[{"-", "1"}]}], "}"}]}], "]"}]}], ";"}]}], "Input", CellLabel->"In[40]:="], Cell["As you can see below, these are not equal!", "Text"], Cell[BoxData[ RowBox[{"ListPlot", "[", RowBox[{ RowBox[{"Table", "[", RowBox[{ RowBox[{ RowBox[{"sumUpN", "[", "n", "]"}], "-", RowBox[{"sumDownN", "[", "n", "]"}]}], ",", RowBox[{"{", RowBox[{"n", ",", "1", ",", "1000"}], "}"}]}], "]"}], ",", RowBox[{"PlotStyle", "\[Rule]", RowBox[{"PointSize", "[", "0.02", "]"}]}]}], "]"}]], "Input", CellChangeTimes->{3.441563316607489*^9}, CellLabel->"In[42]:="], Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{"sumUpN", "[", RowBox[{"10", "^", "5"}], "]"}], " ", "-", " ", RowBox[{"sumDownN", "[", RowBox[{"10", "^", "5"}], "]"}]}], " ", "//", " ", "Timing"}]], "Input", CellLabel->"In[43]:="], Cell[TextData[{ "The computation used for the plot above requires a very long time as ", StyleBox["n", FontSlant->"Italic"], " increases. This is because the required number of calculations scale as ", StyleBox["n", FontSlant->"Italic"], "!. (Do you know why?) We can make things faster in the ", StyleBox["up", FontSlant->"Italic"], " direction by using ", StyleBox["Nest[...]", FontWeight->"Bold"], ", but each expression working down from large ", StyleBox["n", FontSlant->"Italic"], " must be calculated independently." }], "Text"], Cell[BoxData[ RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"(", RowBox[{"sumList", " ", "=", " ", RowBox[{"Table", "[", RowBox[{ RowBox[{"{", RowBox[{"n", ",", " ", RowBox[{"sumUpN", "[", "n", "]"}]}], "}"}], ",", " ", RowBox[{"{", RowBox[{"n", ",", " ", "1", ",", " ", "1000"}], "}"}]}], "]"}]}], ")"}], " ", "//", " ", "Shallow"}], ")"}], " ", "//", " ", "Timing"}]], "Input", CellLabel->"In[44]:="], Cell[BoxData[ RowBox[{"ListPlot", "[", RowBox[{"sumList", ",", RowBox[{"PlotLabel", "\[Rule]", "\"\\""}]}], "]"}]], "Input", CellLabel->"In[45]:="], Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{"nextUp", "[", RowBox[{"{", RowBox[{"n_", ",", " ", "sum_"}], "}"}], "]"}], " ", ":=", " ", RowBox[{"{", RowBox[{ RowBox[{"n", "+", "1"}], ",", " ", RowBox[{"sum", " ", "+", RowBox[{"N", "[", " ", FractionBox["1", RowBox[{"n", " ", "+", "1"}]], "]"}]}]}], "}"}]}], ";"}]], "Input", CellLabel->"In[46]:="], Cell[BoxData[ RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"(", RowBox[{"sumFast", " ", "=", " ", RowBox[{"NestList", "[", RowBox[{"nextUp", ",", " ", RowBox[{"{", RowBox[{"1", ",", "1"}], "}"}], ",", " ", RowBox[{"1000", "-", "1"}]}], "]"}]}], ")"}], " ", "//", " ", "Shallow"}], ")"}], " ", "//", " ", "Timing"}]], "Input", CellLabel->"In[47]:="], Cell[BoxData[ RowBox[{"Max", "[", RowBox[{"Abs", "[", RowBox[{"sumFast", " ", "-", " ", "sumList"}], "]"}], "]"}]], "Input", CellLabel->"In[48]:="] }, Closed]], Cell[CellGroupData[{ Cell["Subtractive Cancellation Example", "Section"], Cell[TextData[{ "Another practical and useful example of a major source of numerical error \ in computational physics is subtractive cancellation (a type of \"round-off\" \ error.) When two numbers are subtracted from each other, the result is ", StyleBox["less precise", FontSlant->"Italic"], " as the original two numbers!" }], "Text"], Cell["\<\ We will illustrate this by computing a first-order and second-order \ approximation to a derivative and compare this approximation with the \"exact\ \" value.\ \>", "Text"], Cell[TextData[{ "A first-order approximation to the derivative of the function ", StyleBox["f[x]", FontWeight->"Bold"], " is\n\t", Cell[BoxData[ FormBox[ RowBox[{ SubscriptBox["\[PartialD]", "x"], " ", "f"}], TraditionalForm]]], " ~ ", Cell[BoxData[ FormBox[ StyleBox[ FractionBox[ RowBox[{ RowBox[{"f", "[", RowBox[{"x", " ", "+", " ", "h"}], "]"}], " ", "-", " ", RowBox[{"f", "[", "x", "]"}]}], "h"], FontSize->18], TraditionalForm]]], "\nIt is called \"first-order\" because it's accuracy improves linearly with \ decreasing ", StyleBox["h", FontSlant->"Italic"], "." }], "Text"], Cell[TextData[{ "A second-order approximation to the derivative of the function ", StyleBox["f[x]", FontWeight->"Bold"], " is\n\t", Cell[BoxData[ FormBox[ RowBox[{ SubscriptBox["\[PartialD]", "x"], " ", "f"}], TraditionalForm]]], " ~ ", Cell[BoxData[ FormBox[ StyleBox[ FractionBox[ RowBox[{ RowBox[{"f", "[", RowBox[{"x", " ", "+", " ", "h"}], "]"}], " ", "-", " ", RowBox[{"f", "[", RowBox[{"x", "-", "h"}], "]"}]}], RowBox[{"2", "h"}]], FontSize->18], TraditionalForm]]], "\nIt is called \"second-order\" because it's accuracy improves linearly \ with decreasing ", Cell[BoxData[ FormBox[ SuperscriptBox["h", "2"], TraditionalForm]]], ". This is called a \"central difference\" because the difference of ", StyleBox["f[x]", FontWeight->"Bold"], " on ", StyleBox["both", FontSlant->"Italic"], " sides of ", StyleBox["x", FontSlant->"Italic"], " are evaluated." }], "Text"], Cell[BoxData[ RowBox[{ RowBox[{ RowBox[{"f", "[", "x_", "]"}], " ", ":=", " ", RowBox[{"x", "^", "3"}]}], ";"}]], "Input", CellLabel->"In[49]:="], Cell[CellGroupData[{ Cell["First Order Derivative", "Subsection"], Cell[BoxData[ RowBox[{ RowBox[{"error1", "[", RowBox[{"h_", ",", RowBox[{"x_:", "1"}]}], "]"}], " ", ":=", " ", RowBox[{"Abs", "[", RowBox[{ RowBox[{"3", " ", RowBox[{"x", "^", "2"}]}], " ", "-", " ", RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"f", "[", RowBox[{"x", "+", "h"}], "]"}], " ", "-", " ", RowBox[{"f", "[", "x", "]"}]}], ")"}], "/", "h"}]}], "]"}]}]], "Input",\ CellLabel->"In[50]:="], Cell[BoxData[ RowBox[{ RowBox[{"err1List", " ", "=", " ", RowBox[{"Table", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"10.0", "^", "h"}], ",", RowBox[{"error1", "[", RowBox[{"10.0", "^", "h"}], "]"}]}], "}"}], ",", RowBox[{"{", RowBox[{"h", ",", RowBox[{"-", "14"}], ",", RowBox[{"-", "1"}], ",", "0.5"}], "}"}]}], "]"}]}], ";"}]], "Input", CellLabel->"In[51]:="], Cell[BoxData[ RowBox[{"err1List", " ", "//", " ", "Shallow"}]], "Input", CellLabel->"In[52]:="], Cell[BoxData[ RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"Needs", "[", "\"\\"", "]"}], ";", RowBox[{"Needs", "[", "\"\\"", "]"}], ";", RowBox[{"Needs", "[", "\"\\"", "]"}]}], ")"}], ";"}]], "Input", CellLabel->"In[53]:="], Cell[BoxData[ RowBox[{"ListLogLogPlot", "[", RowBox[{"err1List", ",", RowBox[{"PlotStyle", "\[Rule]", RowBox[{"PointSize", "[", "0.02`", "]"}]}]}], "]"}]], "Input", CellLabel->"In[54]:="], Cell["\<\ Often, I set Graphics options to make the plot easier to read.\ \>", "Text"], Cell[BoxData[ RowBox[{ RowBox[{"myPlot", "[", "items__", "]"}], ":=", RowBox[{"ListLogLogPlot", "[", RowBox[{"items", ",", RowBox[{"Frame", "\[Rule]", "True"}], ",", RowBox[{"GridLines", "\[Rule]", "Automatic"}], ",", RowBox[{"PlotStyle", "\[Rule]", RowBox[{"{", RowBox[{ RowBox[{"PointSize", "[", "0.02`", "]"}], ",", RowBox[{"RGBColor", "[", RowBox[{"1", ",", "0", ",", "0"}], "]"}]}], "}"}]}], ",", RowBox[{"BaseStyle", "\[Rule]", RowBox[{"{", RowBox[{ RowBox[{"FontWeight", "\[Rule]", "\"\\""}], ",", RowBox[{"FontSize", "\[Rule]", "12"}]}], "}"}]}]}], "]"}]}]], "Input", CellLabel->"In[55]:="], Cell[BoxData[ RowBox[{ RowBox[{"p1", " ", "=", " ", RowBox[{"myPlot", "[", RowBox[{"err1List", ",", RowBox[{ "PlotLabel", "->", "\"\<1st Order Difference Approximation\>\""}]}], "]"}]}], ";"}]], "Input", CellLabel->"In[56]:="] }, Closed]], Cell[CellGroupData[{ Cell["Second Order Derivative", "Subsection"], Cell[BoxData[ RowBox[{ RowBox[{"error2", "[", RowBox[{"h_", ",", RowBox[{"x_:", "1"}]}], "]"}], " ", ":=", " ", RowBox[{"Abs", "[", RowBox[{ RowBox[{"3", " ", RowBox[{"x", "^", "2"}]}], " ", "-", " ", RowBox[{ RowBox[{"(", RowBox[{ RowBox[{"f", "[", RowBox[{"x", "+", "h"}], "]"}], " ", "-", " ", RowBox[{"f", "[", RowBox[{"x", "-", "h"}], "]"}]}], ")"}], "/", RowBox[{"(", RowBox[{"2", "h"}], ")"}]}]}], "]"}]}]], "Input", CellLabel->"In[57]:="], Cell[BoxData[ RowBox[{ RowBox[{"err2List", " ", "=", " ", RowBox[{"Table", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"10.0", "^", "h"}], ",", RowBox[{"error2", "[", RowBox[{"10.0", "^", "h"}], "]"}]}], "}"}], ",", RowBox[{"{", RowBox[{"h", ",", RowBox[{"-", "14"}], ",", RowBox[{"-", "1"}], ",", "0.5"}], "}"}]}], "]"}]}], ";"}]], "Input", CellLabel->"In[58]:="], Cell[BoxData[ RowBox[{ RowBox[{"p2", " ", "=", " ", RowBox[{"myPlot", "[", RowBox[{"err2List", ",", RowBox[{ "PlotLabel", "->", "\"\<2nd Order Difference Approximation\>\""}]}], "]"}]}], ";"}]], "Input", CellLabel->"In[59]:="], Cell[BoxData[ RowBox[{"Show", "[", RowBox[{"p2", ",", "p1", ",", RowBox[{"PlotLabel", "\[Rule]", "\"\\""}]}], "]"}]], "Input", CellLabel->"In[60]:="] }, Closed]], Cell[CellGroupData[{ Cell["Arbitrary-Precision", "Subsection"], Cell[BoxData[ RowBox[{"10.0`32", "^", RowBox[{"-", "16"}]}]], "Input", CellLabel->"In[61]:="], Cell[BoxData[ RowBox[{ RowBox[{"err3List", " ", "=", " ", RowBox[{"Table", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"10.0", "^", "h"}], ",", RowBox[{"error2", "[", RowBox[{"10.0`32", "^", "h"}], "]"}]}], "}"}], ",", RowBox[{"{", RowBox[{"h", ",", RowBox[{"-", "14"}], ",", RowBox[{"-", "1"}], ",", " ", "0.5`32"}], "}"}]}], "]"}]}], ";"}]], "Input", CellLabel->"In[62]:="], Cell[BoxData[ RowBox[{"err3List", " ", "//", " ", "Shallow"}]], "Input", CellLabel->"In[63]:="], Cell[BoxData[ RowBox[{ RowBox[{"p3", " ", "=", " ", RowBox[{"myPlot", "[", RowBox[{"err3List", ",", RowBox[{"PlotLabel", "->", "\"\<2nd Order Arbitrary-Precision\>\""}]}], "]"}]}], ";"}]], "Input", CellLabel->"In[64]:="], Cell[BoxData[ RowBox[{"Show", "[", RowBox[{"p3", ",", "p2", ",", "p1", ",", RowBox[{"PlotLabel", "\[Rule]", "\"\\""}]}], "]"}]], "Input", CellLabel->"In[65]:="] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell["Summary", "Section"], Cell[TextData[{ StyleBox["Mathematica", FontSlant->"Italic"], " computes with three types of numbers: Integers, Reals, and \ Arbitrary-Precision Numbers. \nIntegers are ", StyleBox["exact", FontSlant->"Italic"], ", but they can not represent a continuous variable. Real numbers use the \ floating-point processor within your computer. They are ", StyleBox["approximate", FontSlant->"Italic"], " numbers, but calculations can be fast. Most of your work in this course \ will be with Real numbers. Arbitrary-Precision numbers use a special, \ internal representation within ", StyleBox["Mathematica. ", FontSlant->"Italic"], " They have a known (and controlable) precision.\nFinally, we examined the \ finite-difference approximation to the derivative of a function. We found \ that ", StyleBox["round-off", FontSlant->"Italic"], " error limited the accuracy of numerical approximations to derivatives." }], "Text"] }, Closed]] }, Open ]] }, WindowSize->{640, 882}, WindowMargins->{{10, Automatic}, {Automatic, 3}}, PrintingCopies->1, PrintingPageRange->{1, Automatic}, ShowSelection->True, FrontEndVersion->"6.0 for Mac OS X x86 (32-bit) (May 21, 2008)", StyleDefinitions->"TutorialBook.nb" ] (* End of Notebook Content *) (* Internal cache information *) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[590, 23, 24, 0, 51, "Title"], Cell[617, 25, 62, 3, 41, "Subsubtitle"], Cell[CellGroupData[{ Cell[704, 32, 31, 0, 86, "Section"], Cell[738, 34, 1290, 40, 249, "Text"], Cell[2031, 76, 638, 19, 149, "Text"], Cell[2672, 97, 395, 8, 77, "Text"], Cell[3070, 107, 141, 4, 24, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[3248, 116, 33, 0, 54, "Section"], Cell[CellGroupData[{ Cell[3306, 120, 29, 0, 34, "Subsection"], Cell[3338, 122, 74, 2, 24, "Input"], Cell[3415, 126, 85, 2, 26, "Input"], Cell[3503, 130, 206, 6, 26, "Input"], Cell[3712, 138, 274, 13, 41, "Text"] }, Closed]], Cell[CellGroupData[{ Cell[4023, 156, 45, 0, 34, "Subsection"], Cell[4071, 158, 76, 2, 24, "Input"], Cell[4150, 162, 57, 1, 24, "Input"], Cell[4210, 165, 87, 2, 26, "Input"], Cell[4300, 169, 75, 2, 26, "Input"], Cell[4378, 173, 66, 1, 26, "Input"], Cell[4447, 176, 136, 4, 26, "Input"], Cell[4586, 182, 65, 1, 26, "Input"], Cell[4654, 185, 358, 10, 45, "Input"], Cell[5015, 197, 282, 8, 26, "Input"], Cell[CellGroupData[{ Cell[5322, 209, 55, 0, 33, "Subsubsection"], Cell[5380, 211, 352, 11, 26, "Input"], Cell[5735, 224, 927, 28, 177, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[6699, 257, 55, 0, 33, "Subsubsection"], Cell[6757, 259, 332, 9, 59, "Text"], Cell[7092, 270, 128, 4, 26, "Input"], Cell[7223, 276, 165, 4, 26, "Input"], Cell[7391, 282, 213, 6, 26, "Input"], Cell[7607, 290, 342, 9, 78, "Input"], Cell[7952, 301, 411, 11, 78, "Input"], Cell[8366, 314, 146, 5, 23, "Text"], Cell[8515, 321, 326, 9, 67, "Input"], Cell[8844, 332, 794, 22, 63, "Input"], Cell[9641, 356, 167, 5, 24, "Input"], Cell[9811, 363, 332, 9, 102, "Input"], Cell[10146, 374, 39, 0, 23, "Text"], Cell[10188, 376, 794, 22, 63, "Input"] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell[11031, 404, 41, 0, 34, "Subsection"], Cell[11075, 406, 54, 1, 26, "Input"], Cell[11132, 409, 91, 2, 26, "Input"], Cell[11226, 413, 367, 11, 63, "Input"], Cell[11596, 426, 294, 8, 26, "Input"], Cell[CellGroupData[{ Cell[11915, 438, 72, 0, 33, "Subsubsection"], Cell[11990, 440, 194, 5, 26, "Input"], Cell[12187, 447, 197, 5, 26, "Input"], Cell[12387, 454, 194, 5, 26, "Input"], Cell[12584, 461, 195, 4, 21, "Text"], Cell[12782, 467, 366, 12, 26, "Input"] }, Closed]] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell[13209, 486, 36, 0, 54, "Section"], Cell[13248, 488, 477, 10, 77, "Text"], Cell[13728, 500, 116, 3, 23, "Text"], Cell[13847, 505, 610, 19, 89, "Input"], Cell[14460, 526, 93, 2, 23, "Text"], Cell[14556, 530, 158, 4, 24, "Input"], Cell[14717, 536, 117, 4, 23, "Text"], Cell[14837, 542, 82, 2, 24, "Input"], Cell[14922, 546, 188, 6, 41, "Text"], Cell[15113, 554, 676, 21, 89, "Input"], Cell[15792, 577, 58, 0, 23, "Text"], Cell[15853, 579, 455, 13, 45, "Input"], Cell[16311, 594, 244, 7, 26, "Input"], Cell[16558, 603, 556, 17, 77, "Text"], Cell[17117, 622, 485, 15, 45, "Input"], Cell[17605, 639, 179, 5, 26, "Input"], Cell[17787, 646, 406, 13, 47, "Input"], Cell[18196, 661, 408, 12, 45, "Input"], Cell[18607, 675, 156, 4, 26, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[18800, 684, 51, 0, 54, "Section"], Cell[18854, 686, 342, 7, 59, "Text"], Cell[19199, 695, 182, 4, 41, "Text"], Cell[19384, 701, 644, 24, 89, "Text"], Cell[20031, 727, 964, 36, 107, "Text"], Cell[20998, 765, 156, 5, 26, "Input"], Cell[CellGroupData[{ Cell[21179, 774, 44, 0, 34, "Subsection"], Cell[21226, 776, 464, 16, 28, "Input"], Cell[21693, 794, 442, 14, 44, "Input"], Cell[22138, 810, 97, 2, 26, "Input"], Cell[22238, 814, 282, 8, 45, "Input"], Cell[22523, 824, 201, 5, 26, "Input"], Cell[22727, 831, 86, 2, 23, "Text"], Cell[22816, 835, 701, 18, 100, "Input"], Cell[23520, 855, 255, 8, 45, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[23812, 868, 45, 0, 34, "Subsection"], Cell[23860, 870, 539, 18, 43, "Input"], Cell[24402, 890, 442, 14, 44, "Input"], Cell[24847, 906, 255, 8, 45, "Input"], Cell[25105, 916, 180, 5, 26, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[25322, 926, 41, 0, 34, "Subsection"], Cell[25366, 928, 98, 3, 26, "Input"], Cell[25467, 933, 456, 15, 45, "Input"], Cell[25926, 950, 97, 2, 26, "Input"], Cell[26026, 954, 244, 7, 45, "Input"], Cell[26273, 963, 200, 5, 43, "Input"] }, Closed]] }, Closed]], Cell[CellGroupData[{ Cell[26522, 974, 26, 0, 54, "Section"], Cell[26551, 976, 933, 22, 155, "Text"] }, Closed]] }, Open ]] } ] *) (* End of internal cache information *)