Lec 4newton Raphson

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 31

Numerical Methods

Roots of Nonlinear Equations


Topic: Newton-Raphson Method
Newton-Raphson Method
• This method is one of the most widely used methods to solve
equations also known as the Newton-Raphson.
• The idea for the method comes from a Taylor series expansion,
where you know the function and its’ first derivative.
f(xk+1) = f(xk) + (xk+1 - xk)*f ‘(xk) + ...

• The goal of the calculations is to find a f(x)=0, so set f(xk+1) = 0


and rearrange the equation. [ f ‘(xk) is the first derivative of
f(x). ]
0 = f(xk) + (xk+1 - xk)*f ‘(xk)
xk+1 = xk - [ f(xk) / f ‘(xk) ]
Newton-Raphson Method
• The method uses the slope f(x)
of the line to project to the
x axis and find the root.
f(xi)
• The method converges on x f  x 
i, i

the solution quickly.

f(xi-1)
f(xi )
xi 1 = xi -
f (xi )

x
xi+2 xi+1 xi
Derivation
f(x) AB
tan(  
AC

f(xi) B
f ( xi )
f ' ( xi ) 
xi  xi 1

 A f ( xi )
C
x xi 1  xi 
xi+1 xi f ' ( xi )
Algorithm for Newton-Raphson
Method
Step 1
Evaluate df/dx = f(x) symbolically: that is find an analytical
expression for the first derivative of the function with respect to x.
Newton-Raphson Method: Step 2
Calculate the next estimate of the root

f(xi )
xi 1 = xi -
f'(xi )

Find the absolute relative approximate error

xi 1- xi
a = x 100
xi 1
Step 3
• Find if the absolute relative approximate error is greater than
the pre-specified relative error tolerance.

• If so, go back to step 2, else stop the algorithm.

• Also check if the number of iterations has exceeded the


maximum number of iterations.
Newton-Raphson Method: Example
• You are working for ‘DOWN THE TOILET COMPANY’ that makes
floats for ABC commodes. The ball has a specific gravity of 0.6
and has a radius of 5.5 cm. You are asked to find the distance to
which the ball will get submerged when floating in water.
Newton-Raphson Method: Example 1
Solution:
The equation that gives the depth ‘x’ to which the ball is submerged
under water is given by

f  x   x 3-0.165 x 2+3.993x10- 4
f  x   3x 2 -0.33x

Use the Newton’s method of finding


roots of equations to find the depth ‘x’
to which the ball is submerged under
water. Conduct three iterations to
estimate the root of the above
equation.
Graph of function f(x)

f  x   x 3-0.165 x 2
+3.993x10- 4
Iteration #1
x0  0.02
f  x0 
x1  x0 
f '  x0 
3.413x104
x1  0.02 
 5.4 x103
 0.08320
a  75.96%
Iteration #2
x1  0.08320
f  x1 
x2  x1 
f '  x1 
 1.670x104
x2  0.08320 
 6.689x103
 0.05824
a  42.86%
Iteration #3
x2  0.05824
f  x2 
x3  x2 
f '  x2 
3.717 x105
 0.05284 
 9.043x103
 0.06235
a  6.592%
Newton’s Method: computer algo
Do while |x2 - x1| >= tolerance value 1
or |f(x2)|>= tolerance value 2
or f’(x1) ~= 0

Set x2 = x1 - f(x1)/f’(x1)

Set x1 = x2;

END loop
Example 2:
• Let us solve the following
problem:
x5 + x3 + 4x2 - 3x – 2 = 0
Then f(x) = x5 + x3 + 4x2 - 3x – 2

df/dx = 5x4 + 3x2 + 8x - 3


• Let us first draw a graph and
see where are the roots;
• The roots are between (-1.7,-
1.3), (-1,0), & (0.5,1.5)
• Then let us write a computer
program in MATLAB that
will compute the root using
Newton-Raphson method.
Computer Program in MATLAB
% A program that Uses Newton-Raphson method
% to find the roots of x = x^3 +x^2 -3x -3
% Input: x0 = initial guess;
% n = number of iterations; default: n = 10;
% Output: x = estimate of the root;
n = 10; x0 = -2.0
x = x0; % Initial Guess

fprintf(' k f(x) dfdx x(k+1)\n');

for k=1:n
f = x^5 + x^3 + 4*x^2 - 3*x - 2;
dfdx = 5*x^4 + 3*x^2 + 8*x - 3;
x = x - f/dfdx;
fprintf('%3d %12.3e %12.3e %18.15f\n',k-1,f,dfdx,x);
end
Newton’s Method
Result of the computer program: x0 = -2.0 and iterations n = 10.

k f(x) dfdx x(k+1)


0 -2.000e+001 7.300e+001 -1.726027
1 -5.367e+000 3.651e+001 -1.579022
2 -1.043e+000 2.293e+001 -1.533545
3 -8.054e-002 1.944e+001 -1.529401
4 -6.276e-004 1.914e+001 -1.529368
5 -3.911e-008 1.914e+001 -1.529368
6 -2.665e-015 1.914e+001 -1.529368
7 3.553e-015 1.914e+001 -1.529368
8 -2.665e-015 1.914e+001 -1.529368
9 3.553e-015 1.914e+001 -1.529368

The root value is x = -1.529368 and it was obtained after 4 iterations.


Advantages

• Converges fast, if it converges


• Requires only one guess
Drawbacks
10
f(x)

f  x    x  1  0
3
5

-2 -1 2
0
0 3 1 2
x
Inflection Point
1
-5

-10

-15

-20
Drawbacks (continued)
1.0 0 E-0 5
f(x)
7.50 E-0 6

5.0 0 E-0 6

2 .50 E-0 6

0 .0 0 E+0 0
x
-0 .0 3 -0 .0 2 -0 .0 1 0 0 .0 1 0 .0 2 0 .0 3 0 .0 4
-2 .50 E-0 6
0.02
-5.0 0 E-0 6

-7.50 E-0 6

f  x   x 3  0.03 x 2  2.4x10 6  0
-1.0 0 E-0 5

Division by zero
Drawbacks (continued)
1.5
f(x)
1

0.5

x
0
-2 -0.063069
0 0.54990 2 4 4.462 6 7.53982
8 10

-0.5

-1

f  x   Sin x  0
-1.5

Root Jumping
Drawbacks (continued)
6
f(x)

f  x  x2  2  0
5

3
3

2
2

11
4

0
x
-2 -1 0 1 2 3
-1.75 -0.3040 0.5 3.142
-1

Oscillations near Local Maxima or Minima


Example 3:
• Let us solve the following
problem:
x3 - 4x2 + x – 10 = 0
Then f(x) = x3 - 4x2 + x – 10
df/dx = 3x2 -8 x + 1
• Let us first draw a graph and
see where are the roots;
• The roots are between (3,6).
• Then let us write a computer
program in MATLAB that
will compute the root using
Newton-Raphson method.
Example 3: Computer Program
% A program that Uses Newton-Raphson method
% to find the roots of x = x^3 +x^2 -3x -3
% Input: x0 = initial guess;
% n = number of iterations; default: n = 10;
% Output: x = estimate of the root;
n = 10; x0 = 3.0
x = x0; % Initial Guess

fprintf(' k f(x) dfdx x(k+1)\n');

for k=1:n
f = x^3 - 4*x^2 + x – 10.0;
dfdx = 3*x^2 - 8*x + 1.0;
x = x - f/dfdx;
fprintf('%3d %12.3e %12.3e %18.15f\n',k-1,f,dfdx,x);
end
Example 3: results
Result of the computer program: x0 = -2.0 and iterations n = 10.

k f(x) dfdx x(k+1)


0 -1.600e+001 4.000e+000 7.000000
1 1.440e+002 9.200e+001 5.434783
2 3.781e+001 4.613e+001 4.615102
3 7.716e+000 2.798e+001 4.339292
4 7.280e-001 2.277e+001 4.307327
5 9.181e-003 2.220e+001 4.306913
6 1.526e-006 2.219e+001 4.306913
7 4.796e-014 2.219e+001 4.306913
8 3.553e-015 2.219e+001 4.306913
9 3.553e-015 2.219e+001 4.306913
>>

The root value is x = 4.306913 and it was obtained after 5 iterations.


Example 4:
• Let us solve the following
problem:
exp(x)+x - 10 = 0
Then f(x) = exp(x) + x – 10.
df/dx = exp(x) + 1.
• Let us first draw a graph and see
where are the roots;
• The roots are between (0, 4).
• Then let us write a computer
program in MATLAB that will
compute the root using Newton-
Raphson method.
Example 4: Computer Program
% A program that Uses Newton-Raphson method
% to find the roots of x = x^3 +x^2 -3x -3
% Input: x0 = initial guess;
% n = number of iterations; default: n = 10;
% Output: x = estimate of the root;
n = 10; x0 = 0.0
x = x0; % Initial Guess

fprintf(' k f(x) dfdx x(k+1)\n');

for k=1:n
f = exp(x) + x – 10.0;
dfdx = exp(x) + 1.0;
x = x - f/dfdx;
fprintf('%3d %12.3e %12.3e %18.15f\n',k-1,f,dfdx,x);
end
Example 4: results
Result of the computer program: x0 = -2.0 and iterations n = 10.

k f(x) dfdx x(k+1)


0 -9.000e+000 2.000e+000 4.500000
1 8.452e+001 9.102e+001 3.571415
2 2.914e+001 3.657e+001 2.774566
3 8.806e+000 1.703e+001 2.257515
4 1.817e+000 1.056e+001 2.085456
5 1.337e-001 9.048e+000 2.070678
6 8.746e-004 8.930e+000 2.070580
7 3.803e-008 8.929e+000 2.070580
8 0.000e+000 8.929e+000 2.070580
9 0.000e+000 8.929e+000 2.070580
>>

The root value is x = 2.070580


Why convergence is so rapid?
Let us assume that exact root is a and error in nth iteration is en . Then

xn  a  en
f ( xn )
However, xn 1   a  en  
f ( xn )

The Taylor Series for f(a - en) and its derivative is


en2
f  a  en   f  a   en f  a   f  a   
2
en2
f  a  en   f  a   en f  a   f  a   
2
Why convergence is so rapid?
Putting the Taylor Series for f(a - en) and its derivative in the formula

f ( xn )
xn 1   a  en  
f ( xn )
 en2 f  a  
xn 1   a  en    en   
 2 f (a ) 
en2 f  a 
a
2 f (a)
Hence error in (n+1)th iteration is proportional to (en)2 and that is why it
converges rapidly.

You might also like