Professional Documents
Culture Documents
Introduction
Unlike our previous activities, no code will be discussed or presented in this article. Rather, the idea and concept of the Bisection Method will be explained, but you will be completely responsible for the coding of the algorithm. The Intermediate Value Theorem A function f is continuous on the interval [a, b] if the graph of f can be drawn from the point (a, f (a)) to the point (b, f (b)) without ever lifting your pencil from the paper. Consider the image in Figure 1. Note that you can trace the curve without lifting your pencil from the paper, when moving the point of your pencil from the point (a, f (a)) to the point (b, f (b)). The function f is continuous on the interval [a, b].
y f
f (b)
K f (a) a c b x
Figure 1: A continuous function assumes all values between f (a) and f (b). Further, note that for any intermediate value K between f (a) and f (b), there exists a c between a and b such that f (c) = K. This property is known as the Intermediate Value Theorem. 1
Theorem 1 If f is continuous on the closed interval [a, b] and K is any intermediate value between f (a) and f (b), then there exists a c in [a, b] such that f (c) = K. The intermediate value theorem is very useful when nding roots or zeros of continuous functions. The idea is pretty simple. Suppose that the graph of f passes through the horizontal axis at the point r. Then the graph of f will be above the axis on one side of r and below the axis on the other side of r. In the graph in Figure 2, note that f (a) < 0 and f (b) > 0. Further, note that zero is a value between these two values of f ; i.e., f (a) < 0 < f (b). Consequently, the intermediate value theorem guarantees that there is a zero r between a and b such that f (r) = 0.
y
f f (b) 0 f (a)
a r b
Figure 2: There must be a zero between a and b. When the graph of a function is above the horizontal axis, the function is positive. When the graph is the below the axis, the function is negative. Hence, when a function passes from positive to negative (or negative to positive), the intermediate value theorem guarantees that the graph of the function must pass through the horizontal axis. The point where the graph crosses the horizontal axis is called a zero or root of the function. Consequently, one way to determine the existence of zeros is to create a table of function values. Suppose, for example, that we wish to locate the zeros of the polynomial dened by p(x) = 2x3 + x2 16x 8. It is a simple task to craft a program that will create a table of values for this polynomial. module table_mod public :: p contains function p(x) result (y) real, intent(in) :: x real :: y y=2*x**3+x**2-16*x-8 end function p end module table_mod 2 (1)
program table use table_mod real :: x x=-5.0 do if (x>5.0) then exit else print "(2f10.4)", x, p(x) x=x+1.0 end if end do end program table Format Statements Most of the code in table program should be familiar. However, there is one new construct, the format statement in the following line. print "(2f10.4)", x, p(x) Note the neat appearance of the output. -5.0000 -153.0000 -4.0000 -56.0000 -3.0000 -5.0000 -2.0000 12.0000 -1.0000 7.0000 0.0000 -8.0000 1.0000 -21.0000 2.0000 -20.0000 3.0000 7.0000 4.0000 72.0000 5.0000 187.0000 Note that the usual print * has been replaced with print "(2f10.4)". Lets break this down carefully. The * is known as free format. When we write print *, we are instructing the compiler to use the default formatting. Sometimes free format is sucient, but we will often nd that we would like more control over the formatting of output. Suppose that we had written print "(f10.4)", x instead of what we have above. At run time, the program will print the value of x using the format dened by f10.4. This means that oating point format will be used, 10 spaces will be reserved for the oating point number (a number with a decimal point), 4 of which are reserved for places to the right of the decimal point.
We could have written print "(f10.4,f10.4)", x, p(x) in the table program. This would produce the same output as the format used in print "(2f10.4)", x, p(x). In the latter case, the 2 in front of f10.4 indicates that we are to use this format twice, once for the contents of the variable x, then again for the ouput of the function. This use of a repetition factor makes coding format statements easier. It is also easier to read. Looking for Changes in Sign Lets examine the content of our table more closely. We repeat the table here so that our readers wont have to ip pages back and forth. -5.0000 -153.0000 -4.0000 -56.0000 -3.0000 -5.0000 -2.0000 12.0000 -1.0000 7.0000 0.0000 -8.0000 1.0000 -21.0000 2.0000 -20.0000 3.0000 7.0000 4.0000 72.0000 5.0000 187.0000 Note that the x-values are in the rst column, while the associated function values are in the second column. The rst four function values are 153, 56, 5, and 12. Note the sign change in the last two function values. We go from a negative ve to a positive twelve. At x = 3, p(x) = 5, but then at x = 2, p(x) = 12. At x = 3 the function is below the x-axis, but at x = 2, the function is above the x-axis. This change in sign indicates that there must be a zero crossing between x = 3 and x = 2. That is, the function has a zero between x = 3 and x = 2. In similar fashion note that p(1) = 7, but p(0) = 8. This can only happen if the graph of the function passes through the axis between x = 1 and x = 0. Finally, there is one remaining sign change, between x = 2 and x = 3, where p(2) = 20, but p(3) = 4. Because the function passes from negative to positive, there must be a zero crossing between x = 2 and x = 3. You can also use gnuplot to locate the zeros we found using the table. The image in Figure 3 was produced by the following code. gnuplot> gnuplot> gnuplot> gnuplot> plot [-5:5] [-50:50] 2*x**3+x**2-16*x-8 set xtics -5, 1, 5 set grid replot
Note that the graph of p crosses the x-axis between 3 and 2, then between 1 and 0, and nally between 2 and 3. Finally, note that these are precisely the same as the zero crossings predicted by the sign changes in the output of our program table.
40 20 0 -20 -40 -5 -4 -3 -2
2*x**3+x**2-16*x-8
-1
Figure 3: Locating zeros between consecutive integers. Rening the Estimates of the Zeros Let us concentrate our attention on the zero crossing between x = 2 and x = 3. Calculate the point that is midway between x = 2 and x = 3 (the midpoint). midpoint = 2+3 = 2.5 2
Set the variables left = 2, mid = 2.5, and right = 3, then evaluate the function p dened in equation (1) at each of these points. These results are best summarized in a table. left mid right x 2 2.5 3 p(x) 20 10 7
Make sure that you check these results with a calculator. Note the change in sign between the midpoint and the right endpoint. This means that the graph crosses the x-axis between these two points. Set left = mid, calculate the midpoint, then evaluate the function at left, mid, and right. left mid right x 2.5 2.75 3 p(x) 10 2.8438 7
Again, check these results with your calculator. Note the sign change between the midpoint and right endpoint. Again, set left = mid, then evaluate the function at left, mid, and right. left mid right x 2.75 2.8750 3 p(x) 2.8438 1.7930 7
Again, be sure to check these results with your calculator. This time, note that the sign change occurs between the left endpoint and the midpoint. Therefore, the graph must cross the x-axis between these two points. Set right=mid, then evaluate the function at left, mid, and right. left mid right x 2.75 2.8125 2.8750 p(x) 2.8438 0.5952 1.7930
Again, be sure to check these results with your calculator. Note that the sign change occurs between the midpoint and right endpoint. Therefore, the graph must cross the x-axis between these two points. Set left = mid, then evaluate the function at left, mid, and right. left mid right x 2.8125 2.8438 2.8750 p(x) 0.5952 0.5812 1.7930
Again, be sure to check these results with your calculator. Iterate If you continue in this manner, you can nd the zero crossing to within a desired tolerance. At each iteration, if the sign change occurs between the left endpoint and the midpoint, then set right = mid and continue. Otherwise, the sign change occurs between the midpoint and right endpoint, so set left = mid and continue. Obviously, this is coded in a do loop, but what stopping criterion should we use? One idea is to exit the loop when the distance between the left and right endpoints is less than some tolerance.
The Assignment
Your assignment is to code the bisection method described in the preceding paragraphs. This will be your rst challenging assignment, as I will not provide you with a single line of code for either the function or the subroutine (I will provide code for the main program). Here are your program requirements. 1. Construct a program module named bisect method mod. Your module should declare and initialize a constant tolerance of TOL = 1.0 103 . 2. Your module must also contain a function named p that will aid in the evaluation of the function dened by p(x) = 2x3 + x2 16x 8. (2) 3. Your module shall also contain a subroutine named bisect which will perform the bisection method on the given function. The subroutine header shall look like the following. subroutine bisect(p,a,b,tol) 6
The argument p is assigned the function, a and b are real variables assigned to the left and right endpoints of the search interval, and tol is the tolerance. The iterations should cease when the distance between the left and right endpoints of the interval is less than the tolerance. 4. At each step of the iteration, your subroutine is to print (a) (b) (c) (d) (e) (f) the the the the the the left endpoint, midpoint, right endpoint, function evaluated at the left endpoint, function evaluated at the midpoint, and function evaluated at the right endpoint.
These should be arranged neatly in six columns on the computer screen. Use a format statement f10.4 for each column. 5. Your main program should use your module and set the intitial search interval. Indeed, please use the following code for your main program. Use this code exactly as it appears. Nothing more, nothing less, or points will be deducted. program bisect_method use bisect_method_mod real :: left, right left = 2 right = 3 call bisect(p,left,right,TOL) end program bisect_method
2. (50 points) Will be awarded if the program works and does what it was asked to do. 3. (10 points) Will be awarded for good program style. This includes good indentation practices, etc. Proper use of Emacs should insure good programming style. 4. (10 points) Will be awarded for creativity and extra eort. Did you just do the bare minimum? Or did you stretch and reach a little higher? Did you put something cute or clever into your program that nobody else seemed to think of?
1 See http://www.netlib.org/lapack/single/sgesv.f for an example of production quality code. This is an excellent example of how professionals comment their code.
Penalties
Each program that is assigned during the term will have a due date. On that date, the program must be on the instructors desk before the start of class. Penalties will be assessed as follows. 1. (10 points) There will be a 10 point deduction for any program that is handed in after the class has begun. 2. (20 points) There will be a 20 point deduction per class period. That is, if you hand the program in one class period late, there is an automatic 20 point deduction. Two class periods warrants a 40 point deduction, etc. To be clear, if the program is in the instructors hands before the beginning of the next class, that is a 20 point deduction. If the program is in the instructors hands before the start of the second class period past the due date, that is a 40 point deduction, etc.
may have to adjust the path to bisect.f90 if it is in some sort of subfolder on your oppy.
Note that the dot represents the current directory. Hence, this command copies the le newton.f90 from your oppy to the current directory. Finally, you have to unmount your oppy before removing it from the computer. This is the only safe way to make sure all lesystems are closed on the oppy and prevent the corruption of les. umount /mnt/floppy It is now safe to remove the oppy from the disk drive. If everyone follows these simple rules, I can easily access your work from my oce machine for purposes of assigning a grade.
Caveat
On this project, if you stop by my oce with hardcopy of your program before the due date of this assignment, I will give a quick glance and critique of your source code. Somewhat like receiving a grade on a draft before submitting your nal draft for assessment.