Bisection Method Issues/Help

1 view (last 30 days)
SB
SB on 19 Nov 2012
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
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.
SB
SB on 21 Nov 2012
It's alright, I figured out how to fix it, thank you though!

Sign in to comment.

Answers (0)

Categories

Find more on Functions 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!