0% found this document useful (0 votes)
17 views8 pages

MATLAB Session 2 Newton-Raphson Method Fall 2021-2022

Newton raphson

Uploaded by

dqvd8qrssh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views8 pages

MATLAB Session 2 Newton-Raphson Method Fall 2021-2022

Newton raphson

Uploaded by

dqvd8qrssh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Pharos University in Alexandria

Faculty of Engineering Fall 2021-2022


Department of Basic Sciences

EB207 (Numerical Analysis with MATLAB)


MATLAB Session 2_ Newton-Raphson Method

 Objectives:
- Applying fundamental concepts of presented numerical analysis methods.
- Use MATLAB to implement the Newton-Raphson method and relate it to the theoretical problems.

 The Newton-Raphson Method:

This numerical method is also an iterative method that could be applied for solving nonlinear equations
with single variable like 𝑓(𝑥) = 0 over an interval [𝑎, 𝑏]. It is based on the fact that a root of an equation
is the point crossing the x-axis as y = 0. It is sometimes called Newton method and this algorithm is the
best-known method of finding roots for a good reason: it is simple and fast, that’s why it is widely used
but only if the first derivative of a continuous 𝑓(𝑥) exists and is continuous too around the solution in the
interval [a,b] and its value at the convergent sequence of points does not equal zero. This could be
considered its only drawback. The success of the Newton–Raphson procedure depends entirely on the
initial estimate of p0. If a good starting point that is sufficiently close to the solution is used, convergence
to the solution is very rapid. Otherwise, the results are unpredictable.

Figure 1: The Graphical interpretation for the Newton-Raphson Method.

The concept of the Newton-Raphson method is illustrated graphically in figure 1 and the procedure to it
toward the solution of the nonlinear equation 𝑓(𝑥) = 0 is described in steps as follows:

Page 1 of 8
Pharos University in Alexandria
Faculty of Engineering Fall 2021-2022
Department of Basic Sciences

➱Step 1: Make sure that 𝑓(𝑥) and 𝑓′(𝑥) is a continuous function over the interval [a, b] to guarantee
that 𝑓(𝑥) = 0 have a solution within that interval.

➱Step 2: Let’s assume p0 to be an initial guess for the root of 𝑓(𝑥) = 0 within the interval [a,b].

➱Step 3: Compute 𝑓(𝒑𝒏 ) and 𝑓′(𝒑𝒏 ) -that should not be equals to zero- to fit in the main formula that
we are following:
𝒇(𝒑𝒏 )
𝒑𝒏+𝟏 = 𝒑𝒏 −
𝒇′(𝒑𝒏 )
➱Step 4: Iterations continue until the stopping criteria of the absolute error: |𝑝𝑛+1 − 𝑝𝑛 | < 𝜀, where 𝜀
is called the stopping tolerance and pn+1 is the approximated root allocated within the allowable accuracy.

Then this Newton iterative procedure could be cast into MATLAB algorithm as follows;

MATLAB M-file
%Inputs functions and variables section
syms x
f=input('Enter the function,f(x):');
func=inline(f); %to define functions which will be evaluated
a= input('Enter the x-lower of the interval, a:');
b= input('Enter the x-upper of the interval, b:');
p0= input('Enter the initial guess within [a,b], P0: ');
df=diff(f,x);
dfunc=inline(df);
dfp0= dfunc(p0);
% Graph of the function section
xval = linspace(a,b,100);% to generate equispaced-points between [a,b]
for i=1:100
yval(i) = func(xval(i));
end
plot(xval,yval);
title('The graph of the input function')
grid on;
%%%% Newton-Raphson Method section
% condition check
if dfp0==0
error('Derivative equals zero..Root finding failed!!');
end

Page 2 of 8
Pharos University in Alexandria
Faculty of Engineering Fall 2021-2022
Department of Basic Sciences

% the remaining inputs variables


tol= input('Enter the stopping tolerance, Tol:');
maxitr= input('Enter max. no. of interations, MaxItr:');
% iterations begins here to find the root
p(1)=p0; %Initialization for 1st Element of the main formula
for i=1:maxitr
fpi=func(p(i));
dfpi=dfunc(p(i));
if dfpi==0
error('Derivative equals zero..Root finding failed!!');
end
p(i+1)=p(i)-fpi/dfpi;
if abs(p(i+1)-p(i))<= tol ,break;
end
end
if abs(p(i+1)-p(i))>tol
error('Guess is far from solution..Root finding failed..Try again!!');
else
r_final= p(i+1); % the root which the algorithm has reached
% output display section
disp('The required root of the equation f(x)=0 is:')
disp(r_final)
fprintf('This solution is in %d iterations \n',i)
end

