COS 323 - Computing for the Physical and Social Sciences

Fall 2012

Course home Outline and lecture notes Assignments


Assignment 0

Due Tuesday, Sep. 25

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.

I. Matlab

Matlab is available to install on your own machine - look here for details.

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

Read through the following for a basic introduction to Matlab:


II. 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?

FAQ

Please check out the Assignment 0 FAQ for frequently asked questions and answers.


Submitting

This assignment is due Tuesday, September 25, 2012 at 11:59 PM. Please see the course policies regarding assignment submission, late assignments, and collaboration.

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 13-Sep-2012 16:39:04
smr at princeton edu