





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
This is handout for Numerical Methods course by Raj Shrman at Bengal Engineering
Typology: Lecture notes
1 / 9
This page cannot be seen from the preview
Don't miss anything!






Abstract In this activity we investigate a method for finding the roots or zeros of a function, an algorithm known as the Bisection Method.
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].
x
y
a c (^) b
f (a)
f (b)
f
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.
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.
x
y
a b f (a)
f (b)
f
(^0) r
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 defined by
p(x) = 2x^3 + x^2 − 16 x − 8. (1)
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=2x3+x2-16x- end function p end module table_mod
Looking for Changes in Sign
Let’s examine the content of our table more closely. We repeat the table here so that our readers won’t have to flip pages back and forth.
-5.0000 -153. -4.0000 -56. -3.0000 -5. -2.0000 12. -1.0000 7. 0.0000 -8. 1.0000 -21. 2.0000 -20. 3.0000 7. 4.0000 72. 5.0000 187.
Note that the x-values are in the first column, while the associated function values are in the second column. The first four function values are −153, −56, −5, and 12. Note the sign change in the last two function values. We go from a negative five 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> plot [-5:5] [-50:50] 2x3+x2-16x- gnuplot> set xtics -5, 1, 5 gnuplot> set grid gnuplot> replot
Note that the graph of p crosses the x-axis between −3 and −2, then between −1 and 0, and finally 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.
0
20
40
-5 -4 -3 -2 -1 0 1 2 3 4 5
2x3+x2-16x-
Figure 3: Locating zeros between consecutive integers.
Refining 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 =
Set the variables left = 2, mid = 2.5, and right = 3, then evaluate the function p defined in equation (1) at each of these points. These results are best summarized in a table.
x p(x) left 2 − 20 mid 2.5 − 10 right 3 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.
x p(x) left 2.5 − 10 mid 2.75 − 2. 8438 right 3 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.
x p(x) left 2.75 − 2. 8438 mid 2.8750 1. 7930 right 3 7
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.
program bisect_method use bisect_method_mod real :: left, right left = 2 right = 3 call bisect(p,left,right,TOL) end program bisect_method
The following rules define the rubric I will use to grade your program.
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.
Each of you has been given personal space on the sci-math server to store your work. In Windows, this space is mapped to the drive letter H. If you open the Windows Explorer (the file manager, not the internet browser), you can see that the drive letter has been mapped to your login name. In Linux, your personal space is in your home directory, typically /home/username. In this folder, create a new folder called fortran. Note that you must never use spaces in filenames. Also note that in the Windows operating system, filenames are not case-sensitive, which is exactly opposite to what happens in Unix and Linux, where filenames are case-sensitive. In your /home/username/fortran folder, create another folder called assign5. It is in this folder (/home/username/fortran/assign5) that you are to place the source code and executables for this current project. When you receive your next project, create a new folder called assign5 to hold that project, etc. If you work at home, I still want you to place copies of your work in the space reserved for you on our system. Simply copy your home files onto a floppy disk and bring them with you to school. In Windows, use the Explorer to copy the files on your disk into the proper folder on your H: drive. In Linux, you have to mount your floppy. Open a terminal window and enter this command.
mount /mnt/floppy
Make an assign5 directory.
cd /home/username/fortran mkdir assign
Change to the new directory.
cd assign
Copy the file bisect.f90 from your floppy to the current directory with the following command.^2
cp /mnt/floppy/bisect.f.
(^2) You may have to adjust the path to bisect.f90 if it is in some sort of subfolder on your floppy.