Docsity

Newton and Secant Method-Numerical Methods-MATLAB Code, Exercises of Mathematical Methods for Numerical Analysis and Optimization

Toggle sidebar
bg1
1-5
1 Chapter 1
1.3 The Secant Method
The bisection method is generally inefficient, it requires more function evaluations in
comparison with the secant method which is linear interpolation using the latest two points.
Figure 1.4 shows graphically the root x3 obtained from the intersection of the line
BA
with
the x-axis.
x1x2
x3x4x5
f(x)
f(x )
1
f(x )
2
A
B
Figure 1.4 Graphical depiction of the secant method.
The intersection of the straight line with the x-axis can be obtained by using similar triangles
x3x1A and x3x2A or by using linear interpolation with the following points.
x
x1
x3
x2
f(x)
f(x1)
0
f(x2)
12
23
xx
xx
=
x3 = x2 f(x2)
The next guess is then obtained from the straight line through two points [x2, f(x2)] and [x3,
f(x3)]. In general, the guessed valued is calculated from the two previous points [xn-1, f(xn-1)]
and [xn, f(xn)] as
xn+1 = xn f(xn)
)()( 1
1
nn
nn
xfxf
xx
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Newton and Secant Method-Numerical Methods-MATLAB Code and more Exercises Mathematical Methods for Numerical Analysis and Optimization in PDF only on Docsity!

1 Chapter 1

1.3 The Secant Method

The bisection method is generally inefficient, it requires more function evaluations in comparison with the secant method which is linear interpolation using the latest two points.

Figure 1.4 shows graphically the root x 3 obtained from the intersection of the line AB with the x -axis.

x 1 x 2

x 3 x 4 x 5

f(x)

f(x ) 1

f(x ) 2

A

B

Figure 1.4 Graphical depiction of the secant method.

The intersection of the straight line with the x -axis can be obtained by using similar triangles x 3 x 1 A and x 3 x 2 A or by using linear interpolation with the following points.

x x 1 x 3 x 2 f ( x ) f ( x 1 ) 0 f ( x 2 )

2 1

3 2 x x

x x

2 1

2 f x f x

f x

x 3 = x 2  f ( x 2 ) ( 2 ) ( 1 )

2 1 f x f x

x x

The next guess is then obtained from the straight line through two points [ x 2 , f ( x 2 )] and [ x 3 , f ( x 3 )]. In general, the guessed valued is calculated from the two previous points [ x n-1, f ( x n-1)] and [ x n, f ( x n)] as

x n+1 = x n  f ( x n) ( ) ( 1 )

1 

 

n n

n n f x f x

x x

The secant method always uses the latest two points without the requirement that they bracket the root as shown in Figure 1.4 for points [ x 3 , f ( x 3 )] and [ x 4 , f ( x 4 )]. As a consequence, the secant method can sometime diverge. A Matlab program for the secant method is listed in Table 1.2 where the function f ( x ) is an input to the program. The statement eval(f) is used to evaluate the function at a given value of x.

Table 1.2 __________________________________ % Secant method f=input('f(x)=','s'); tol=input('error tolerance =1e-5, new tolerance='); if length(tol)==0,tol=1e-5;end x1=input(' First guess='); x=x1; f1=eval(f); x2=input(' Second guess='); x=x2; f2=eval(f);xsave=x2; disp(' Secant method') for i=2: x=x2-f2*(x2-x1)/(f2-f1); fx=eval(f); x1=x2;f1=f2;ex=abs(x2-x); x2=x;f2=fx; fprintf('i = %g, x = %g, fx = %g\n',i,x,fx) if ex<tol, break,end end

1.4 The False-Position Method

The false-position method is also a linear interpolation method, however is based on the previous two points that bracket the roots. Figure 1.5 shows graphically the root x 3 obtained

from the intersection of the line AB with the x -axis. This root is no different from the value obtained from the secant method. The next guess x 4 is then obtained from the straight line through two points [ x 2 , f ( x 2 )] and [ x 3 , f ( x 3 )] since these two previous points also bracket the roots. However, for the next iteration, x 5 is obtained from the two latest points [ x 2 , f ( x 2 )] and [ x 4 , f ( x 4 )] that bracket the root. The second method uses [ x 3 , f ( x 3 )] and [ x 4 , f ( x 4 )]. The false- position method always converges however the convergence can be slow if the solution is approached from one side of the root.

A Matlab program for the false-position method is listed in Table 1.3 where the function f ( x ) is an input to the program. The statement eval(f) is used to evaluate the function at a given value of x. The iteration will not start if the first two guesses do not bracket the root.

1.5 The Newton-Raphson Method

The Newton-Raphson method and its modification is probably the most widely used of all root-finding methods. Starting with an initial guess x 1 at the root, the next guess x 2 is the intersection of the tangent from the point [ x 1 , f ( x 1 )] to the x -axis. The next guess x 3 is the intersection of the tangent from the point [ x 2 , f ( x 2 )] to the x -axis as shown in Figure 1.6. The process can be repeated until the desired tolerance is attained.

x 1

f(x) f(x ) 1 B

x 3 x 2

Figure 1.6 Graphical depiction of the Newton-Raphson method.

The Newton-Raphson method can be derived from the definition of a slope

f’ ( x 1 ) = 1 2

x x

f x

x 2 = x 1  '( )

1

1 f x

f x

In general, from the point [ x n, f ( x n)], the next guess is calculated as

x n+1 = x n  '( )

n

n f x

f x

The derivative or slope f ( x n) can be approximated numerically as

f’ ( x n) = x

f xn x f xn

Example 1.

Solve f ( x ) = x^3 + 4 x^2  10 using the the Newton-Raphson method for a root in [1, 2].

Solution

From the formula x n+1 = x n  '( )

n

n f x

f x

f ( x n) = x^3 n + 4 x n^2  10  f’ ( x n) = 3 x n^2 + 8 xn

x n+1 = x n  n n

n n x x

x x 3 8

2

3 2

Using the initial guess, x n = 1.5, x n+1 is estimated as

x n+1 = 1.5  3 1. 5 8 1. 5

2

3 2

  

A Matlab program for the Newton-Raphson method is listed in Table 1.4 where the function f ( x ) is an input to the program. The statement eval(f) is used to evaluate the function at a

given value of x. The derivative is evaluated numerically using f’ ( x n) = x

f xn x f xn

with  x = 0.01. A sample result is given at the end of the program.

Table 1.4 __________________________________ % Newton method with numerical derivative f=input('f(x)=','s'); tol=input('error tolerance =1e-5, new tolerance='); if length(tol)==0,tol=1e-5;end x1=input(' First guess='); x=x1; fx=eval(f); for i=1: if abs(fx)<tol, break,end x=x+.01; ff=eval(f); fdx=(ff-fx)/.01; x1=x1-fx/fdx; x=x1; fx=eval(f); fprintf('i = %g, x = %g, fx = %g\n',i,x,fx) end

f(x)=x^3+4*x^2- error tolerance =1e-5, new tolerance= First guess=1. i = 1, x = 1.37391, fx = 0. i = 2, x = 1.36531, fx = 0. i = 3, x = 1.36523, fx = 6.39291e-