% Broyden's method example

clear

f=inline('[y-1./x; (x/5).^2+(y/8).^2-1]');
jacobian=inline('[1./x.^2 1; 2*x/25 2*y/64]');

% try an initial guess

x0=[6;1];
A0=jacobian(x0(1),x0(2));
x1=x0-A0\f(x0(1),x0(2));
A1=A0+((f(x1(1),x1(2))-f(x0(1),x0(2))-A0*(x1-x0))/norm(x1-x0)^2)*(x1-x0)';

% results of first step
A1
jacobian(x1(1),x1(2))

% iterate to desired tolerance
count=1;tol=1e-8;
while norm(f(x1(1),x1(2)))>tol && count<50
    x0=x1;A0=A1;
    x1=x0-A0\f(x0(1),x0(2));
    A1=A0+((f(x1(1),x1(2))-f(x0(1),x0(2))-A0*(x1-x0))/norm(x1-x0)^2)*(x1-x0)';
    count=count+1;
end
x1

% another initial guess

x0=[0.1;10];
A0=jacobian(x0(1),x0(2));
x1=x0-A0\f(x0(1),x0(2));
A1=A0+((f(x1(1),x1(2))-f(x0(1),x0(2))-A0*(x1-x0))/norm(x1-x0)^2)*(x1-x0)';

A1
jacobian(x1(1),x1(2))

count=1;tol=1e-8;
while norm(f(x1(1),x1(2)))>tol && count<50
    x0=x1;A0=A1;
    x1=x0-A0\f(x0(1),x0(2));
    A1=A0+((f(x1(1),x1(2))-f(x0(1),x0(2))-A0*(x1-x0))/norm(x1-x0)^2)*(x1-x0)';
    count=count+1;
end
x1
