function x=nsolve(a,b,x0,varargin) % solve nonlinear system a(x)=b using Krylov iteration with % Jacobian-vector multiplication by finite differences % extra arguments are passed to a % arguments % a the function % b the rhs % x0 initial approximation % varargin optional extra arguments for a % output % x approximate solution newt_tol = 1e-5; newt_maxit = 5; h = 1e-1; krylov_tol = sqrt(newt_tol); krylov_maxit = min(10,length(b)); x = x0; for it=1:newt_maxit % one Newton step % solve the linear system a(x)+a'(x)*dx=b ax = feval(a,x,varargin{:}); % get the rhs of the linear system r = b-ax; err=norm(r); % fprintf('Newton iteration %i starting error %g\n',it,err) if err