function Bisect2(a,b,nmax,emax) % Cheney/Kincaid 5th Ed., Page 95. % Bisection with a maximum number of iterations and % a fixed value for epsilon. % [Remember that "eps" is a reserved name in MATLAB!] % I used: % Bisect2(1.4,1.5,100,1e-20) tic % This function will approx sqrt(2). % Inputs: a < sqrt(2) < b. % nmax = maximum number of iterations. % emax = our max error f = @(x) x^2 - 2; % Initialize the end points. fa = f(a); fb = f(b); if sign(fa)==sign(fb) fprintf('Function has same signs at a & b!\n'); stop; end % Initialize error. error = b - a; % Print them out. fprintf(' a = %20.17f\n',a); fprintf(' b = %20.17f\n',b); fprintf('f(a) = %20.17f\n',fa); fprintf('f(b) = %20.17f\n',fb); fprintf('Initial error = %20.17f\n',error); % We will run nmax iterations (or fewer). for i=0:nmax % Calculate the new error and new values for c & f(c). error = error/2.; c = a + error; fc = f(c); % If the abs. error is small enough, then we have converged. % Print out the results. if abs(error)