Prog 2
Prog 2
Fall 2011
1
y
f(x)
x
a c b
f(x)
x
a c e b
2
while f(e) != 0:
. . .
However, we always know that c is between a and b, and since e is halfway between a and b, we
know that the distance from c to e is at most (b − a)/2. (Look at the x-axis in Figure 2.) In other
words, the “error” in the estimate e is ≤ (b − a)/2.
So if we input a maximum acceptable error tol we can repeat the loop until (b − a)/2 ≤ tol.
This gives us the following algorithm:
e = (a + b)/2
max_err = (b-a)/2
while max_err > tol:
if f(a)*f(e) < 0:
b = e
else
a = e
# new estimate
e = (a + b)/2
max_err = (b-a)/2
In case f (e) is exactly zero, we can immediately exit from the loop using Python’s break
statement:
e = (a + b)/2
max_err = (b-a)/2
while max_err > tol:
if f(e) == 0:
break
elif f(a)*f(e) < 0:
b = e
else
a = e
# new estimate
e = (a + b)/2
max_err = (b-a)/2
The break statement will jump out of the loop and continue executing the first statement in the
program after the while loop.
If a and b are chosen so that f (a)f (b) < 0, and if f (x) is a reasonably nice function, the bisection
method is guaranteed to eventually find an approximate solution to f (x) = 0. However, it can take
a very long time to find c. To avoid this problem, we should add an additional condition to the
while statement: if the number of passes through the loop body is too large, we can leave the loop
and our bisection function can return None.
2 Program 2
For programming assignment 2, you should write a Python program that uses the bisection method
to solve an equation having the form f (x) = 0. Input to the program will be
3
• max iter: the maximum number of times the program can execute the body of the while
loop,
3 Due Date
In order to receive full credit, your program must be in the p2 subdirectory of your Subversion
repository by 2:00 pm on Friday, October 7, and you must turn in a print out of your program by
5 pm on the 7th.
4 Grading
Your program will be graded on the basis of its correctness and its “static features.”
1. Correctness will be 50% of your grade. Does your program find a solution with the desired
accuracy when the bisection method finds an approximate solution within the required number
of passes? Does the program print a message when it doesn’t find the solution? Does it print
the number of passes? Does it keep prompting for a and b until f (a)f (b) < 0?
(a) Function use will be 20% of your grade. Does your program use the two required
functions — the bisection function and the function f (x). Do they perform as required?
4
(b) Documentation will be 10% of your grade. Does your header documentation include
the author’s name, the purpose of the program, and a description of how to use the
program? Are the identifiers meaningful? Are any obscure constructs clearly explained?
(c) Source format will be 10% of your grade. Is the indentation consistent? Have blank
lines been used so that the program is easy to read?
(d) Quality of solution will be 10% of your grade. Does your solution contain unnecessary
calculations? Is your solution too clever — e.g., has the solution been condensed to the
point where it’s incomprehensible?
5 Collaboration
It is OK for you to discuss solutions to this program with your classmates. However, no collaboration
should ever involve looking at one of your classmate’s source programs! It is usually extremely easy
to determine that someone has copied a program, even when the indivdual doing the copying has
changed identifiers and comments. If we discover that someone has copied a program, the authors
of both programs will receive an F in the course.