As the previous M-file is suitable for any functions within any interval and with any tolerance as well any
number of iterations, so all you need now is to run it with any problem data.

Example (1):

Write an M-file that performs 20 iterations using the Newton-Raphson method to find an approximate
value to the root of the equation x2-2sin(x2) = 0 to achieve an accuracy of 10-4 within the interval
[1,2] considering 1.5 as the initial guess. Compare the result with the one reached using the bisection
method from session 1 and comment on what you will get.

Page 3 of 8
Pharos University in Alexandria
Faculty of Engineering Fall 2021-2022
Department of Basic Sciences

Solution:

MATLAB command window/ Figure

>> Newton_Raphson_method_algorithm

Enter the function,f(x):x^2-2*sin(x^2)


Enter the x-lower of the interval, a:1
Enter the x-upper of the interval, b:2
Enter the initial guess within [a,b], P0: 1.5

Enter the stopping tolerance, Tol:1e-4


Enter max. no. of iterations, MaxItr:20
The required root of the equation f(x)=0 is:
1.3768

This solution is in 4 iterations>>

Comment: We notice that the convergence criterion is reached fast near the root in Newton Raphson
method compared to the bisection method which provides time as well as cost saving.

Page 4 of 8
Pharos University in Alexandria
Faculty of Engineering Fall 2021-2022
Department of Basic Sciences

Example (2):
Write an M-file that performs different iterations using the Newton Raphson method to find an
approximate value to the root of the following equations to achieve an accuracy of 10-4 within the indicated
intervals considering the mentioned initial guesses:

(a) 𝑒 𝑥 − 3𝑥 , 0 ≤ 𝑥 ≤ 2 with P0= 1 and P0= 1.2


3 2
(b) 𝑥 + 2𝑥 − 2𝑥 + 1 , -4 ≤ 𝑥 ≤ -1 with P0= -2 and -1 ≤ 𝑥 ≤ 2 with P0= 1
(c) 𝑒 𝑥 − sin𝑥 , -1 ≤ 𝑥 ≤ 1 with P0= 0

Solution:
a)
MATLAB command window/ Figure
>> Newton_Raphson_method_algorithm
Enter the funcution,f(x):exp(x)-3*x
Enter the x-lower of the interval, a:0
Enter the x-upper of the interval, b:2
Enter the initial guess within [a,b], P0: 1

Enter the stopping tolerance, Tol:1e-4


Enter max. no. of interations, MaxItr:20
The required root of the equation f(x)=0 is:
0.6191
This solution is in 5 iterations
Page 5 of 8
Pharos University in Alexandria
Faculty of Engineering Fall 2021-2022
Department of Basic Sciences

>> Newton_Raphson_method_algorithm
Enter the function,f(x):exp(x)-3*x
Enter the x-lower of the interval, a:0
Enter the x-upper of the interval, b:2
Enter the initial guess within [a,b], P0: 1.2

Enter the stopping tolerance, Tol:1e-4


Enter max. no. of interations, MaxItr:20
The required root of the equation f(x)=0 is:
1.5121

This solution is in 6 iterations

b)
MATLAB command window/ Figure
>> Newton_Raphson_method_algorithm
Enter the function,f(x):x^3+2*x^2-2*x+1
Enter the x-lower of the interval, a:-4
Enter the x-upper of the interval, b:-1
Enter the initial guess within [a,b], P0: -2

Page 6 of 8
Pharos University in Alexandria
Faculty of Engineering Fall 2021-2022
Department of Basic Sciences

Enter the stopping tolerance, Tol:1e-4


Enter max. no. of interations, MaxItr:20
The required root of the equation f(x)=0 is:
-2.8312

This solution is in 7 iterations

>> Newton_Raphson_method_algorithm
Enter the function,f(x):x^3+2*x^2-2*x+1
Enter the x-lower of the interval, a:-1
Enter the x-upper of the interval, b:2
Enter the initial guess within [a,b], P0: 1

Enter the stopping tolerance, Tol:1e-4


Enter max. no. of interations, MaxItr:20
Error using Newton_Raphson_method_Final (line 42)
Guess is far from solution..Root finding..Try again!!
Page 7 of 8
Pharos University in Alexandria
Faculty of Engineering Fall 2021-2022
Department of Basic Sciences

c)
MATLAB command window/ Figure

>> Newton_Raphson_method_algorithm
Enter the function,f(x):exp(x)-sin(x)
Enter the x-lower of the interval, a:-1
Enter the x-upper of the interval, b:1
Enter the initial guess within [a,b], P0: 0

Error using Newton_Raphson_method_Final (line 24)


The method failed to find the root..!!

© By Engineering Mathematics Group

Page 8 of 8

You might also like