Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Newton Raphson Help

Subject: Newton Raphson Help

From: Chm

Date: 11 Dec, 2011 13:34:08

Message: 1 of 8

So I'm writing this code to find the root of a function. It works fine with some functions. However, whenever I try to solve cos(x) it returns a value where cos(x)=1 instead of 0. Can somebody help me figure out what the problem is? thank you!

function root = newton(g,cc,x0)
%function finding root using Newton-Raphson
%method. g is a function in terms of x, cc
%is the covergence criteria, and x0 is the
%initial guess
 
tol = cc; %set the tolerance of funtion
 
f = inline(g);
%covert g from an expression of x to a
%function(f(x))
 
x(1) = x0; %initial guess
d_f = inline(char(diff(g)));
%derivative of f(x)
 
i=2;
 
if f(i-1)<tol
    x(i) = x(i-1)-(f(x(i-1))/d_f(x(i-1)));
    %find x where f(x-i)<cc
    
    i=i+1;
end
disp('the root is: ');
root = x(i-1)
%result
 
end

Subject: Newton Raphson Help

From: Roger Stafford

Date: 11 Dec, 2011 17:25:08

Message: 2 of 8

> if f(i-1)<tol
> x(i) = x(i-1)-(f(x(i-1))/d_f(x(i-1)));
> %find x where f(x-i)<cc
> i=i+1;
> end
- - - - - - -
  There seems to be a misunderstanding in this code as to how an 'if' construct works in matlab. As you have written it, its interior will execute only one time at most and then only when your initial guess is already a solution within the proper tolerance bound. You state that for some functions your routine "works fine" but I can't see how that can possibly be true.

  What you need is a 'while' loop which will continue to repeat until its stated condition becomes false, and for this you would need f(i-1) > tol rather than the "less than" that you show. In other words, you want the loop to continue the iteration on x until such a time as the function lies within the proper tolerance bound at which time the loop is to exit.

Roger Stafford

Subject: Newton Raphson Help

From: Roger Stafford

Date: 11 Dec, 2011 18:37:08

Message: 3 of 8

"Roger Stafford" wrote in message <jc2p1k$jdm$1@newscl01ah.mathworks.com>...
> What you need is a 'while' loop which will continue to repeat until its stated condition becomes false, and for this you would need f(i-1) > tol rather than the "less than" that you show.

- - - - - - -
  My comment needs to be corrected to "you would need while abs(f(i-1)) > tol" to allow for adjustments from both too high and too low.

Roger Stafford

Subject: Newton Raphson Help

From: Chm

Date: 11 Dec, 2011 18:57:07

Message: 4 of 8

Thank you, Ill look into that. And if you try a function such as f(x)=x-5 my code will return ans=5. I don't know why it worked out after reading your explanation haha. Maybe it's jsut a coincidence

"Roger Stafford" wrote in message <jc2p1k$jdm$1@newscl01ah.mathworks.com>...
> > if f(i-1)<tol
> > x(i) = x(i-1)-(f(x(i-1))/d_f(x(i-1)));
> > %find x where f(x-i)<cc
> > i=i+1;
> > end
> - - - - - - -
> There seems to be a misunderstanding in this code as to how an 'if' construct works in matlab. As you have written it, its interior will execute only one time at most and then only when your initial guess is already a solution within the proper tolerance bound. You state that for some functions your routine "works fine" but I can't see how that can possibly be true.
>
> What you need is a 'while' loop which will continue to repeat until its stated condition becomes false, and for this you would need f(i-1) > tol rather than the "less than" that you show. In other words, you want the loop to continue the iteration on x until such a time as the function lies within the proper tolerance bound at which time the loop is to exit.
>
> Roger Stafford

Subject: Newton Raphson Help

From: Chm

Date: 11 Dec, 2011 19:01:09

Message: 5 of 8

also i'm just trying to find one root. and whatever the funtion returns isnt even one of the roots.

"Roger Stafford" wrote in message <jc2p1k$jdm$1@newscl01ah.mathworks.com>...
> > if f(i-1)<tol
> > x(i) = x(i-1)-(f(x(i-1))/d_f(x(i-1)));
> > %find x where f(x-i)<cc
> > i=i+1;
> > end
> - - - - - - -
> There seems to be a misunderstanding in this code as to how an 'if' construct works in matlab. As you have written it, its interior will execute only one time at most and then only when your initial guess is already a solution within the proper tolerance bound. You state that for some functions your routine "works fine" but I can't see how that can possibly be true.
>
> What you need is a 'while' loop which will continue to repeat until its stated condition becomes false, and for this you would need f(i-1) > tol rather than the "less than" that you show. In other words, you want the loop to continue the iteration on x until such a time as the function lies within the proper tolerance bound at which time the loop is to exit.
>
> Roger Stafford

