Write a function with the following header: function [n iterations, root] = my r
ID: 3846727 • Letter: W
Question
Write a function with the following header: function [n iterations, root] = my root bisection(f, a, b, tolerance) where: _ f is a function handle that represents a continuous real-valued function f de_ned on R. f takes a single input argument that is a scalar of class double (other than NaN, Inf, and -inf) and outputs a single output argument that is a scalar of class double (other than NaN, Inf, and -Inf). _ a and b are scalars of class double such that (b > a) and (f(a)*f(b) > [n, root] = my_root_bisection(my_function, 2, 3, 1e-3) n = 12 root = 2.2361 >> f = @(var) cos(var/2); >> [n, root] = my_root_bisection(f, 3, 4, 1e-5) n = 10 root = 3.1416 >> f = @(x) cos(x)-x; >> [n, root] = my_root_bisection(f, -10, 10, 1e-4) n = 15 root = 0.7391Explanation / Answer
function result = fun(x)
result = x*x + 3*x + 2;
end
function [n, root] = my_root_bisection(fn, a, b, tolerance)
n = 0;
if fn(a) < fn(b)
temp = a;
a = b;
b = temp;
end
root = (a+b)/2;
while abs(fn(root)) > tolerance
if fn(root) > 0
a = root;
else
b = root;
end
root = (a+b)/2;
n = n + 1;
end
end
[n, root] = my_root_bisection(@fun,-5,-1.5,0.00003);
n
root
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.