Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a Matlab function that can use the bisection algorithm ot find the root o

ID: 670024 • Letter: C

Question

Create a Matlab function that can use the bisection algorithm ot find the root of a function. The matlab function's call syntax should beable to understand all of the following: x=bisection(fun,x0) x=bisection(fun,x0,tolX) x=bisection(fun,x0,tolX,tolFun) x=bisection(fun,x0,tolX,tolFun,iterMax) [x,fval]=bisection(fun,x0,tolX,tolFun,iterMax) [x,fval, exitflag]=bisection(fun,x0,tolX,tolFun,iterMax) Where the input arguments are: fun-a function handle x0-a two element array of lower and upper bounds tolX-OPTIONAL USER INPUT scalar, max level of error in absolute units (no percent) (default=1e^-6) tolFun-OPTIONAL USER INPUT scalar, satisfactory tolerance (default=1e^-6) iterMax-OPTIONAL USER INPUT scalar, max iterations (default=1e^3) return arguments are: x-scalar root estimate at termination fval-scalar function value at root location exitflag-scalar integer, indicates reason for termination of algorithm (2:abs(fun(x)),(1:estimation error is less than tolX),(0:exited due to max iterations)

Explanation / Answer

function val = bisection(x0,x1,M) %bisection method
f = @(val) val - tan(val); %function declared
u = f(x0);
v = f(x1);
e = x1-x0;
val = [x0, x1, u, v]
if (u > 0 && v > 0) || (u < 0 && v < 0)
return;
end;
for k = 1:M %here iteratins using for loop
e = e/2;
c = x0 + e;
w = f(c);
val = [k, c, w, e]
if (abs(e) < 10^(-5) || abs(w) < eps) %checking for absolute error
return;
end
if (w < 0 && u > 0) || (w > 0 && u < 0)
x1 = c;
v = w;
else
x0 = c;
u = w;
end
end