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
clc;
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;
    drawnow();
    plot(abs(p),'r','linewidth',3);
    grid;
    if (count > 300)
        fprintf('Error...! Solution not converging !!! \n');  % printing the error message
        break;
    end
end
% plot(abs(p));
if (count < 300)
    fprintf('The solution = ');  %printing the result
    x
    fprintf('\nNumber of iteration taken = %d\n',count);
end

Try it for different functions
Thank you...

19 comments:

  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?

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

      Delete
  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

    ReplyDelete
  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'

    ReplyDelete
  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

    ReplyDelete
  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-https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CC0QFjAA&url=http%3A%2F%2Fwww1.au.edu.tw%2Fox_view%2Fedu%2Ftojms%2Fj_paper%2FFull_text%2FVol-25%2FNo-3%2F25(3)8-8(313-332).pdf&ei=MquRUrjNE8HsrAfE64BY&usg=AFQjCNE2BkEHd_JA6FucJfd9KDRuu2rRhg&bvm=bv.56988011,d.bmk

    ReplyDelete
    Replies
    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.

      Delete
  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

    ReplyDelete
    Replies
    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.

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

    ReplyDelete
    Replies
    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.

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

    ReplyDelete
    Replies
    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.

      Delete
  9. hello, what does it mean?

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

    ReplyDelete