Producing 100 iterations from Newton's method in matlab

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
5 replies · 8K views
noblegas
Messages
266
Reaction score
0

Homework Statement

[tex]x_k+1=x_k-(f(x_k)/f'(x_k)), x_0=.5[/tex]

Homework Equations


The Attempt at a Solution



Not really a math question, just a computation question. I want to put a do loop around Newton's method using mathlab.

[tex]x_1=x_0-(f(x_0)/f'(x_0)), x_0=.5,x_2=x_1-(f(x_1)/f'(x_1)),..., x_100=x_99-f(x_99)/f'(x_99)[/tex]

Would I write my 100 iterations in MATLAB like this:

for i=[0:100]
x_0=.5
x_i+1=x_i-f(x_i)/f'(x_i)

end
 
Physics news on Phys.org
noblegas said:
Would I write my 100 iterations in MATLAB like this:

for i=[0:100]
x_0=.5
x_i+1=x_i-f(x_i)/f'(x_i)

end

Probably not. I think it'd be easier to do it this way:

x(0)=0.5
for i = 1:101
x(i)=x(i-1)-f(x(i-1))/f'(x(i-1))
end

mostly because it's nicer looking to call the previous number than to call the current number and insert it into the future one. Also, you will probably need to define f(x) and f'(x) inside the loop (not really sure, you might be able to call it outside).
 
jdwood983 said:
Probably not. I think it'd be easier to do it this way:

x(0)=0.5
for i = 1:101
x(i)=x(i-1)-f(x(i-1))/f'(x(i-1))
end

mostly because it's nicer looking to call the previous number than to call the current number and insert it into the future one. Also, you will probably need to define f(x) and f'(x) inside the loop (not really sure, you might be able to call it outside).

Thats only the algorithm right? Its not the code for the whole loop.
 
noblegas said:
Thats only the algorithm right? Its not the code for the whole loop.

For the most part, you are correct. As I said, it is probable that f(x) and f'(x) should be defined outside the loop, but they might not need be. So really it would be

Code:
f(a) =zeros(100);
fp(a)=zeros(100);

f(a)='whatever the function is'
fp(a)='whatever the function derivative is'

then the loop.

However, if you don't know what the function actually is, you would possibly need to call it inside the loop by writing

Code:
function Newton=nwtn(f)

fp(a)=zeros(100);

fp(a)=diff(f);

then the loop.

where in both cases, fp=f'.
 
jdwood983 said:
For the most part, you are correct. As I said, it is probable that f(x) and f'(x) should be defined outside the loop, but they might not need be. So really it would be

Code:
f(a) =zeros(100);
fp(a)=zeros(100);

f(a)='whatever the function is'
fp(a)='whatever the function derivative is'

then the loop.

However, if you don't know what the function actually is, you would possibly need to call it inside the loop by writing

Code:
function Newton=nwtn(f)

fp(a)=zeros(100);

fp(a)=diff(f);

then the loop.

where in both cases, fp=f'.

Lets say I wanted to put () = ^2, _0 = 0.5 in the do loop. How would I go about doing that? x(0)=0.5
for i = 1:101
x(i)=x(i-1)-f(x(i-1))/f'(x(i-1))
end
 
noblegas said:
Lets say I wanted to put () = ^2, _0 = 0.5 in the do loop. How would I go about doing that? x(0)=0.5
for i = 1:101
x(i)=x(i-1)-f(x(i-1))/f'(x(i-1))
end

If that is your function that you want to use then it'd be more easily done the first way I said,

Code:
f = zeros(50);
fp = zeros(50);
x = zeros(50);

x(1)=0.5;
for j = 2:51
     f(j-1) = x(j-1)*x(j-1);
     fp(j-1) = 2*x(j-1);
     x(j)=x(j-1)-f(j-1)/fp(j-1);
end

where, again, fp=f'.

Now there is the issue that, when I ran this program, the value of x(j) was minimized to zero in 14 iterations (which makes sense since the Newton method is trying to find the minimizing value of [itex]x[/itex] in the function [itex]f(x)[/itex], which would be zero in this case), so I just cut down the number of iterations to 50.
If you wanted to try a more complicated function, you just replace the f(j-1)=x(j-1)*x(j-1) part with what ever the function is and then I'd suggest WolframAlpha to calculate the derivative if you can't do it by hand and put that in for fp(j-1).

Hope this helps!