Bisection Method Issues/Help
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
John Petersen
le 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.
Réponses (0)
Voir également
Catégories
En savoir plus sur Functions dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!