MATLAB Session 2 Newton-Raphson Method Fall 2021-2022
MATLAB Session 2 Newton-Raphson Method Fall 2021-2022
Objectives:
- Applying fundamental concepts of presented numerical analysis methods.
- Use MATLAB to implement the Newton-Raphson method and relate it to the theoretical problems.
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.
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
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:
>> Newton_Raphson_method_algorithm
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:
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
>> 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
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
>> 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
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
Page 8 of 8