Bisection Method-Numerical Methods-Handouts, Lecture notes of Mathematical Methods for Numerical Analysis and Optimization

This is handout for Numerical Methods course by Raj Shrman at Bengal Engineering

Typology: Lecture notes

2011/2012

Uploaded on 07/06/2012

javed
javed 🇮🇳

4.5

(6)

38 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
The Bisection Method
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.
The Intermediate Value Theorem
A function fis continuous on the interval [a, b] if the graph of fcan 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 fis continuous on the interval [a, b].
x
y
a c b
f(a)
K
f(b)
f
Figure 1: A continuous function assumes all values between f(a) and f(b).
Further, note that for any intermediate value Kbetween f(a) and f(b), there exists a cbetween
aand bsuch that f(c) = K. This property is known as the Intermediate Value Theorem.
1
docsity.com
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Bisection Method-Numerical Methods-Handouts and more Lecture notes Mathematical Methods for Numerical Analysis and Optimization in PDF only on Docsity!

The Bisection Method

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.

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)

K

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

  • 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

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.

  1. 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.
  2. 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

The Grading Rubric

The following rules define the rubric I will use to grade your program.

  1. (30 points) Will be awarded for adequate comments. Comments should include: (a) A description of the program’s purpose. (b) Your name, the date of submission, and the version or revision number. (c) A complete dictionary of all variables and parameters used in the program.^1 (d) Interprogram comments should proceed any code snippets explained by the comments. These should be adequately sprinkled throughout your code.
  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 effort. 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 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.

Managing Files and Folders

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.