DAY12
Thursday, 07 June 2012


Today is a LAB day.

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

We have a LAB #4 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.

I will talk for 15 minutes about an example of the break statement, (the program "next_binary.c") and then we will concentrate on the lab assignment.


LAB#4: Consider the following function:

        f(x) = cos ( x )  + 1
      
The (an) indefinite integral of f(x) is:
        g(x) = sin ( x ) + x
      
Therefore, for any interval [a,b], the integral of f(x) from a to b is:
        g(b) - g(a) = sin ( b ) + b - sin ( a ) - a
      

When we don't know an indefinite integral for our function, we have to estimate it. One way to estimate an integral over [a,b] starts by subdividing the interval into $n$ small subintervals. The first interval is [x0,x1], and so on. The left hand rule estimates the integral over [x0,x1] as:

        Estimate for subinterval [x0,x1] = f(x0) * ( x1 - x0 )
      
Therefore, we estimate the integral over [a,b] as the sum of these estimates:
        Integral is approximately = f(x0)*(x1-x0) + f(x1)*(x2-x1) + ... + f(xn-1)*(xn-xn-1)
      

Write a program which estimates the integral of our function f(x) over the interval [a,b], using n subintervals. To define n equal subintervals, we actually need n+1 points (not n!). What is the spacing that we will need? Your program needs a loop that looks at each subinterval. Inside the loop, you need to define the left and right endpoints, evaluate the function, and update the sum.

Run your program using the f(x) described above, a = 0, b = 4, and 20 subintervals.

Print:


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

Homework 4.1: Write a program that is given the value of a number nmax, and which then computes and prints out the number of divisors for each of the numbers 1, 2, 3, ... up to nmax. Note that 11 has 2 divisors, namely 1 and 11, while 12 has 6 divisors, which are 1, 2, 3, 4, 6 and 12. So part of your output should be:

        11: 2
        12: 6
      
When I programmed this, I used an "if" statement, which we haven't covered in class yet officially, although I've shown you some examples. For this assignment, you will probably need an if statement like the following:
        if ( condition )
        {
          statement;
        }
      
where "condition" could be a condition you are checking such as "n <= 2" or "i == j".

Test your program for nmax = 25. Print the table.

Homework 4.2: Newton's method helps us find solutions to equations. We can use this idea to find the square roots of numbers. Consider the equation r^2 = s, where we know s, but not r. Newton tells us to rewrite this as r^2 - s = 0, and then think of the problem as trying to solve f(r) = 0. We will need the derivative of f(r), in other words, f'(r) = 2*r. Now here's how we proceed:

        Let r be a guess for the solution;
        If |f(r)| is small enough, you're done (and very lucky!).
        Otherwise, replace r by r - f(r) / f'(r)
        If |f(r)| is small enough, you're done.
        Otherwise, replace r by r - f(r) / f'(r)
        and so on...
      

Write a program that uses Newton's method to estimate the square root of a number s. To keep things simple, your initial guess for r should be 1. Your check should be | f(x) | <= 0.000001. What kind of loop makes sense for this problem?

Test your program by looking for the square root of s = 14. Print the value of r at each step of the loop. Once you are done, print the final estimate, the exact square root of 14, and the error in your estimate.

Homework 4.3:

Another way to estimate an integral is known as the midpoint rule. Given a function f(x) and an interval [a,b], we again divide the interval into n subintervals. Again, we estimate the total integral by estimating its value over each subinterval. The left hand rule used an estimate involving the left endpoint of the interval. The midpoint rule uses the midpoint:

        Estimate for subinterval [x0,x1] = f((x0+x1)/2) * ( x1 - x0 )
      

Write a program which estimates the integral of our function f(x) over the interval [a,b], using n subintervals. To define n equal subintervals, we actually need n+1 points (not n!). What is the spacing that we will need? Your program needs a loop that looks at each subinterval. Inside the loop, you need to define the left and right endpoints, compute the midpoint, evaluate the function, and update the sum.

Run your program using the f(x) described above, a = 0, b = 4, and 20 subintervals.

Print your estimate for the integral, the exact value, and the error.

For each homework program, turn in a copy of the program, and a copy of the output.


Programs we might discuss:


Last revised on 06 June 2012.