DAY09
Thursday, 31 May 2012


Today is a LAB day.

HOMEWORK #2 is due today. You should submit it to Detelina by email.

We have a LAB #3 to be done during class time, for credit. When you have completed the lab, please let Detelina know so that she can approve and record your work.


LAB#3: A for loop makes it easy to count, or to carry out operations that are similar to counting. A common task in scientific computing is to examine regularly spaced real numbers in an interval [a,b]. For example, we might want to print out the tenths (0/10, 1/10, 2/10...) between 0 and 1. How can we do this with a for loop?

One way that will work begins by asking how many points we want to compute, which we'll call n. The length of the interval is b-a, so if the intervals are equal, neighboring points are spaced (b-a)/(n-1) apart. So we start at a, and then want to compute the following sequence of values for x:

        x = a, a+(b-a)/(n-1), a+2*(b-a)/(n-1), ..., b
      
but, thinking about the for loop index i, we can write:
        x = a+0*(b-a)/(n-1), a+1*(b-a)/(n-1), a+2*(b-a)/(n-1), ..., a+(n-1)*(b-a)/(n-1)
      
so now we should be able to see a simple relationship between the counting variable i and the number x.

Part 1: write a program which uses a for loop to compute and print n equally spaced numbers x between a and b. The values n, a, and b should be stored as variables. To test your program, have it compute 6 equally spaced values between 10.0 and 20.0. Is your third number 14?

Part 2: modify your program so that on each step, after we compute the current point x, we evaluate the following formula:

        f = ( x + 1 ) * ( x - 1 )^2 * ( 4 - x )
      
and then print the value of x and f:
        printf ( "  %f  %f\n", x, f );
      
Try your program with a = 0, b = 4, n = 3. Is your second result x = 2, f = 6?

Change n to 101, but leave a = 0, b = 4. Run the program. We want to save the output to a file called "data.txt". You can always do this with cut and paste, but on unix, you can also do it this way:

        ./a.out > data.txt
      
There should be 101 lines in the file, and each line should be simply a value of x and a value of f.

Part 3: Once you have your data file, make a plot with gnuplot:

        gnuplot
          set style data linespoints
          plot "data.txt" using 1:2
      
When you have your plot done, show Detelina, and you're done! (To exit from gnuplot, just type "quit".)


HOMEWORK #3 (must be turned in by next Thursday):

3.1: The "double factorial" function is symbolized by n!!. The double factorial is similar to the factorial, but here are the formulas for the first five:

        1!! = 1
        2!! = 2
        3!! = 3 * 1
        4!! = 4 * 2
        5!! = 5 * 3 * 1
      
so the double factorial multiplies every other number from n, down. Demonstrate your program by computing and printing 12!!

Write a program that can evaluate the double factorial. If you think you understand the factorial example, you can try to set up your double factorial as a separate function that your program calls. Otherwise, you can do everything as part of a single program.

3.2: The natural logarithm function log(x) has the following Taylor series for x close to 1:

        log(x) = (x-1) - (x-1)^2/2 + (x-1)^3/3 - (x-1)^4/4 + ...
      
Write a program that estimates the logarithm of a number x by adding up n terms of this sequence, using a for loop. Demonstrate your code by approximating log(1.5) using 7 terms. Print your approximate value and the exact value, which can be computed by the C expression log ( 1.5 ). To access the C log() function, you will need to #include <math.h> and then add the switch "-lm" on your compile statement.

Turn in: send email to Detelina by Thursday, June 07, including:



Last revised on 31 May 2012.