% Broyden method for the roots of 
% a set of nonlinear equations
% Requires no explicit computation of Jacobian
% See example 5.16 on p.242 of Heath
% This version requires only O(p*n) arithmetic operations/loop.
% K. Ming Leung, 02/22/03

clear all; format long;
%profile on;
maxIt = 20; tol2 = eps;
x=[1; 2]; N=length(x);
it = 0; err = 1;

f = feval('fcnEg5p15',x); n2=f.'*f;
x=x-f;
U=feval('fcnEg5p15',x);
d=-1/(f.'*(f-U));
V=-d*f.';
f=U;
while it < maxIt & n2 > tol2
    s=-f-sum(repmat((V*f).',N,1).*U,2); sT=s.';
    n2=sT*s; %if sqrt(n2) < tol2, x=x+s; break; end;
    x=x+s,
    f = feval('fcnEg5p15',x);
    u = f+sum(repmat((V*f).',N,1).*U,2);
    d=1/(n2+sT*u); 
    v=-d*(sT+sum(repmat((sT*U).',1,N).*V,1));
    U=[U u];
    V=[V; v];
    it = it+1;
end

%profile viewer;
it,
x,