Solving an Equation using Newton-Raphson Method

36 views (last 30 days)
I am trying to write a function file that can invoke Newton Raphson method. I need to have the function input to be the function(f1) I am analyzing, its derivative(df1), an interval( R), and an increment size(I) and the function should out put the initial guess and its corresponding root much like this:
function [ InitialGuess , Roots ] = NewtonRaphson( f1,df1,R,I )
The function should be able to use the interval and increment size to "scan" for potential roots by analyzing when the derivative changes sign and assign an initial guess which can be used in the actual Newton Raphson equation. I do not know where to start with writing this code and some insight would be appreciated. Also this is an example of the type of problem it should be able to solve:
f(x)=1.7x^4-2.5x^3-5.0x^2+7.2x-1.2 on the interval [-5, 5] and with increments 0.1 and 0.5.

Answers (2)

Hugo
Hugo on 17 Mar 2015
Are the functions polynomials as the example you mentioned?
If they are, assume that the order of the polynomial is N (in your example would be 4). Then you can derivate until order N-2, which will leave you something like f^(N-2)(x)=alpha x^2 + beta x+ gamma, and compute the zero there using the usual formulas for quadratic equations. The point here is that these two zero crossings in the N-2 derivative are relative extrema in the N-3 derivative. In consequence, there will be three zeros, one at the left, one between, and one at the right of the zeros found for the N-2 derivative. To find these zeros, you then apply the Newton-Raphson method starting from any point inside that interval, although it may well be a better idea to do a binary search trying to get near the zero crossing to avoid divergence of the method. You repeat this until you find the zeros of the original function.
Recall that the derivatives can be easily computed by writing the coefficients in a vectors. In your case, it would be f_0=[1.7,-2.5,-5,7.2,-1.2]. This could be called the derivative of order zero. The first derivative f^1 can be obtained by doing f_1 = f_0(1:end-1) .* numel(f_0)-1:-1:1, the second derivative by f_2 = f_1(1:end-1).* numel(f_1)-1:-1:1, and so on.
If they are not polynomials, then it is a problem because you do not know how many zeros there may be. What you can do then is to take any two points for which the sign of the function differs and apply the Newton Raphson method. Use this point to separate the interval in two new intervals and do the same procedure for each interval. This can be done with a recursive algorithm, that is, a function that calls to itself until no sign change is found.
Hope this helps.
  1 Comment
Angel Armenta
Angel Armenta on 18 Mar 2015
So can you give me some sample code of the recursive algorithm so I can get an idea of how to implement it?

Sign in to comment.


Farhad Sedaghati
Farhad Sedaghati on 3 Aug 2015
The following link is the matlab code to perform Newton-Raphson's Method:

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!