Newton Ralphson Method using Matlab

Newton Ralphson method is  finding successively better approximations to a functions root. In this method one initial guess of root is given. Then we find value of the function at that root. A tangent is drawn at that value of function then value of that tangent where it crosses x-axis is taken. This value is comparatively better root. Again value of function is found on that point and then again a tangent is drawn at that point and so on... This process is repeated until error has become significantly small.
Following figure is explaining this in a simple way.



MATLAB Code:

%Author: Muhammad Awais 2012-ee-506 UET Lahore
%This Function uses Newton Ralphson Method to find root of a function
%Input: Function , initial guess, minmum tolerace
%Output: Roots of function, Number of iterations used.
%Please input function in following way...
%syms x
%f=x.^3-20
function [y,iteration]=NewtonRalphson(f,x0,error)

g=diff(f);      %Differentiation of f
%older and new is to hold values of x in each iteration and last iteration.
older=0;
new=1;
%Number of iterations that it has to complete
iteration=1;

while ((abs(new-older)>error ) )
    older=x0;
    x1=x0-subs(f,x0)/subs(g,x0);  %Newton Ralphson Formula
    new=x1;
    x0=x1;
    iteration=iteration+1;

end

y=double(x1);   %Syms tool doesnt produces values in double so...

Output:


Problems:

1) Derivation

Finding derivation is a expensive process when we talk about computers. They take a lot of time determining derivation.

2)Convergence

Some time this method simply did't converge so algorithm fails.

3)Poor initial estimate

Poor initial estimate may make this  algorithm non-convergent. One such example is...
f(x)=|x|^a,\quad 0 < a < \tfrac{1}{2}  [1]

References: 

[1] https://en.wikipedia.org/wiki/Newton%27s_method

No comments:

Post a Comment

Thank you for your response.