Page 198 - Applied Numerical Methods Using MATLAB
P. 198
NEWTON(–RAPHSON) METHOD 187
be used for this kind of problem as well as general nonlinear equation problems,
only if the first derivative of f(x) exists and is continuous around the solution.
The strategy behind the Newton(–Raphson) method is to approximate the
curve of f(x) by its tangential line at some estimate x k
y − f(x k ) = f (x k )(x − x k ) (4.4.1)
and set the zero (crossing the x-axis) of the tangent line to the next estimate
x k+1 .
0 − f(x k ) = f (x k )(x k+1 − x k )
f(x k )
x k+1 = x k − (4.4.2)
f (x k )
This Newton iterative formula is cast into the MATLAB routine “newton()”,
which is designed to generate the numerical derivative (Chapter 5) in the case
where the derivative function is not given as the second input argument.
Here, for the error analysis of the Newton method, we consider the second-
degree Taylor polynomial (Appendix A) of f(x) about x = x k :
f (x k )
2
f(x) ≈ f(x k ) + f (x k )(x − x k ) + (x − x k )
2
function [x,fx,xx] = newton(f,df,x0,TolX,MaxIter)
%newton.m to solve f(x) = 0 by using Newton method.
%input: f = ftn to be given as a string ’f’ if defined in an M-file
% df = df(x)/dx (If not given, numerical derivative is used.)
% x0 = the initial guess of the solution
% TolX = the upper limit of |x(k) - x(k-1)|
% MaxIter = the maximum # of iteration
%output: x = the point which the algorithm has reached
% fx = f(x(last)), xx = the history of x
h = 1e-4; h2 = 2*h; TolFun=eps;
if nargin == 4 & isnumeric(df), MaxIter = TolX; TolX = x0; x0 = df; end
xx(1) = x0; fx = feval(f,x0);
fork=1: MaxIter
if ~isnumeric(df), dfdx = feval(df,xx(k)); %derivative function
else dfdx = (feval(f,xx(k) + h)-feval(f,xx(k) - h))/h2; %numerical drv
end
dx = -fx/dfdx;
xx(k+1) = xx(k)+dx; %Eq.(4.4.2)
fx = feval(f,xx(k + 1));
if abs(fx)<TolFun | abs(dx) < TolX, break; end
end
x = xx(k + 1);
if k == MaxIter, fprintf(’The best in %d iterations\n’,MaxIter), end