Using matlab find all the roots of the following function in
Using matlab, find all the roots of the following function in the interval 0.5<=z<=5.
f(x) = (2/x-x)cos(x)+0.0119
Each root must be accurate to a minimum of six significant figures. Use either the bisect or false position methods.
What is the conclusion regarding the results?
Solution
The code below is a function for finding roots of one variable function using bisection section method,
after compiling the code , .m file will be saved in your system, then type bisect in your workspace area of matlab with parameters. Note: due to insufficient physical memory of system, desired accuracy is not achieved.
CODE:
function c = bisect(a, b, delta)
%% bisect.m
%%
%% Implements the bisection method
%%
%% Input: a the left endpoint of the interval
%% b the right endpoint of the interval
%% delta the tolerance/accuracy we desire
%%
%% Output: c the approxmiation to the root of f
%%
%% Syntax: bisect(a, b, delta)
fa = f(a); %% compute initial values of f(a) and f(b)
fb = f(b);
if sign(fa) == sign(fb) %% sanity check: f(a) and f(b) must have different
%% signs
%%
%% the error function prints an error message and
%% exits
error(\'f must have different signs at the endpoints a and b. Aborting.\')
end
fprintf(\'initial interval: a=%d, b=%d, fa=%d, fb=%d\ \',a,b,fa,fb)
while ( abs(b - a) > 2*delta ) %% While the size of the interval is
%% larger than the tolerance
c = (b + a)/2; %% Set c to be the midpoint of the interval
fc = f(c); %% Calculate the value of f at c
if sign(fc) ~= sign(fb) %% If f(c) and f(b) are of different sign, then
%% f must have a zero between c and b (by the
%% Intermediate Value Theorem).
a = c; fa = fc;
else %% This is the case where f(a) and f(c) are of
%% different sign.
%%
b = c; fb = fc;
end
%% Repeat the algorithm on the new interval
fprintf(\' a=%d, b=%d, fa=%d, fb=%d\ \',a,b,fa,fb)
end
%%
%% put subroutines here
%%
%%
function fx = f(x)
fx = (((2*x^-1)-x)*cos(x))+0.0119);
return;
after saving this block of code, type the following statement in matlab workspace
bisect(0.5,5,0.000001)
this will provide solution.
Conclusion is after numerical simulation using Bisection method, root of the function is approximatley 4.7152, but when we plot the function in matlab using basic command of plot, we see that root actually lies beyond x=5.

