Problems: In this lab, you are to write solutions to the following two problems. In each case, turn your solutions utilizing the format for submitting assignments.
In particular, your program should distinguish the following cases:
Notes:
gcc -o example example.c -lm
Here, the -lm flag appears at the end of the line, as it is an option passed to the linker from the compiler -- the compiler finds the correct library without the need for the flag.
In the following problem, use the following C format to define a function f(x)of a real parameter x that returns a real result:
double f (double x) {
}
Using the notation above, the Bisection Method proceeds as follows for an iteration:
In the second and third cases, the interval containing the root has been cut in half, from [a, b] to [a, c] or [c, b]. For the next steps, the above process is repeated, with the interval containing the root being halved each time. Once the root has been trapped in a very small interval (smaller than epsilon), the midpoint of that interval provides an approximation to the root.
Example: The square root of 2 can be approximated as a root of the function f(x) = x2 - 2. If we start with [a, b] = [1, 2], and continue until the length of the interval is less than 0.01, we might obtain the following output:
left endpoint right endpoint 1.000000 2.000000 1.000000 1.500000 1.250000 1.500000 1.375000 1.500000 1.375000 1.437500 1.406250 1.437500 1.406250 1.421875Since the last subinterval has length 0.0078125, less than the 0.01 required, the midpoint 1.14196875 of the last interval is the desired approximation. (Note: This agrees with the true value of the square root of 2, which is 1.141431, to two decimal places.)
This document is available on the World Wide Web as
http://www.math.grin.edu/~walker/courses/213.fa98/lab-intro-to-c.html