## Welcome to the World of Modelling and Simulation

### What is Modelling?

This blog is all about system dynamics modelling and simulation applied in the engineering field, especially mechanical, electrical, and ... ### Finding Roots of a Equation by Newton-Raphson Method

In this tutorial, we are interested to the roots of the following function.

f(x) = sin x + 2 - 0.1 x

To find the value of x or roots of the equation, Newton-Raphson method is applied. For the proper initial guess for x, graphical method is applied to visualize the characteristics of this nonlinear function. This graphical technique also helps to find multiple roots for the unknown function. The following MATLAB commands generate the plot of the function, f(x). This function is plotted with respect to the values of x from -50 to +50.

>> X=-100:100;
>> plot(X,(sin(X)+2-(0.1*X)))

From the following figure 1, it is clear that the equation has seven roots and plotting the function helps to get the roots. The function changes its sign from, x = 10 - 11, 11 -12, 16 -17, 18 - 19, 21 - 22, 25 - 26, and 27 - 28. And, the roots of the equation must lie between these intervals. Now, to get good approximations of the roots, a Newton-Raphson algorithm is written in MATLAB.

Figure 1: Plot of the function f(x) to identify the intervals where roots of the function lie.

The program is developed by MATLAB user defined function called “sinusoidal”. The initial assumptions for approximating seven roots of x are 10.5, 11.5, 16.5, 18.5, 21.5, 25.5 and 27.5 respectively. The codes are given below. It is straightforward to run the program seven times as we have seven initial guesses for the corresponding roots.

## Newton-Raphson Algorithm by MATLAB

% Definition of a function "sinusoidal" to solve equation by Newton-Raphson
function [x,ea] = sinusoidal(X, etol)
format long;

% Input: X=21.5, etol=Tolerance definition for error
% Output: x=Solution of equation, ea=Calculated error in loop

% Program Initialization

solution = 21.5;

% Iterative Calculations

while (1)
solutionprevious = solution;

% Implementation of Newton-Raphson Method

solution = X - (sin(X)+2-(0.1*X))/(cos(X)-0.1);
X=solution;

if solution~=0                         % Approximate percent relative error

ea=abs((solution - solutionprevious)/solution)*100;
end

if ea<=etol,                  % Condition to meet specified error tolerance

break,
end

x = solution;

disp(x);

end

% Display of output parameters

disp(x);
disp(ea);

end

## Program Outputs:

>> sinusoidal (10.5,1e-10)
ans =
10.636782424281707
>> sinusoidal(11.5, 1e-10)
ans =
11.562055865585652
>> sinusoidal (16.5,1e-10)
ans =
16.107752970689450
>> sinusoidal (18.5,1e-10)
ans =
18.721338781142521
>> sinusoidal (21.5,1e-10)
ans =
21.809224297055675
>> sinusoidal (25.5,1e-10)
ans =
25.744697403517950
>> sinusoidal (27.5,1e-10)
ans =
27.435908953388580

So, the seven roots from the program are 10.637, 11.562, 16.108, 18.721, 21.809, 25.745 and 27.436. In all the cases, the error tolerance limit is kept extremely small (0.0000000001) to get more accurate and precise results.

#Blog #Blogger #NewtonRaphson #Matlab #Sinusoidal