Function roots = BiSectionSearch (xStart, xStop, xStep, xOverlap, Function) % Fu
ID: 3794417 • Letter: F
Question
Function roots = BiSectionSearch (xStart, xStop, xStep, xOverlap, Function) % Function that uses bisection to search for the roots of f(x) % Inputs: xStart and xStep - the end points of the search. % x Step - the size of interval used in search. % x Overlap - the overlap between intervals. % Function - the function to be searched. % Outputs: roots - array of roots found in the search. In this part of the project we will be using the bisection algorithm, to search for the solution to a highly nonlinear function. This will involve a rewrite of the Bisection function. Rewrite the function Bisection to be BiSectionSearch. The function will use the bisection algorithm to search for a set of roots. It should do this systematically by applying the Bisection algorithm to a set of overlapping intervals that work through the region of the roots. The function call should be as shown below.Explanation / Answer
Matlab code:
function [roots] = bisectionsearch(xstart,xstop,xstep,intervals,func)
roots = [];
for i = 1:size(intervals,1)
a = intervals(i,1);
b = intervals(i,2);
if(a >= xstart && b<= xstop)
c = a;
EPSILON = 0.00000001;
iter = 1;
while ((b-a) >= EPSILON)
c = (a+b)/2;
if ((func(c)) == 0.0)
break;
elseif ((func(c))*(func(a)) < 0)
b = (a+b)/2;
else
a = (a+b)/2;
end
iter = iter + 1;
end
roots = [roots, c];
end
end
roots = unique(roots);
end
Sample Input 1:
bisectionsearch(-5,8,1,[[-pi,(-pi/4)];[0,pi];[pi,2*pi]],@cos)
Sample Output:
roots = [ -1.570796323869061 1.570796332646569 4.712388986236362 ]
Sample Input 2: Here x = -1.570796323869061 is not in range [0,8], so only two roots displayed
bisectionsearch(0,8,1,[[-pi,(-pi/4)];[0,pi];[pi,2*pi]],@cos)
Sample Output:
roots = [ 1.570796332646569 4.712388986236362 ]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.