CS 130 HW 10

Winter 1997

Due: Wednesday, 19 March at 5:00 P.M.

1. Write a program that finds a root of a polynomial
using the bisection method. A root is a value of x for which y(x) = 0.
Your program should prompt the user for:
* the degree of the polynomial, n
* the coefficients a0, a1, a2, . . . , an
* x0, x1 which are two values of x that bracket a root (that is, y(x0)*y(x1) < 0).
The bisection method works like this:
Step 1: xm = (x0 + x1)/2
Step 2: ym = y(xm).
Step 3: If y(x0)*ym<0, the program should replace x1=xm. Otherwise, it should replace x0 = xm.
Step 4: If x1-x0 < 0.001, print "The answer is (x0+x1)/2 to within 0.0005"
Otherwise, go back to step 1.

Your program should work for polynomials up to degree 10.

Try your program out on:

n = 1; a0 = -1; a1 = 3; x0 = 0; x1 = 1

(The answer should be 1/3)

n = 2; a0 = -2; a1 = 5; a2 = 25; x0 = 0; x1 = 1

(The answer should be 1/5)

n = 8; a0 = -3; a1 = 5; a2 = 0; a3 = -3; a4 = 5; a5 = 0; a6 = 0; a7 = -3; a8 = 5; x0 = 0; x1 = 1;

(The answer should be 3/5)