function x=mnhf_newton_system(Fun,Jac,x,tol,trace) %MNHF_NEWTON_SYSTEM solves a system of nonlin. eqns. using Newton's method. % % Fun - name of the external function to compute f % Jac - name of the externan function to compute J % x - vector of initial guesses % tol - error criterion % trace - print intermediate results % % Usage mnhf_newton_system(@cstrF,@cstrJ,[3.0 2.0 1.0 0.75 0.5]',1e-10,1) %Check inputs if nargin < 5, trace = 1; end if nargin < 4, tol = 1e-8; end fprintf('\nCount\t Norm(f)\n') fprintf('-------------------------\n') Nmax = 25; f = 1.0; % Can select any arbitrary value with magnitude > tol. count = 0; % Initialize counter. while (norm(f) > tol && count <= Nmax) count = count+1; % Increment counter. f = feval(Fun,x); % Function evaluation. J = feval(Jac,x); % Jacobian evaluation. x = x-J\f; % Update the guess. if trace fprintf(1,'%3i %12.5f\n',count,norm(f)); end end if count > Nmax fprintf('Maximum number of iterations reached.\n\n') end