function [root,F,p] = newton_raphson(X0, Func, Jacobian, eq_number, m, tol1, tol2)
root=X0;
stop=0;
p=0;
while (stop==0)
	p=p+1;
	X0 = root;

	y = feval(Func,X0);
	y = y(eq_number);
	Jy = feval(Jacobian,X0);
	Jy = Jy(eq_number,eq_number);
	if (Jy == NaN)
		break;
	end

%	if (abs(Jy) - tol1 < 0)
%	error ('Method trapped at local minimum of f(%i) at x(%i) = %f\n',eq_number, eq_number, X0(eq_number));
%	end
	root(eq_number) = X0(eq_number) - y/Jy;
	F = feval(Func, root);
	if (abs(root(eq_number)-X0(eq_number)) <= tol1 || abs(F(eq_number)) <= tol2 || p >= m)
		stop=1;
		break;
	end
end

