Professional Documents
Culture Documents
David Arnold
February 25, 2003
Abstract
In this activity we investigate a method for finding the roots or zeros of a function, an
algorithm known as the Bisection Method.
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.
f (b)
f (a)
x
a c b
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 finding 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 (b)
a
0 x
r b
f (a)
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 defined by
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
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.
• The * is known as “free format.” When we write print *, we are instructing the compiler to
use the default formatting. Sometimes “free format” is sufficient, but we will often find 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 defined by f10.4. This means
that floating point format will be used, 10 spaces will be reserved for the floating point number
(a number with a decimal point), 4 of which are reserved for places to the right of the decimal
point.
3
• 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.
4
2*x**3+x**2-16*x-8
40
20
-20
-40
-5 -4 -3 -2 -1 0 1 2 3 4 5
5
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. There-
fore, the graph must cross the x-axis between these two points. Set right=mid, then evaluate the
function at left, mid, and right.
x p(x)
left 2.75 −2.8438
mid 2.8125 −0.5952
right 2.8750 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.
x p(x)
left 2.8125 −0.5952
mid 2.8438 0.5812
right 2.8750 1.7930
Again, be sure to check these results with your calculator.
Iterate
If you continue in this manner, you can find 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 first 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 × 10−3 .
2. Your module must also contain a function named p that will aid in the evaluation of the
function defined 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) the left endpoint,
(b) the midpoint,
(c) the right endpoint,
(d) the function evaluated at the left endpoint,
(e) the function evaluated at the midpoint, and
(f) the 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
7
Penalties
Each program that is assigned during the term will have a due date. On that date, the program
must be on the instructor’s 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 instructor’s hands
before the beginning of the next class, that is a 20 point deduction. If the program is in the
instructor’s hands before the start of the second class period past the due date, that is a 40
point deduction, etc.
cd assign5
Copy the file bisect.f90 from your floppy to the current directory with the following command.2
cp /mnt/floppy/bisect.f90 .
2 You may have to adjust the path to bisect.f90 if it is in some sort of subfolder on your floppy.
8
Note that the dot represents the current directory. Hence, this command copies the file newton.f90
from your floppy to the current directory.
Finally, you have to unmount your floppy before removing it from the computer. This is the
only safe way to make sure all filesystems are closed on the floppy and prevent the corruption of
files.
umount /mnt/floppy
It is now safe to remove the floppy from the disk drive.
If everyone follows these simple rules, I can easily access your work from my office machine for
purposes of assigning a grade.
Caveat
On this project, if you stop by my office 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 final draft for assessment.