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

matlab bi You and your team will develop a MATLAB program to find a root for a f

ID: 1718341 • Letter: M

Question

matlab bi You and your team will develop a MATLAB program to find a root for a function: f(x) = 5x^4 + 3x^3 + 5x^2 +4x + 8 in the interval of -1 x 0. The method to be implemented is the Bisection Method (please see textbook Chapter 5 for more details) The pseudocode provided by the textbook for the Bisection method is given as: Your Matlab program will be able to automatically find the root in the interval of -1 x 0 and stop the iteration when epsilon_a epsilon_r = 0.001%. Please have your Matlab program output each iteration's approximated root value (x_r) and the relative percent error (epsilon_a). You can use plot to compare the error reduction vs. iterations. Each team should turn in a hard copy of project report containing cover page, problem description. flow chart, all Matlab source codes, results and discussion, conclusions. ACD containing all the m-files should be attached with the hardcopy report.

Explanation / Answer

function [ r ] = bisection( f, a, b, N, eps_step, eps_abs )
% Check that that neither end-point is a root
% and if f(a) and f(b) have the same sign, throw an exception.

if ( f(a) == 0 )
   r = a;
   return;
elseif ( f(b) == 0 )
   r = b;
   return;
elseif ( f(a) * f(b) > 0 )
error( 'f(a) and f(b) do not have opposite signs' );
end

% We will iterate N times and if a root was not
% found after N iterations, an exception will be thrown.

for k = 1:N
% Find the mid-point
c = (a + b)/2;

% Check if we found a root or whether or not
% we should continue with:
% [a, c] if f(a) and f(c) have opposite signs, or
% [c, b] if f(c) and f(b) have opposite signs.

if ( f(c) == 0 )
r = c;
return;
elseif ( f(c)*f(a) < 0 )
b = c;
else
a = c;
end

% If |b - a| < eps_step, check whether or not
% |f(a)| < |f(b)| and |f(a)| < eps_abs and return 'a', or
% |f(b)| < eps_abs and return 'b'.

if ( b - a < eps_step )
if ( abs( f(a) ) < abs( f(b) ) && abs( f(a) ) < eps_abs )
r = a;
return;
elseif ( abs( f(b) ) < eps_abs )
r = b;
return;
end
end
end

error( 'the method did not converge' );
end

%Script...

format long
eps_abs = 1e-5;
eps_step = 1e-5;
a = -1;
b = 0;
f=@(x) 5*x^5-7*x^4+3*x^3+5*x^2+4*x+8;
N=1000;
Appr_root=bisection( f, a, b, N, eps_step, eps_abs )

%output...

Appr_root =

-0.86497926712036