4. (30pts, 25, 5) Below is a MATLAB implementation of the bisection method with
ID: 2262144 • 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) tol& n 0 elseif f(x-n) *f (b-n) >= 0 end err- n = n+1 ; end c (b-n + a-n) / 2.0; end (b) Is the code working if you consider f(x) = x*, a =-1 and b = 2 ? Explain why.Explanation / Answer
Bisection method coding
function[c,err,n]=bisection(f,a,b,tol,N)
n=0;
N=1000;
tol=10^-6;
f=@(x)x^4;
a=-1;
b=2;
a_n=a;
b_n=b;
err=b_n-a_n;
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;
n
err
end
output
c =
2
n =
1000
err =
128
Clearly for (b) 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]
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.