COS 323 - Computing for the Physical and Social Sciences

Fall 2010

Course home Outline and lecture notes Assignments


Assignment 1

Due Thursday, Sep. 30

The purpose of this assignment is to ensure that you are familiar with Matlab, and to explore the consequences of operation ordering on roundoff error. In particular, you will compare sequential summation to "cascaded" pairwise summation.

Matlab

Matlab is available on several OIT machines (look here for details). You should be able to run it remotely from any Unix workstation.

Alternatively, you can install it on your own matchine - look here for details.

Read through the following for a basic introduction to Matlab:


Roundoff Error and Precision

Please follow the instructions:

  1. Generate a vector of n random numbers
    Hint: "help rand". Be sure not to generate an n by n matrix!

  2. Convert the random numbers to single precision.
    Hint: help single

  3. Compute the sum of the random numbers by direct sequential accumulation.
    Hint #1: use a for loop, or find a way to use cumsum. Do not use sum, since that does not guarantee in which order order the summation will be performed.
    Hint #2: "format long" to see all the digits in the answer.

  4. Compute the sum by cascaded pairwise addition (i.e., add the first two numbers, then the next two, then the next two, etc. Then add the first two partial sums, etc. Repeat until you have one number.) All calculations should be in single precision.
    Hint: a = a(1:2:size(a)) + a(2:2:size(a));

  5. Compare the results of (3) and (4) to summing (1) in double precision

  6. Graph the error introduced in (3) and (4) as a function of n. Pick a number of values of n (e.g., powers of 2, 2^10 through 2^24) and compute the average absolute-value-of-error over 100 trials for each n. Save the graph to a png file.
    Hint #1: "help plot"
    Hint #2: Think about (and justify in your writeup) whether you want logarithmic axes for x, y, both, or neither.
    Hint #3: Use "File... Save As..." in the plot window, or print('-dpng', 'out.png')

  7. Comment on your findings. How does the error appear to grow as a function of n for each scheme?


Submitting

This assignment is due Thursday, September 30, 2010 at 11:59 PM. Please see the general notes on submitting your assignments, as well as the late policy and the collaboration policy.

Please submit:

The Dropbox link to submit your assignment is here.

Note that programming in Matlab is not an excuse to write unreadable code. You are expected to use good programming style, including meaningful variable names, a comment or three describing what the code is doing, etc. Also, all images you submit should have been saved with the "print" function or "Save As" - do not submit screen captures of the image window.


Last update 25-Sep-2010 17:45:20
smr at princeton edu