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

% Set initial point
clear all
x = 3*pi/2;

% Newton iteration
dxMax = 1.e-6;
dx = inf;
iter = 0;
while abs(dx)>dxMax
    iter = iter+1;
    y = (sin(x)-x*cos(x))/x^2;                  % y(x)
    dydx = sin(x)/x - 2*(sin(x)-x*cos(x))/x^3;  % dy(x)/dx
    dx = -y/dydx;                               % y(x)+(dy/dx)*dx=0
    x = x+dx;
end

% Show root
x0=x
iter
