%
% testing the covergence behavior of Newton's Method
%
N = 400; 
tol = 1e-12; 
Nit = 200;
rho = 10;
FLG = [];
Cov = [];
NumIt = [];
X = [];
F = [];
warning off;
disp(['Running Newton Method with ', num2str(Nit), ' different random starting points']);
for k = 1:Nit
   x0=rho*randn(3,1); 
   [fun,x,flg] = NewtonMethod2(@myfunc3,@myfunc3Jacob,x0,N,tol);
   FLG(k) = flg;
   Cov(k) = norm(fun(:,end));
   NumIt(k) = size(fun,2);
   X      = [X,x(:,end)];
   F      = [F,fun(:,end)];
end
success = length(find(FLG==0));
failure = length(find(FLG>0));
disp([num2str(success), ' starting points were successful']);
disp([num2str(failure), ' starting points failed']);
mask = find(FLG==0);
disp(['Average number of iterations for successful starting points: ', num2str(round(mean(NumIt(mask))))]);
disp(['Median number of iterations for successful starting points: ', num2str(median(NumIt(mask)))]);
X = X(:,mask);
F = F(:,mask);
[Q,R,P] = qr(X,0);
X = X(:,P(1:2));
disp('Two Solutions are')
disp(X(:,1));
disp('and')
disp(X(:,2));




