function x = simpleNewtonSolver( f,x0, accuracy) % Implements the Newton Method in Matlab % Takes the following arguments: % f name of the function to be solved % x0 initial guess (the closer to a solution, the better) % accuracy the minimum allowed accuracy (to determine if % the method had converged. As an reminder, most % computer nowadays use at least 32-bit % floating point numbers, so you may want accuracy % to be at least 2^32 times smaller than your % expected result. %initialize x: x = x0; err_too_big = 1; iter = 0; while(err_too_big) fct = feval(f,x); %dx should be a 'small' number der = (feval(f,x+dx)-fct)/dx; %check if derivative = 0. If so, replace it by small number to %avoid a division by zero %check if not too many iterations %update x %define your stopping criterion % You should at least consider these 2 conditions in your % stopping criterion: % f_too_big i.e. f is "too big" % delta_too_big i.e. haven't converged yet. err_too_big = (f_too_big || delta_too_big); end