Completely lost on BiSection Function in Matlab

2 views (last 30 days)
I want to implement the bisection algorithm to solve nonlinear algebraic equations. My function is supposed to find a real root of the nonlinear algebraic equation f(x) = 0, using a bisection algorithm. I tried to do a lot of the code...but I don't even know where to start and I really tried my best on it :(
function [r,rHist,N,fRoot] = bisectionE7(fHan,xL,xR,fTol,iterMax)
if(fHan(xR) < 0)
error('xR must be positive')
end;
if(fHan(xL)>0)
error('xL must be negative')
end;
if (xL >= xR)
error ('xL must be less than xR')
end;
xg=(xR+xL)/2;
fHang=fHan(xg);
N=1;
while ( (abs(fHan) > fTol) & (abs(xg-xR) > iterMax) )
if (fHan>0)
xR=xg;
else
xL=xg;
end
xg=(xR+xL)/2;
fHan=fHan(xg);
N= N+1;
end
x_sol=xg;
f_at_x_sol=fHang;
if (f_at_x_sol > fTol)
error('No convergence')
end
The function declaration is
function [r,rHist,N,fRoot] = bisectionE7(fHan,xL,xR,fTol,iterMax)
where,
  1. fHan: function handle to f(x),
  2. xL: scalar, double, represents xleft,
  3. xR: scalar, double, represents xright,
  4. fTol: tolerance for stopping criterion, i.e., f(r) ≤ fTol,
  5. iterMax: maximum number of allowed iterations.
  6. r: the estimated root obtained by the algorithm,
  7. rHist: a vector containing the sequence of midpoints obtained by the algorithm,
  8. N: the total number of performed iterations,
  9. fRoot: the absolute value of the function f(x) at r, i.e., fRoot = f(r)
The function bisectionE7 should be written following the next steps:
(a) First, check whether xL or xR satisfy the stopping criterion specified by fTol. If one (or both) satisfy the criterion, then that value (or the one that makes f(x) smaller) should be returned as the root, with N set to 0, and rHist should be an empty array.
(b) Second, if neither xL nor xR satisfy the stopping criterion, then the function should verify that f(xL) and f(xR) do not have the same sign. If they do have the same sign, then bisectionE7 should return, with N, r, rHist, fRoot, set to empty arrays.
c) Third, if f(xL) and f(xR) have opposite signs, then the bisection should proceed like this: a middle point xMid is defined in terms of xL and xR, and the value f(xMid) is calculated. While the stopping criterion (f(xMid) ≤ fTol) is not satisfied, then depending on the sign of f(xMid), either xL or xR are replaced with xMid, and the process iterates until the stopping criterion is met or until the maximum number of iterations is reached. Note: The array rHist should be a row vector that contains all the midpoints xMid. Clearly, length(rHist) must be equal to N.
(d) Finally, in the case rHist is not empty, the function should compute the root, rE, using the MATLAB function fzero. Treat rE as the “exact” root, and produce a plot (using semilogy) of the absolute error (i.e., rHist − rE) as a function of iterations (i.e., 1:N).

Answers (0)

Categories

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