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

Need help writing a code on matlab Write a Matlab program that implements the bi

ID: 3784008 • Letter: N

Question

Need help writing a code on matlab

Write a Matlab program that implements the bisection method that we discussed in class. Hard coded at the beginning of your program should be f, a, b, and epsilon. Here f is the function for which we are trying to solve f(x) = 0 and the initial interval is [a, b]. Finally, e is a small positive number such that the program halts execution when the error (i.e., the difference between the exact answer and its approximation) is less than epsilon. The output of your program should be the approximate solution x* such that f(x*) approximately 0.

Explanation / Answer

function [ ans] = bisection( f, a, b, N, eps_st, eps_ab )

if ( f(a) == 0 )
   ans = a;
   return;
elseif ( f(b) == 0 )
   ans = b;
   return;
elseif ( f(a) * f(b) > 0 )
error( 'both function does not have opposite sign' );
end


for p = 1:N
% mid-point is
c = (a + b)/2;

if ( f(c) == 0 )
ans= c; %this would be root
return;
elseif ( f(c)*f(a) < 0 )
b = c; %b would be root
else
a = c;
end

  

if ( b - a < eps_st )
if ( abs( f(a) ) < abs( f(b) ) && abs( f(a) ) < eps_ab )
ans = a;
return;
elseif ( abs( f(b) ) < eps_ab )
ans = b;
return;
end
end
end

error( 'method not converging' );
end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote