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's Method

Subject: Newton's Method

From: James

Date: 20 Apr, 2012 02:51:22

Message: 1 of 4

Hey I've never used MATLAB before and I am having trouble getting my simple Newton's method code to work. Here's what I have so far:

clear all
clc
x = zeros(6,1);
x(1) = 3.0;
f = x.^2-1;
df = 2.*x;
for n = 1:6
    x(n+1) = x(n) - f(n)/df(n)
end

After two iterations, all of my x values go to Inf. Maybe I've just been staring at this for too long, but its too annoying to quit. Thanks.

Subject: Newton's Method

From: Nasser M. Abbasi

Date: 20 Apr, 2012 03:18:06

Message: 2 of 4

On 4/19/2012 9:51 PM, James wrote:
> Hey I've never used MATLAB before and I am having trouble getting my simple Newton's method code to work. Here's what I have so far:
>
> clear all
> clc
> x = zeros(6,1);
> x(1) = 3.0;
> f = x.^2-1;
> df = 2.*x;
> for n = 1:6
> x(n+1) = x(n) - f(n)/df(n)
> end
>
> After two iterations, all of my x values go to Inf. Maybe I've just
>been staring at this for too long, but its too annoying to quit. Thanks.


---------------------
f = @(x) x^2-1;
df = @(x) 2*x;

x = .1; %initial guess
keep_trying = true;
k = 0; %counter

while keep_trying
     
     x = x - f(x)/df(x);
     k = k+1;
     
     if abs(f(x)) < 0.001 %change tolerance as needed
         keep_trying = false;
     end
     
     if k>15
        keep_trying = false; %give up !
     end
end

%assume we found a root for now
fprintf('root found at %f, after %d iterations\n',x,k)

%compare
fzero(f,.1)
--------------------------


--Nasser

Subject: Newton's Method

From: Roger Stafford

Date: 20 Apr, 2012 03:41:27

Message: 3 of 4

"James" wrote in message <jmqiva$bci$1@newscl01ah.mathworks.com>...
> x(1) = 3.0;
> f = x.^2-1;
> df = 2.*x;
> for n = 1:6
> x(n+1) = x(n) - f(n)/df(n)
> end
- - - - - - - - - -
  In your for-loop the values in 'f' and 'df' remain the same, never changing. Just because you wrote the formulas for these before entering the loop doesn't mean that they will automatically be recomputed. You have to arrange for that to happen inside the loop.

 x = 3;
 while abs(x^2-1) > 1e-12
  x = x - (x^2-1)/(2*x); % <-- Recompute f and df each time
 end

  Nasser has shown you a way using anonymous functions.

Roger Stafford

Subject: Newton's Method

From: prabhakaran m

Date: 20 Apr, 2012 05:16:19

Message: 4 of 4

Roger, i am also doing the same newton method. here code is not converging. what may the problem. also i need explanation when to use error greater or lesser than tolerance.

noiter=10
tolerence=1e-6;
del_int=[x0; y0; zta; rho; Rint]; % initial parameters
del=[];
del(1,:)=del_int;
for j=2:noiter
for i=n
%here in the loop i have found the H and J matrix. i have excluded its calcuations.
end
Jinv=inv(J);
tempo=del(j-1,:);
del(j,:)=tempo'-Jinv*H
error=max(abs(del(j,:)-del(j-1,:)))
if error <= tolerence
    final=j;
    break;
end
end
final_val=del(final,:)

when i evaluate the error, it goes on increasing. what may be the problem. also if reduce the tolerance value to 1e-2 it converges in the 2 iteration itself.
here is the single page reference form where i took the procedure
http://www.sendspace.com/file/x19u7q

Tags for 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