1
3.25 Write a user-defined MATLAB function that solves for all the real roots in a specified domain of a
nonlinear function using the bisection method. Name the function R=BisecAll-
Roots(fun,a,b,TolMax). The output argument R is a vector whose elements are the values of the
roots. The input argument Fun is a name for a function that calculates for a given x. (It is a dummy
name for the function that is imported into BisecAllRoots.) The arguments a and b define the domain,
and TolMax is the maximum tolerance that is used by the bisection method when the value of each root is
calculated. Use the following algorithm:
1. Divide the domain into 10 equal subintervals of length h such that .
2. Check for a sign change of at the endpoints of each subinterval.
3. If a sign change is identified in a subinterval, use the bisection method for determining the root in that
subinterval.
4. Divide the domain into 100 equal subintervals of length h such that .
5. Repeat step 2. If a sign change is identified in a subinterval, check if it contains a root that was already
obtained. If not, use the bisection method for determining the root in that subinterval.
6. If no new roots have been identified, stop the program.
7. If one or more new roots have been identified, repeat steps 4–6, wherein each repetition the number of
subintervals is multiplied by 10.
Use the function BisecAllRoots, with TolMax value of 0.0001, to find all the roots of the equa-
tion .
Solution
function R = BisecAllRoots(Fun,a,b,TolMax)
% BisecAllRoot finds all the roots of Fun = 0 in the interval [a,b]
x45.5x3
–7.2x2
–43x36++ 0=