Fundamentals of Numerical Mathematics for Physicists and Engineers. Alvaro Meseguer
href="#fb3_img_img_302de88e-aea1-5c0f-be85-ded3acb3a1c1.png" alt="images"/>, usually called the residuals. Since this quantity should approach zero when
1.3 Newton's Method
Newton's method, also known as Newton–Raphson method,7 is probably the most important root‐finding algorithm of numerical mathematics. Its formulation naturally leads to a wide variety of other root‐finding algorithms and is easily adaptable to solving systems of nonlinear equations.8 To formulate this method we will assume that
(1.6)
where
(1.7)
The resulting abscissa
(1.8)
and so on, leading to the iterative formula:
Newton's Iteration:
(1.9)
A simple implementation of Newton's method can be found in the next code, whose structure essentially differs from the one seen in the bisection in two main aspects. First, the code needs an initial guess fun
, the code also needs the one associated with dfun
. Providing the M‐file for the exact derivative entails both analytical differentiation and its subsequent programming, both being error‐prone tasks. While this is the correct procedure, it is sometimes advisable to let the computer do the job by approximating the value of
where
% Code 2: Newton's method for solving f(x) = 0 % Input: 1. a: initial guess % 2. tol: tolerance so that abs(x_k+1 - x_k) < tol % 3. itmax: maximum number of iterations allowed % 4. fun: function's name % 5. dfun: derivative's name % (If dfun = ‘0’, then f'(x) is approximated) % Output: 1. xk: resulting sequence % 2. res: resulting residuals % 3. it: number of required iterations function [xk,res,it] = newton(a,tol,itmax,fun,dfun) xk = [a]; fk = feval(fun,xk); res = abs(fk); it = 0; tolk = res(1); dx = 1e-8 ; while it < itmax & tolk > tol if dfun == ‘0’ dfk = (feval(fun,xk(end)+dx)-fk)/dx; else dfk = feval(dfun,xk(end)); end xk = [xk, xk(end) - fk/dfk]; fk = feval(fun,xk(end)); res = [res abs(fk)]; tolk = abs(xk(end)-xk(end-1)); it = it + 1; end
Let us compare the performance of Newton's method with the bisection by applying both algorithms to solve the