(*********************************************************************** Mathematica-Compatible Notebook This notebook can be used on any computer system with Mathematica 3.0, MathReader 3.0, or any compatible application. The data for the notebook starts with the line of stars above. To get the notebook into a Mathematica-compatible application, do one of the following: * Save the data starting with the line of stars above into a file with a name ending in .nb, then open the file inside the application; * Copy the data starting with the line of stars above to the clipboard, then use the Paste menu command inside the application. Data for notebooks contains only printable 7-bit ASCII and can be sent directly in email or through ftp in text mode. Newlines can be CR, LF or CRLF (Unix, Macintosh or MS-DOS style). NOTE: If you modify the data for this notebook not in a Mathematica- compatible application, you must delete the line below containing the word CacheID, otherwise Mathematica-compatible applications may try to use invalid cache data. For more information on notebooks and Mathematica-compatible applications, contact Wolfram Research: web: http://www.wolfram.com email: info@wolfram.com phone: +1-217-398-0700 (U.S.) Notebook reader applications are available free of charge from Wolfram Research. ***********************************************************************) (*CacheID: 232*) (*NotebookFileLineBreakTest NotebookFileLineBreakTest*) (*NotebookOptionsPosition[ 10583, 309]*) (*NotebookOutlinePosition[ 11220, 332]*) (* CellTagsIndexPosition[ 11176, 328]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell["Cantilevered Blocks", "Section"], Cell[TextData[{ "This notebook is designed to accompany Chapter 4 of \"Introduction to \ Scientific Programming: Computational Problem Solving Using ", StyleBox["Mathematica", FontSlant->"Italic"], " and C\" by Joseph L. Zachary. In it, we will use ", StyleBox["Mathematica", FontSlant->"Italic"], " to explore the block-stacking problem. (26Jul97}" }], "Text"], Cell[CellGroupData[{ Cell["Getting Started", "Subsection"], Cell[TextData[{ " To use this worksheet you will need to use some ", StyleBox["Mathematica", FontSlant->"Italic"], " extensions that we have created. Load our blocks package by evaluating \ the ", StyleBox["Mathematica", FontSlant->"Italic"], " command below. (You will need to have first installed our custom ", StyleBox["Mathematica", FontSlant->"Italic"], " library.) " }], "Text"], Cell[BoxData[ \(Needs["\"]\)], "Input"] }, Closed]], Cell[CellGroupData[{ Cell["Calculating Extensions", "Subsection"], Cell["\<\ The four functions defined in the library are described in Chapter \ 4 of the text. Each takes as a parameter the number of blocks in a stack of \ blocks arranged as described in the text and reports back the amount by which \ the top block extends beyond the table. For example, let's compute (in four \ different ways) the extension that can be achieved with 100 blocks.\ \>", "Text"], Cell["\<\ The \"BlockFloat\" function uses floating-point arithmetic to \ explicitly sum the series 1/2 + 1/4 + 1/6 + 1/8 + ... + 1/2n. For n = 100, \ that sum is\ \>", "Text"], Cell[BoxData[ \(BlockFloat[100]\)], "Input"], Cell["\<\ The \"BlockRat\" function uses rational arithmetic to sum the same \ series. After it has computed an exact sum, we use \"N\" to convert the \ result into a floating-point number.\ \>", "Text"], Cell[BoxData[ \(N[BlockRat[100]]\)], "Input"], Cell["\<\ The \"BlockFast\" function computes the sum of the series, not by \ adding up each term, but by using the formula (1/2) (Psi(n+1) + gamma). \ Notice that we get the same answer as with \"BlockFast\".\ \>", "Text"], Cell[BoxData[ \(BlockFast[100]\)], "Input"], Cell["\<\ The \"BlockPrecision\" function takes a second parameter \"p\". It \ uses simulated floating-point arithmetic with precision p (if p<=10); it uses \ arbitrary-precision arithmetic with precision p (if p>$MachinePrecision); \ othewise, it uses regular floating-point numbers. For example, here we \ compute the extension possible with 100 blocks using 3-digit floating-point \ numbers\ \>", "Text"], Cell[BoxData[ \(BlockPrecision[100, \ 3]\)], "Input"], Cell["\<\ \"BlockPrecision\" can be used to study how roundoff error can \ accumulate in a series of computations. Let's compare the results of using \ 4-digit floating-point numbers, rational numbers, and the formula embodied in \ \"BlockFast\" to sum increasingly larger numbers of blocks.\ \>", "Text"], Cell[BoxData[{ \(BlockPrecision[100, 4]\), \(N[BlockRat[100]]\), \(BlockFast[100]\)}], "Input"], Cell[BoxData[{ \(BlockPrecision[200, 4]\), \(N[BlockRat[200]]\), \(BlockFast[200]\)}], "Input"], Cell[BoxData[{ \(BlockPrecision[400, 4]\), \(N[BlockRat[400]]\), \(BlockFast[400]\)}], "Input"], Cell[BoxData[{ \(BlockPrecision[800, 4]\), \(N[BlockRat[800]]\), \(BlockFast[800]\)}], "Input"], Cell[BoxData[{ \(BlockPrecision[1600, 4]\), \(N[BlockRat[1600]]\), \(BlockFast[1600]\)}], "Input"], Cell["\<\ Notice what happens: BlockPrecision, which operates by directly summing the series using 4-digit \ floating-point arithmetic, suffers more and more from roundoff error as the \ number of terms increases. BlockRat, which operates by directly summing the series using rational number \ arithmetic, always returns an exact answer. We then convert that exact \ answer into a floating-point number that is exact to the available number of \ digits. BlockFast, which operates by exploiting specialized mathematical knowledge \ about the Harmonic series, returns a number that is exact to the available \ number of digits. BlockFast is also extremely fast.\ \>", "Text"], Cell[CellGroupData[{ Cell["Exercises", "Subsubsection"], Cell["\<\ Let's go back to 16-digit mantissas and explore how roundoff error \ accumulates when doing a long series of floating-point calculations. Using BlockFast, we can find the extension possible with one thousand blocks. \ (We save the result in the variable \"truth\".)\ \>", "Text"], Cell[BoxData[ \(truth\ = \ BlockFast[1000]\)], "Input"], Cell["\<\ Let's use BlockPrecision to do the same calculation using mantissa \ lengths ranging from 1 to 10. For example, with a five-digit mantissa, the \ result is as follows. (We save the result in the variable \"result\".)\ \>", "Text"], Cell[BoxData[ \(\(\ result\ = \ BlockPrecision[1000, \ 5]\)\)], "Input"], Cell["The relative error is approximately 0.024%.", "Text"], Cell[BoxData[ \(Abs[truth\ - \ result]\ /\ truth\)], "Input"], Cell["\<\ You should do similar calculations using for each mantissa length \ from 1 to 10. When you are done, you will know the relative error for each \ mantissa length that is exhibited by BlockPrecision when calculating the \ extension for one thousand blocks. You can visualize the effect of mantissa length on relative error by plotting \ mantissa length on the x-axis against relative error on the y-axis. To do \ this, use the command below. It plots the numbers 1 through 10 against some \ arbitrarily chosen numbers. You will have to replace the arbitrarily chosen \ numbers with the relative errors that you have calculated. For example, [5, \ 30] would become [5, .00024]. (Rounding the relative errors to two decimal \ places is OK.)\ \>", "Text"], Cell[BoxData[ \(ListPlot[{{1, \ 88}, \ {2, \ 22}, \ {3, \ 14}, \ {4, \ 13}, \ {5, \ 30}, \n\ \ \ \ \ \ \ \ \ \ {6, \ 0}, \ {7, \ 2}, \ {8, \ 99}, \ {9, \ 17}, \ {10, \ 88}}, \ AxesLabel -> {"\", \ "\"}, \ PlotStyle -> PointSize[0.02], \ PlotRange -> {{0, 10}, {0, 100}}]\)], "Input"], Cell["\<\ It is also interesting to compare the amount of time required by \ BlockFloat and BlockRat to do their calculations. For block stack sizes of \ 100, 200, 400, 800, 1600, and 3200, let's calculate the time required to \ calculate the extension using both BlockFloat and BlockRat. For example, the \ time required to compute BlockFloat[100] is the first number displayed by \ this expression. (The second is the result of the call to BlockFloat.)\ \>", "Text"], Cell[BoxData[ \(Timing[BlockFloat[100]]\)], "Input"], Cell["\<\ The Timing function is so crude that for quick calculations, it is \ a good idea to do several calculations and calculate an average. To obtain a \ better timing result for blockFloat[100], for example, we can do eight \ identical calculations and then divide the overall result by 8. (You'll have \ to ignore the calculated extension, however.)\ \>", "Text"], Cell[BoxData[ \(Timing[BlockFloat[100]; \ BlockFloat[100]; \ BlockFloat[100]; \ BlockFloat[100]; \ BlockFloat[100]; \ BlockFloat[100]; \ BlockFloat[100]; \ BlockFloat[100]]/8\)], "Input"], Cell["\<\ For long calculations such as BlockFloat[3200], of course, this \ would be unnecessary . You should time BlockFloat for stack sizes of 100, 200, 400, 800, 1600, and \ 3200. You can plot your results by completing the following command. You \ will need to insert the times that you obtain in place of the zeroes.\ \>", "Text"], Cell[BoxData[ \(ListPlot[{{100, \ 5}, \ {200, \ 5}, \ {400, \ 5}, \ {800, \ 5}, \ {1600, \ 5}, \n\ \ \ \ \ \ \ \ \ \ {3200, \ 5}}, \ AxesLabel -> {"\", \ "\"}, \ PlotStyle -> PointSize[0.02], \ PlotRange -> {{0, 3200}, {0, 10}}]\)], "Input"], Cell["\<\ Repeat the timing experiments using BlockRat instead of BlockFloat. \ Plot your results by completing the command below.\ \>", "Text"], Cell[BoxData[ \(ListPlot[{{100, \ 5}, \ {200, \ 5}, \ {400, \ 5}, \ {800, \ 5}, \ {1600, \ 5}, \n\ \ \ \ \ \ \ \ \ \ {3200, \ 5}}, \ AxesLabel -> {"\", \ "\"}, \ PlotStyle -> PointSize[0.02], \ PlotRange -> {{0, 3200}, {0, 10}}]\)], "Input"], Cell["\<\ Compare the shapes of the two curves that you create and try to \ explain the difference.\ \>", "Text"], Cell[BoxData[ \(HTMLSave::"fileexists" \( : \ \) "The destination file \!\(\"/a/home/handin/hamlet/.public_h\" \ \\[Ellipsis] \"va/notebooks/blocks/blocks.html\"\) already exists. In this \ implementation, it will be overwritten."\)], "Message"] }, Closed]] }, Closed]] }, Open ]] }, FrontEndVersion->"X 3.0", ScreenRectangle->{{0, 1280}, {0, 1024}}, WindowSize->{520, 600}, WindowMargins->{{Automatic, 51}, {162, Automatic}} ] (*********************************************************************** Cached data follows. If you edit this Notebook file directly, not using Mathematica, you must remove the line containing CacheID at the top of the file. The cache data will then be recreated when you save this file from within Mathematica. ***********************************************************************) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[1731, 51, 38, 0, 54, "Section"], Cell[1772, 53, 381, 9, 68, "Text"], Cell[CellGroupData[{ Cell[2178, 66, 37, 0, 45, "Subsection"], Cell[2218, 68, 412, 12, 68, "Text"], Cell[2633, 82, 57, 1, 27, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[2727, 88, 44, 0, 29, "Subsection"], Cell[2774, 90, 402, 7, 86, "Text"], Cell[3179, 99, 177, 4, 50, "Text"], Cell[3359, 105, 48, 1, 27, "Input"], Cell[3410, 108, 204, 4, 50, "Text"], Cell[3617, 114, 49, 1, 27, "Input"], Cell[3669, 117, 224, 4, 68, "Text"], Cell[3896, 123, 47, 1, 27, "Input"], Cell[3946, 126, 409, 7, 86, "Text"], Cell[4358, 135, 57, 1, 27, "Input"], Cell[4418, 138, 306, 5, 86, "Text"], Cell[4727, 145, 109, 3, 59, "Input"], Cell[4839, 150, 109, 3, 59, "Input"], Cell[4951, 155, 109, 3, 59, "Input"], Cell[5063, 160, 109, 3, 59, "Input"], Cell[5175, 165, 112, 3, 59, "Input"], Cell[5290, 170, 678, 15, 230, "Text"], Cell[CellGroupData[{ Cell[5993, 189, 34, 0, 42, "Subsubsection"], Cell[6030, 191, 292, 6, 104, "Text"], Cell[6325, 199, 60, 1, 27, "Input"], Cell[6388, 202, 245, 5, 68, "Text"], Cell[6636, 209, 77, 1, 27, "Input"], Cell[6716, 212, 59, 0, 32, "Text"], Cell[6778, 214, 66, 1, 27, "Input"], Cell[6847, 217, 767, 13, 194, "Text"], Cell[7617, 232, 348, 6, 91, "Input"], Cell[7968, 240, 474, 8, 104, "Text"], Cell[8445, 250, 56, 1, 27, "Input"], Cell[8504, 253, 371, 6, 86, "Text"], Cell[8878, 261, 209, 3, 75, "Input"], Cell[9090, 266, 341, 8, 104, "Text"], Cell[9434, 276, 291, 5, 75, "Input"], Cell[9728, 283, 145, 3, 50, "Text"], Cell[9876, 288, 291, 5, 75, "Input"], Cell[10170, 295, 113, 3, 32, "Text"], Cell[10286, 300, 257, 4, 71, "Message"] }, Closed]] }, Closed]] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)