CS 2073, Spring 2006
 Program 5
 Roots Using Functions
    Week 5: Feb 13-17
 Due (on time): 2006-02-24  23:59:59
 Due (late):        2006-02-27  23:59:59

Program 5 must be emailed to: nrwagner@cs.utsa.edu
following directions for: running and submitting a C program, with deadlines:
  • 2006-02-24  23:59:59 (that's Friday, 24 February 2006, 11:59:59 pm) for full credit.
  • 2006-02-27  23:59:59 (that's Monday, 27 February 2006, 11:59:59 pm) for 75% credit.


Introduction: I messed up the last program. Initially it was supposed to use functions, but there wasn't time to cover this topic, so I removed the functions from some places, but left them in the notation elsewhere, making the assignment very confusing. The objective of this programming assignment is to redo Program 4 using functions and function notation.

The function you should focus on is still the function f(x) = cos(x) - x   or   y = cos(x) - x . A root of this function will be a real number x satisfying cos(x) = x , where 0 <= x <= pi/2 , given in radians.

You will also be using a second function: f(x) = x3 - 2   or   y = x3 - 2 .

Finally, for Newton's method, you will need the derivative of these functions. You should write this derivative as another function. We can't call the derivative f' because this would be illegal in C. Instead, you should choose a name for the derivative function, say fp.


Two Different Programs: As before, you are to write two different program, but this time each of them using function notation.


First Program: Newton's method: First use Newton's method as before, but this time use function notation.

You should use the ``magic'' formula in the following form in your program:

Separately, your program needs to define the functions f(x) = cos(x) - x and fp(x) = -sin(x) - 1. I will go over how to define functions in class, or you can look at the Week 6 Lectures, which include the page Introduction to functions.

Proceed exactly as before, but with somewhat different (and more sophisticated) notation.


Second Program: the Bisection Method: In the second program, my write-up used function notation all over the place. This time you should directly make use of the function notation. Here is my write-up from before:

The second program will use the bisection method. Here one starts with two values x1 and x2 (with x1 < x2) for which f(x1) and f(x2) have opposite signs (i.e., one is positive and the other is negative, or f(x1) * f(x2) < 0). If f is a continuous function (no jumps or breaks), then there must be an x between x1 and x2 for which f(x) = 0. To get closer to that value, let xmid = (x1 + x2)/2, the midpoint. Consider f(xmid) and replace either x1 or x2 by xmid, so that after the replacement we still have f(x1) and f(x2) with opposite signs, but now x1 and x2 are half as far apart as they were before. Repeat this process until the distance between x1 and x2 is less than epsilon, again taken to be 0.000000000001 in this assignment. Your final answer should be xmid.

For the particular example here, you should take x1 = 0.0 , x2 = PI/2.0 , and f(x) = cos(x) - x.

Notice that a convenient way to check if f(x1) and f(xmid) have opposite signs is to check if f(x1)*f(xmid) < 0. Either this is true, or else f(x2) and f(xmid) have opposite signs. In the first case you set x2 = xmid and in the second case you set x1 = xmid.


Directions for both programs: Finish the two programs exactly as before, only with the function notation.


Use a second function: To see that this is more flexible, you can change the function definitions to finds the roots of a second funtions. Let's try f(x) = x3 - 2, so that we will be finding the qube root of two. Notice that you also have to change the definition of the derivative fp. You may also have to change your starting point x0 in the first program, where any value greater than 0 should work, and your two starting points x1 and x2 in the second program, where as before it must be true that f(x1) and f(x2) have opposite signs, that is that f(x1)*f(x2) < 0.


What you should email: Refer to the submissions directions and to deadlines at the top of this page. The text file that you submit should first have Your Name, the Course Number, and the Program Number. The rest of the file should have the following in it, in the order below, and clearly labeled, including at the beginning the appropriate item letters: a, b, c, etc.

 Contents of email submission for Program 5:

Last Name, First Name; Course Number; Program Number.

  1. C source for the first program that uses Newton's Method,
    and the first function, along with a run of this program.
  2. C source for the second program that uses the Bisection Method,
    and the first function, along with a run of this program.
  3. C source for the first program that uses Newton's Method,
    and the second function, along with a run of this program.
  4. C source for the second program that uses the Bisection Method,
    and the second function, along with a run of this program.

All programs must use the function notation.


Revision date: 2006-02-20. (Please use ISO 8601, the International Standard.)