Subject: Newton Raphson Help

From: Roger Stafford

Date: 11 Dec, 2011 20:57:07

Message: 6 of 8

"Chm" wrote in message <jc2ue3$58n$1@newscl01ah.mathworks.com>...
> Thank you, Ill look into that. And if you try a function such as f(x)=x-5 my code will return ans=5. I don't know why it worked out after reading your explanation haha. Maybe it's jsut a coincidence
- - - - - - -
  It has occurred to me that you might be calling on your 'newton' function repeatedly to achieve convergence to a root. If that is the case and you start with a negative f(x) where f has a negative second derivative, you would eventually converge to a root, with the inequality you used. Or perhaps you are using a linear function, as in your f(x) = x-5, where a single step would find the root.

  That is certainly not the way to efficiently do this algorithm. A single call on your 'newton' routine should perform all the needed iterations before exiting and that would require the use of matlab's 'while' function rather than an 'if'. You should have something like:

 x = x0;
 while abs(f(x)) > tol
  x = x - f(x)/f'(x); % where f' is the derivative
 end

Either that or use a for-loop which does a 'break' to exit when the tolerance condition is satisfied.

Roger Stafford

Subject: Newton Raphson Help

From: Rune Allnor

Date: 12 Dec, 2011 00:53:52

Message: 7 of 8

On 11 Des, 14:34, "Chm " <lrh...@gmail.com> wrote:
> So I'm writing this code to find the root of a function. It works fine with some functions. However, whenever I try to solve cos(x) it returns a value where cos(x)=1 instead of 0. Can somebody help me figure out what the problem is? thank you!

> function root = newton(g,cc,x0)

You need a function for the gradient as well.

> d_f = inline(char(diff(g)));

I don't like this line. First of all, you can not
use the Newton iteration with a fixed data vector.
The data points are computed along the way.

Second, you need a function to compute the gradient.
It is supplied either as a separate function,
or you need to somehow use g to compute the gradient
around a test point x.

Third, I don't understand what the CHAR function
has to do in here.

Maybe you mess around with symbolic fundtions.
If so,you should be aware that the Newton-Raphson
method works with numerical functions.

So all in all, you might want to go back to your
textbook and try and read the material a couple
of times over.

Rune

Subject: Newton Raphson Help

From: Steven_Lord

Date: 12 Dec, 2011 14:50:50

Message: 8 of 8



"Rune Allnor" <allnor@tele.ntnu.no> wrote in message
news:71b32cd3-6262-4502-a5ea-edc138e5b6a6@f11g2000yql.googlegroups.com...
> On 11 Des, 14:34, "Chm " <lrh...@gmail.com> wrote:
>> So I'm writing this code to find the root of a function. It works fine
>> with some functions. However, whenever I try to solve cos(x) it returns a
>> value where cos(x)=1 instead of 0. Can somebody help me figure out what
>> the problem is? thank you!
>
>> function root = newton(g,cc,x0)
>
> You need a function for the gradient as well.
>
>> d_f = inline(char(diff(g)));
>
> I don't like this line. First of all, you can not
> use the Newton iteration with a fixed data vector.
> The data points are computed along the way.
>
> Second, you need a function to compute the gradient.
> It is supplied either as a separate function,
> or you need to somehow use g to compute the gradient
> around a test point x.
>
> Third, I don't understand what the CHAR function
> has to do in here.

The OP has Symbolic Math Toolbox installed, and they realized that this:

root = newton('sin(x)', ...)

would work. The reason it works is (deep breath) that line will create an
inline function object from the string created by converting the symbolic
result of differentiating the function represented by the input string. So
the OP is using a function, albeit one they went a somewhat roundabout way
to create.

FYI for the OP: the Symbolic Math Toolbox version of DIFF for char arrays is
being removed; in future versions this code will go through the MATLAB
implementation of DIFF for char arrays, which will NOT treat the char array
as a function but as a regular old array of characters. When that happens,
diff('sin(x)') will return diff(double('sin(x)'));

http://www.mathworks.com/help/toolbox/symbolic/rn/bsllh45-1.html#bsllmg2-1

Instead, I recommend using function handles:

root = newton(@sin, ...)

and either passing a function handle that you can evaluate to determine the
derivative into newton as an additional input argument or approximating it
using finite differences.

http://en.wikipedia.org/wiki/Finite_difference_method

Of course, if you're not doing this for a class you could always use FZERO.

> Maybe you mess around with symbolic fundtions.
> If so,you should be aware that the Newton-Raphson
> method works with numerical functions.

Even though it doesn't look like it, the OP is using functionality from
Symbolic Math Toolbox.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us