Creative Commons License Copyright © Michael Richmond. This work is licensed under a Creative Commons License.

Project 2: Calculating infinite series

Thursday, March 22, at 6:00 PM
Monday, March 26, at noon
the finished Scilab code and analysis

There are two tasks in this assignment. Both are designed to point out dangers of numerical calculation on computers.

The infinite series

                  1         1     1     1
   sum over n   [---]   =  --- + --- + --- + ....
                 n^2        1     4     9 
should add up to one-sixth of pi squared.

Part I

  1. Write a function called calc_sum which takes a single input argument, num_terms; the argument indicates how many terms to include in the summation.
               function series_sum = calc_sum(num_terms)
    The function should return the value of the sum after num_terms have been added. This function should start with n = 1 and continue adding terms up to (and including) n = num_terms.

  2. Use this function to calculate the value of pi. Determine the number of terms you must include to yield pi correct to 1, 2, 3, 4, 5, and 6 digits. Write all this down. Added for clarity: "3" is correct to 1 digit. "3.1" is correct to 2 digits. "3.14" is correct to three digits.

  3. What is the relationship between the number of correct digits and the number of terms in this series? That is, can you find a relatively simple function which predicts the number of terms needed to reach a particular number of digits?

  4. Make a table showing the amount of time it takes to calculate the value of pi correct to 1 to 7 digits. Using your table, extrapolate to estimate the length of time it would take to calculate pi correct to 20 digits. It's probably a good idea to ignore the first few times, since they aren't long enough to be meaningful.

  5. The observable universe is about 15 billion light years in radius. Suppose you use a value of pi which is wrong in the 20th digit to calculate the circumference of the universe. How large an error will you make, in meters?

Bells and Whistles Find a better way to calculate the value of pi, without calling built-in inverse trigonometric functions. That is, find a better series approximation to pi. Write down the series, and your source for the information. How many terms of this better series are required to calculate pi accurate to 6 digits?

Compare the speeds of your original function and this improved function. How long does each take to calculate pi to 5 decimal places? To 6 decimal places? How long would your improved function take to reach 20 decimal places?

Part II

  1. Now, write another function called calc_sum_with_offset, which takes two arguments, num_terms and offset. The first argument is the same as before: the number of terms to include in the sum. The other argument is a number that is added to the sum at the start, then subtracted at the end, like this:
      sum = offset
      (go into loop to add terms for each integer from 1 to num_terms)
      sum = sum - offset
    Since we add and subtract the same value at the start and end, it shouldn't make any difference in the result -- right?

  2. Use this function to calculate the value of pi. Set num_terms at 10000 and leave it there; vary the offset from 1 to 1e16 by factors of 10 (that is, 1, 10, 100, ...). For each value of offset, add the sum to the offset, then subtract the offset; and use the amount left over to calculate pi. Make a table showing the value of pi for each value of offset.

  3. Discuss your results. Explain in particular what happens when offset has its largest value. Using your table, try to determine how many bits Scilab uses to represent the mantissa of a floating-point number.

Bells and Whistles Find the location in the Scilab source code which defines the default floating-point variable type. Print out a section of the source code. Explain in your own words how many bytes and how many bits are used for this type of variable.

This page maintained by Michael Richmond. Last modified Mar 22, 2007.

Creative Commons License Copyright © Michael Richmond. This work is licensed under a Creative Commons License.