Question
Solve using MATLAB.
method for root-finding Exercise 3: Bisection This question helps students to understand how to develop a numerical solver to find the roots of an algebraic equation. Write a MATLAB user-defined function that solves for a root of an equation f(x)-0 using the bisection method. Name the function function [x] = bisection (fun, a, b, maxtol, maxitr) The input argument fun is a name for the function f(x) (it is a dummy name for the function that is imported into bisection), a and b are two points that bracket the root, maxtol is the maximum tolerance and maxitr is the maximum number of iterations. The program will stop if either of the following conditions is met: The approximate error (abs (fun (x)) is less than the maximum tolerance (maxtol) . The number of iterations exceeds the maximum number of iterations (maxitr) Output the following values at each iteration (k) using Eprint f to allow checking of results within the loop k, a, f (a), b, f (b), x, f (x) A partially completed code is provided for your reference. Complete the code by following the logic and codes for the bisection method developed in the lecture notes.
Explanation / Answer
function [ x ] = bisection( fun,a,b,maxtol,maxitr)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
if fun(a)*fun(b)>0
disp('take some other values of a and b')
else
k=1;
x(k)= (a+b)/2;
err=abs(fun(x));
while (err > maxtol && k < maxitr)
if fun(a)*fun(b)<0
b=x(k);
else
a=x(k);
end
x(k+1)=(a+b)/2;
err= abs(fun(x(k+1)));
k=k+1;
end
end