Bisection Method Issues/Help
    5 views (last 30 days)
  
       Show older comments
    
Hi people, I'm having a bit of an issue with my Bisection Method Algorithm, which I understand conceptually, but it doesn't quite work with my code. My outputs are the final root, absolute value of the function at the root, number of iterations and all the midpoints generated through each iteration. Here's my code so far:
    % function [r,rHist,N,fRoot] = bisectionE7(fHan,xL,xR,fTol,iterMax)
k= 0; % initialize iteration counter
fxL = feval(fHan,xL);
fxR = feval(fHan,xR);
sfxL = sign(fxL);
sfxR = sign(fxR);
if (sfxL*sfxR > 0)
  r=[];
  rHist=[];
  N=[];
  fRoot=[];  
end
while (k <= iterMax)
  % use midpoint as the iterate  
  c = (xL+xR)/2;
  fc = feval(f,c);
  sfc = sign(fc);
  if ((xR-xL)<2*fTol | fc == 0)
    x = c;
    return 
  elseif (sfxL*sfc < 0)
    xR = c;
  else
    xL = c;
  end
  k = k+ 1;
end
% if this while loop terminates before returning the root
disp(' max number of iterations reached ... ')
  x = xL;
2 Comments
  John Petersen
      
 on 20 Nov 2012
				Okay, so the code doesn't work. What is your question? Where does it fail? What parts work? Try bisecting the problem to help you find where you are having a problem and then someone will be more able and willing to help.
Answers (0)
See Also
Categories
				Find more on Debugging and Analysis in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!