4. (30pts, 25, 5) Below is a MATLAB implementation of the bisection method with
ID: 2262969 • Letter: 4
Question
4. (30pts, 25, 5) Below is a MATLAB implementation of the bisection method with some code missing.
(a) Fill the blanks below to correctly execute the bisection method. READ CAREFULLY THE CODE !
function [c,err,n] = bisection(f,a,b,tol,N)
% function to solve f(x) = 0 using the bisection method over [a,b]
% ASSUMPTIONS: we assume that f(a)*f(b)<0
% INPUTS:
% f is the function at hand
% a is the lower bound of the tested interval
% b is the upper bound of the tested interval
% tol is the error tolerance
% N is the maximum number of iterations
% OUTPUTS:
% c is the computed root
% err is the error bound at the end
% n is the last iteration before breaking
n = 0;
a_n = a;
b_n = b;
err = b_n-a_n;
while err > tol && n < N
x_n = _____________;
if f(x_n)*f(a_n) > 0
______ = ______;
______ = ______;
elseif f(x_n)*f(b_n) >= 0
______ = ______;
______ = ______;
end
err = _____________;
n = n+1;
end
c = (b_n + a_n) / 2.0;
end
f(x) = 1Explanation / Answer
Solution
Please find the below blanks from the question
(a) while err > tol && n < N
x_n = _(a_n+b_n)/2;___
if f(x_n)*f(a_n) > 0
a_n= x_n;
b_n = b_n;
elseif f(x_n)*f(b_n) >= 0
b_n = x_n;
a_n = a_n;
end
err = abs(f(a_n+b_n)/2);
n = n+1;
end
c = (b_n + a_n) / 2.0;
end
(b) For sure it is not working as f(a)*f(b)=1*16=16>0 since f(a)*f(b) has to be less than zero for initial [a,b]
Thank you!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.