Sunday, February 10, 2013

Newton Raphson Method

Hello everybody...
The following is a sample program to understand finding solution of a non linear equation using Newton Raphson Method. This program is not a generalised one. But you can understand the basic idea of the method and how to implement it using MATLAB. For more information about this method please try this

% This programme is to find the solution of a non linear equation by Newton Raphson method.
% This is not a generalized program
clear all;
format long;
syms x;
e = 1e-5;                      % setting the tolerance value
dx = e + 1;
f = log(2-x) + x^2;               % enter your function here;
x = 7;                          % initially assumed value of x
count = 0;                      % setting counter to know the no of iterations taken
p = zeros(1,1);
while (abs(dx) > e)             % initialising the iteration and continue until the error is less than tolerance
    dx = eval(f/(diff(f)));     % calculating dx, diff is used for finding the differentiation of the fuction
    x = x - dx                 % updating the value of x
    count = count + 1;          % incrimenting the counter
    p(count) = x;
    if (count > 300)
        fprintf('Error...! Solution not converging !!! \n');  % printing the error message
% plot(abs(p));
if (count < 300)
    fprintf('The solution = ');  %printing the result
    fprintf('\nNumber of iteration taken = %d\n',count);

Try it for different functions
Thank you...


  1. I just checked about syms on the net, i found that this feature is available for MATLAB 2013 version. I'm using an older version of MATLAB and it doesn't support syms function. Is their any alternative for this?

    1. here we are creating a symbolic variable x. I did this in MATLAB R2009b, Its working for me. Try entering x = sym('x');

  2. Syms is used to define a variable...Since u r going to use x in d lines below line no 6 therfore it is required to declare the variable first n then u can use it

  3. i have used matlab 2008 version and now using 2010.but syms can work in both the version plz put a space between syms and the variable 'x'

  4. i want a program which will correct a matrix (or continue to manupulate the matrix) till a tolerance value of 0.000001 is achieved

  5. hello dear... i need your help....i am persuing ph.d....i want to solve these papers using matlab.. i m totally new in matlab ...pls pls help me ...thanxx in advance....regards
    paper link-,d.bmk

    1. Hi,
      Unfortunately I am also busy with my Masters thesis. So I won't be able to go through your paper and reply you. Instead, if you have any specific question on Matlab, please drop me a mail and I will do the best I can.

  6. Hy Muhammed can you help me Finding the for this on MATLAB
    power flow.

    Project outcome: Design a program in MATLAB to numerically solve a set of generic non-linear equations in the form of f(x)=0
    Usning Newton Raphson method

    1. Hi,
      Its simple. Whats your doubt? The procedure is as follows... First you need to linearize the set of equations using Taylor's series expansion. Then find the linear set of equations, and solve it. And continue the iteration until you reaches acceptable level of tolerance.

  7. Hi, what is the meaning of line no. 8: dx = e + 1; ?

    1. I am setting the threshold value of error, e = 1e-5; For initial iteration the value of dx should be greater than the threshold value. Hence I gave dx = e+1.
      Hope this helps.

  8. hi muhamed,i writing my masters to matlab,can i solve a ninth order polynomial using this matlab code

    1. Change this line with your ninth order polynomial?
      f = log(2-x) + x^2;

      It will give only one solution, it depends the initial value you give.

  9. hello, what does it mean?

    ??? Newton-raphson method
    Error: Unexpected MATLAB expression,