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

Write a MATLAB function, called bisection_method that inputs a function f, two n

ID: 2324662 • Letter: W

Question

Write a MATLAB function, called bisection_method that inputs a function f, two numbers a, b, an error tolerance, tol, and a maximum number of iterations, N, and nds a root c of f in the interval [a, b] using the bisection method. Your function should compute a bound on the error, and stop when the error is less than the tolerance, or if the number of iterations exceeds N - whichever happens rst.

• Hint #1: The function should start with the line function [c,n,err] = bisection_method(f,a,b,tol,N) •

Hint #2: The function should contain a ‘while’ loop, looking something like: while err > tol && n < N

Explanation / Answer

f = @(x) 5*x^4 - 2.7*x^2 - 2*x + .5;
low = .1;
high = 0.5;
tol = .00001;
Nmax = 1000;
%m = bisection(f, low, high, tol);
disp('Bisection Method');


% Evaluate both ends of the interval
y1 = feval(f, low);
y2 = feval(f, high);
i = 0;

% Display error and finish if signs are not different
if y1 * y2 > 0
disp('Have not found a change in sign. Will not continue...');
m = 'Error'
return
end

% Work with the limits modifying them until you find
% a function close enough to zero.
disp('Iter low high x0');
while (abs(high - low) >= tol)
while i <= Nmax
i = i + 1;
% Find a new value to be tested as a root
m = (high + low)/2;
y3 = feval(f, m);
if y3 == 0
fprintf('Root at x = %f ', m);
return
end
fprintf('%2i %f %f %f ', i-1, low, high, m);   

% Update the limits
if y1 * y3 > 0
low = m;
y1 = y3;
else
high = m;
end
end
end

% Show the last approximation considering the tolerance
w = feval(f, m);
fprintf(' x = %f produces f(x) = %f %i iterations ', m, y3, i-1);
fprintf(' Approximation with tolerance = %f ', tol);
  

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