MATLAB program – Newton Raphson Method

Hello friends! In this post, I want to share with you, a MATLAB program which I wrote for implementing Newton Raphson Method.

For those who are unaware, Newton Raphson method fondly known as NR method is an iterative method for finding the solution or roots of equations arising from the differential equations. I studied Newton Raphson method in 3 of my courses namely “Applied Numerical Methods”, “Optimization and Soft Computing” and “Power System Analysis”.
To learn more about Newton Raphson method, you can refer various literature texts available online and offline.

A concise explanation of NR method is as follows:

  1. Consider a function f(x), say f(x) = x^2 - 5
  2. To find the approximate root of f(x), we will assume an initial solution of f(x). Let us consider x=1
  3. Then we need to find out the amount of accuracy required in the approximations. Let us consider it as ‘e’.
  4. The formula of NR method is given by “x(n+1) = x(n) - (f(x)/f'(x))”. Using this relation, the value of ‘x’ is calculated in each iteration and the difference between the current and previous values of ‘x’ is estimated.
  5. After every iteration, the difference is compared with the accuracy value.
  6. If the difference is less than the accuracy value, then iteration is stopped and the current value of x is considered as the approximate root of the solution.
  7. If the difference remains larger than the accuracy value even after a large number of iteration, then the solution is said to be non-converging.

The MATLAB implementation of Newton Raphson Method is given below:



%***** MATLAB PROGRAM FOR NEWTON RAPHSON METHOD****!

%*************************************************!

%

% PROGRAMMED BY: AMIT BISWAL

% URL          : http://amitbiswal.blogspot.com

%

%*************************************************!

%---------NEWTON RAPHSON METHOD FORMULA-----------!

%

% x(n+1) = x(n) - (f(x)/f'(x))

% 

% ************************************************!

close all

clc

clear all



syms x

%---START OF FUNCTION-----------!

f= (x^2)+ (2*x) - 10

%---END OF FUNCTION-------------!

fdash=diff(f);

x=input('Enter the initial solution, x0=');

e=input('Enter the value of error, e=');

iteration=0;

error=1;

while (error>e)

iteration=iteration+1;

fprintf('********************Iteration no. %0.0f********************',iteration)

error=abs(x);

x=x-subs(f/fdash)

error=abs(x-error)

if(iteration>100) %exit condition for non-coverging solution

    break

end

end

fprintf('*************************RESULT****************************');

if(iteration>100)

    fprintf('\nSOLUTION NOT CONVERGING !\n TRY WITH DIFFERENT INITIAL SOLUTION/ERROR')

else

    fprintf('\nSolution x= %0.20f\nNumber of Iteration= %0.0f',x,iteration);

end

fprintf('\n***********************************************************');


If you have any query or issue with the code then you can comment below. Thanks for reading this post.

4 comments:

  1. i don't understand the coding for this method please help me

    ReplyDelete
  2. please more explain

    ReplyDelete
  3. please more explain step by step

    ReplyDelete
  4. please more explain

    ReplyDelete

If you liked this blog then Join me at:
Facebook
Twitter