Bisection Method Issues/Help

1 次查看(过去 30 天)
SB
SB 2012-11-19
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 个评论
John Petersen
John Petersen 2012-11-20
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 2012-11-21
It's alright, I figured out how to fix it, thank you though!

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Functions 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by