Bisection method with array at end points

1 view (last 30 days)
The function bisectEW7 should first check if either xL or xR satisfies the stopping criterion specified by fTol. If one (or both) satisfy the criterion, then that value (or the one that makes f(x) smallest) should be returned as the root, and rHist should be a 0 x 1 empty array.
Hello,
I am trying to check if the left end point (xL) and right end point (xR) as inputs to a function handle are roots within the tolerance I set (fTol). I am trying to set up a part of code that will check if xR or xL are zeros to the function.
The problem I am running into is that I want to say if either one is a zero, then to find the one that minimizes fHan(x), then call that r.
Here is my code so far
if (fHan(xL) <= fTol) || (fHan(xR) <= fTol)
rPossible = [abs(fHan(xL)) abs(fHan(xR))]
rSmallest = min(rPossible);
r =
rHist = cell(0,1);
if I do rPossible, I will get f(xR) and f(xL). rSmallest = min(rPossible) will give me the smallest of those two (i.e. closest to zero). Now what I want r to do is store either xL or xR, whichever makes the function closest to zero. How do I extract xL or xR from the handle?
Thanks

Accepted Answer

Geoff Hayes
Geoff Hayes on 13 Aug 2014
Rick - you almost have this. Not only will the min return the minimum value of a set of numbers, it can also return the index of that minimum number within an array. So your above code becomes
if (fHan(xL) <= fTol) || (fHan(xR) <= fTol)
rPossible = [fHan(xL) fHan(xR)];
[rSmallest,rIdx] = min(abs(rPossible));
r = rPossible(rIdx(1));
rHist = cell(0,1);
end
Note how the absolute value abs has been moved to within the min function so that we still have access to the original values. And in case fHan(xL) and fHan(xR) are identical, then rIdx will have two elements so we just use rIdx(1) to grab the first one (which works even if there is just one element).
Try the above and see what happens!

More Answers (0)

Categories

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