% 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 -detail operator;
maxIt = 200; tol2 = eps; N=4; %fcnEg5p15
hundredK=1000; V=zeros(maxIt,N); U=zeros(N,maxIt);
tic;
for i=1:hundredK
x=[1; 2; 3; 4]; N=length(x);
it = 1; 

f = feval('fcn4',x); n2=f.'*f;
x=x-f;
U(:,1)=feval('fcn4',x);
d=-1/(f.'*(f-U(:,1)));
V(1,:)=-d*f.';
f=U(:,1);
while it < maxIt & n2 > tol2
    s=-f-sum(repmat((V(1:it,:)*f).',N,1).*U(:,1:it),2); 
    sT=s.';
    n2=sT*s;
    x=x+s;
    f = feval('fcn4',x);
    u = f+sum(repmat((V(1:it,:)*f).',N,1).*U(:,1:it),2);
    d=1/(n2+sT*u);
    v=-d*(sT+sum(repmat((sT*U(:,1:it)).',1,N).*V(1:it,:),1));
    it = it+1;
    U(:,it)=u;
    V(it,:)=v;
end
end
toc;
%profile viewer;
it,
x,
