% bisectionZero.m
% Uses bisection method to find the first positive zero of
% y(x)=(sin(x)-x*cos(x))/x^2
% J.M.Soler. Oct.2011

% Set bisection interval
clear all
x1 = pi;
x2 = 2*pi;
y1 = (sin(x1)-x1*cos(x1))/x1^2;
y2 = (sin(x2)-x2*cos(x2))/x2^2;

% Bisection iteration
dxMax = 1.e-6;
iter = 0;
while x2-x1>dxMax
    iter = iter+1;
    x = (x1+x2)/2;
    y = (sin(x)-x*cos(x))/x^2;  % y(x)
    if (y*y1>0)      
        x1=x;
        y1=y;
    else                        % x>x0
        x2=x;
        y2=y;
    end
end

% Show root
x0 = (x1*y2-x2*y1) / (y2-y1)
iter
