function x=robbins_monro(fn,x)
% fnds the root of the function fn(x)=0  where x is starting value and both
% x and the output from fn are d-dimensional. both are row vectors
% e.g  robbins_monro('junk2',[1.2 -1.7 .05])
d=length(x);
A=eye(d); B=100*A; % initialize the matrices A and B
% the approximation to the gradient is A^(-1)B
% the robbins-monro sequence is k/(k+i)  for some k;
k=10;
for i=1:1000
    ID=inv(A)*B; % this is the inverse of D
    f=eval([fn '(x)']);
    dx=-(k/(i+k))*f*ID';
    xn=x-dx;
    fnew=eval([fn '(xn)']); 
    df=fnew-f;
    if (fnew*fnew'<f*f')
    A=A+(dx')*(dx);
    B=B+(df')*dx;
    x=xn;
    f=fnew
end   
end