Hi all
Here is a question from my assignment in Algorithms & Complexity Theory. I'm having a problem with part 2.
A continuous function f(x) such that f(a) and f(b) have opposite signs must have a zero (a point x such that f(x)=0) in the interval [a,b]. By checking whether the zero occurs in the subinterval [a,(a+b/2] or the subinterval [(a+b)/2,b].
The bisection method for approximating a zero of f(x) is based on repeating this process until a zero is obtained to within a predescribed error.
1. Give pseudocode for the bisection method algorithm Bissec(f(x),a,b,error) for finding an approxmiation to a zero of a continuous function f(x) in the interval [a,b] accurate to within error.
2. Show how to use the bisection method of (a) to compute sqrt(c) of a positive number c. Give pseudocode for the bisection method algorithm BissecSQRT(c,error) for finding the square root of a number c accurate to within error.
My answer to part 1 is as follows:
int Bissec(f(x), a, b, error){
repeat{
x = (a+b)/2;
if (f(x)==0) return x;
else if (((f(a)>0)&&(f(x)>0)) || ((f(a)<0)&&(f(x)<0))) a = (a+b)/2;
else if (((f(a)>0)&&(f(x)<0)) || ((f(a)<0)&&(f(x)>0))) b = (a+b)/2;
} until ((b-a)<error)
return x;
}
I'm completely stuck with the second question. Would greatly appreciate any help/tips/guidance. :o