{VERSION 2 3 "IBM INTEL NT" "2.3" } {USTYLETAB {CSTYLE "Maple Input" -1 0 "Courier" 0 1 255 0 0 1 0 1 0 0 1 0 0 0 0 }{CSTYLE "2D Math" -1 2 "Times" 0 1 0 0 0 0 0 0 2 0 0 0 0 0 0 }{CSTYLE "2D Comment" 2 18 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 } {CSTYLE "" -1 256 "" 0 1 0 0 22 0 1 0 0 0 0 0 0 0 0 }{PSTYLE "Normal" -1 0 1 {CSTYLE "" -1 -1 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 }0 0 0 -1 -1 -1 0 0 0 0 0 0 -1 0 }{PSTYLE "Heading 1" 0 3 1 {CSTYLE "" -1 -1 "" 1 18 0 0 0 0 0 1 0 0 0 0 0 0 0 }1 0 0 0 6 6 0 0 0 0 0 0 -1 0 }{PSTYLE "H eading 2" 3 4 1 {CSTYLE "" -1 -1 "" 1 14 0 0 0 0 0 0 0 0 0 0 0 0 0 }0 0 0 -1 4 4 0 0 0 0 0 0 -1 0 }{PSTYLE "Bullet Item" 0 15 1 {CSTYLE "" -1 -1 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 }0 0 0 -1 3 3 0 0 0 0 0 0 15 2 }{PSTYLE "Title" 0 18 1 {CSTYLE "" -1 -1 "" 1 18 0 0 0 0 0 1 1 0 0 0 0 0 0 }3 0 0 -1 12 12 0 0 0 0 0 0 19 0 }} {SECT 0 {PARA 18 "" 0 "" {TEXT -1 19 "Cantilevered Blocks" }}{PARA 0 " " 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 53 "This worksheet is \+ designed to accompany Chapter 4 of " }{TEXT 256 87 "Introduction to Sc ientific Programming: Computational Problem Solving Using Maple and C " }{TEXT -1 98 " by Joseph L. Zachary. In it, we will use Maple to ex plore the block-stacking problem. (08Oct96)" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 15 "Getting Started" }} {EXCHG {PARA 0 "" 0 "" {TEXT -1 252 "To use this worksheet you will ne ed to use some Maple extensions that we have created. Read in our blo cks package by evaluating the Maple command below. (You will need to \+ have first installed our custom Maple library and configured Maple to \+ use it.) " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 13 "with(blocks);" }}}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 22 "Calculating Extensions" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 370 "The three functions defined in the library are described in Chapter 4 of \+ the text. Each takes as its parameter the number of blocks in a stack of blocks arranged as described in the text and reports back the amou nt by which the top block extends beyond the table. For example, let' s compute (in three different ways) the extension that can be achieved with 100 blocks." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 86 "The \"blockFloat\" function uses floating-point arithmeti c to explicitly sum the series " }{XPPEDIT 18 0 "Sum(1/(2*i),i=1..n" " -%$SumG6$*&\"\"\"\"\"\"*&\"\"#F'%\"iGF'!\"\"/F*;\"\"\"%\"nG" }{TEXT -1 8 " . For " }{XPPEDIT 18 0 "n = 100" "/%\"nG\"$+\"" }{TEXT -1 13 " , that sum is" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 16 "blockFloat(100);" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {EXCHG {PARA 0 "" 0 "" {TEXT -1 287 "The \"blockRat\" function uses ra tional arithmetic to sum the same series. After it has computed an ex act sum, we use \"evalf\" to convert the result into a floating-point \+ number. The difference between this sum, and the one calculated with \+ \"blockRat\", can be attributed to roundoff error." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 21 "evalf(blockRat(100) );" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 110 "The \"blockFast\" function computes the sum of the series, not by adding up each term, but by using the formula " }{XPPEDIT 18 0 "(1 /2) (Psi(n+1) + gamma" "-*&\"\"\"\"\"\"\"\"#!\"\"6#,&-%$PsiG6#,&%\"nGF %\"\"\"F%F%%&gammaGF%" }{TEXT -1 58 ". Notice that we get the same an swer as with \"blockFast\"." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 15 "blockFast(100);" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 213 "\"blockFloat\" ca n be used to study how roundoff error can accumulate in a series of co mputations. Let's begin by setting Digits to 4, which means that we w ill be computing with four-digit floating-point mantissas." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 12 "Digits := 4 ;" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 103 "We can now compare the results of calling the three functions for increasingly large numbers of blocks." }}{PARA 0 "" 0 "" {TEXT -1 0 " " }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 16 "blockFloat(100);" }}{PARA 0 "> \+ " 0 "" {MPLTEXT 1 0 21 "evalf(blockRat(100));" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 15 "blockFast(100);" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 16 "blockFloat(200);" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 21 "evalf(blockRat(200));" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 15 "blockFast(200);" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 16 "blockFloat(400);" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 21 "evalf(blockRat(400));" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 15 "blockFast(400);" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 16 "blockFloat(800);" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 21 "evalf(blockRat(800));" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 15 "blockFast(800);" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 17 "blockFloat(1600);" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 22 "evalf(blockRat(1600));" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 16 "blockFast(1600);" }}}{PARA 0 "" 0 "" {TEXT -1 0 " " }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 22 "Notice what happens: " }} {PARA 15 "" 0 "" {TEXT -1 166 "blockFloat, which operates by directly \+ summing the series using floating-point arithmetic, suffers more and m ore from roundoff error as the number of terms increases." }}{PARA 15 "" 0 "" {TEXT -1 234 "blockRat, which operates by directly summing the series using rational number arithmetic, always returns an exact answ er. We then convert that exact answer into a floating-point number th at is exact to the available number of digits." }}{PARA 15 "" 0 "" {TEXT -1 167 "blockFast, which operates by exploiting specialized math ematical knowledge about the Harmonic series, returns a number that is exact to the available number of digits." }}{PARA 15 "" 0 "" {TEXT -1 173 "blockRat is much slower than blockFloat for large series, but \+ neither is practical for dealing with extremely large series. blockFa st, on the other hand, is extremely fast." }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{SECT 1 {PARA 4 "" 0 "" {TEXT -1 9 "Exercises" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 135 "Let's go back to 10-digit mantissas and \+ explore how roundoff error accumulates when doing a long series of flo ating-point calculations." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 " > " 0 "" {MPLTEXT 1 0 13 "Digits := 10;" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 124 "Using blockFast, we can fi nd the extension possible with ten thousand blocks. (We save the resu lt in the variable \"truth\".)" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {PARA 0 "> " 0 "" {MPLTEXT 1 0 26 "truth := blockFast(10000);" }}} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 135 "L et's use blockFloat to do the same calculation using mantissa lengths \+ ranging from 1 to 10. For example, with a five-digit mantissa, " }} {PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 12 "Dig its := 5;" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 73 "the result is as follows. (We save the result in the var iable \"result\".)" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 " " {MPLTEXT 1 0 28 "result := blockFloat(10000);" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 35 "and after going ba ck to ten digits " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 " " {MPLTEXT 1 0 13 "Digits := 10;" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {EXCHG {PARA 0 "" 0 "" {TEXT -1 54 "we find the that relative error is approximately 2.8%." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 28 "abs(truth - result) / truth;" }}}{PARA 0 "" 0 " " {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 245 "You should do s imilar calculations using for each mantissa length from 1 to 10. When you are done, you will know the relative error for each mantissa leng th that is exhibited by blockFloat when calculating the extension for \+ ten thousand blocks." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 " " {TEXT -1 475 "You can visualize the effect of mantissa length on rel ative 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 th e numbers 1 through 10 against some arbitrarily chosen numbers. You w ill have to replace the arbitrarily chosen numbers with the relative e rrors that you have calculated. For example, [5, 30] would become [5, .0277]. (Rounding the relative errors to three decimal places is OK. )" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 50 "plot([[1, 88], [2, 22], [3, 14], [4, 13], [5, 30]," }}{PARA 0 "> \+ " 0 "" {MPLTEXT 1 0 50 " [6, 0], [7, 2], [8, 99], [9, 17], [10, 8 8]]," }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 36 " 0..10, style=POINT, sy mbol=BOX," }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 61 " title = `Relative error when computing blockFloat(100)`," }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 44 " labels = [`Mantissa length`, `Error`]);" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 165 "It is also int eresting to compare the amount of time required by blockFloat and bloc kRat to do their calculations. Let's begin by going back to ten-digit mantissas." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 13 "Digits := 10;" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {EXCHG {PARA 0 "" 0 "" {TEXT -1 330 "For block stack sizes of 100, 200 , 400, 800, 1600, and 3200, let's calculate the time required to calcu late the extension using both blockFloat and blockRat. For example, t he time required to compute blockFloat(100) is the second number displ ayed by this sequence of commands. (The first is the result of the ca ll to blockFloat.)" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 " " {MPLTEXT 1 0 49 "start := time(): blockFloat(100); time() - start;" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 279 "The timing function is so crude that for quick calculations, it i s a good idea to do several calculations and calculate an average. To obtain a better timing result for blockFloat(100), for example, we ca n do eight identical calculations and then divide the overall result b y 8." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 174 "start := time(): blockFloat(100): blockFloat(100): blockFloat(1 00): blockFloat(100): blockFloat(100): blockFloat(100): blockFloat(100 ): blockFloat(100); (time() - start) / 8;" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 124 "For long calculations s uch as blockFloat(3200), of course, this would be unnecessary and woul d take an extremely long time. " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }} {PARA 0 "" 0 "" {TEXT -1 220 "You should time blockFloat for stack siz es of 100, 200, 400, 800, 1600, and 3200. You can plot your results b y completing the following command. You will need to insert the times that you obtain in place of the zeroes." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 68 "plot([[100, 0], [200, 0], [4 00, 0], [800, 0], [1600, 0], [3200, 0]]," }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 38 " 0..3200, style=POINT, symbol=BOX," }}{PARA 0 "> \+ " 0 "" {MPLTEXT 1 0 54 " title = `Time in seconds required by bloc kFloat`," }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 38 " labels = [`Stack s ize`, `Time`]);" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 134 "You should now repeat the timing experiments using \+ blockRat instead of blockFloat. Plot your results by completing the c ommand below." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 68 "plot([[100, 0], [200, 0], [400, 0], [800, 0], [1600, \+ 0], [3200, 0]]," }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 38 " 0..3200, st yle=POINT, symbol=BOX," }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 52 " titl e = `Time in seconds required by blockRat`," }}{PARA 0 "> " 0 "" {MPLTEXT 1 0 38 " labels = [`Stack size`, `Time`]);" }}}{PARA 0 " " 0 "" {TEXT -1 0 "" }}{EXCHG {PARA 0 "" 0 "" {TEXT -1 87 "Compare the shapes of the two curves that you create and try to explain the diffe rence." }{MPLTEXT 1 0 0 "" }}}}}}{MARK "4" 0 }{VIEWOPTS 1 1 0 1 1 1803 }