Producing 100 iterations from Newton's method in matlab

Click For Summary

Homework Help Overview

The discussion revolves around implementing Newton's method for finding roots of a function in MATLAB, specifically focusing on producing 100 iterations of the method starting from an initial guess of x_0 = 0.5.

Discussion Character

  • Exploratory, Mathematical reasoning, Problem interpretation

Approaches and Questions Raised

  • Participants explore different ways to structure the loop for iterations, with suggestions on initializing variables and defining the function and its derivative. There are questions about whether to define these functions inside or outside the loop.

Discussion Status

Participants are actively discussing various coding approaches and sharing their thoughts on the structure of the MATLAB code. Some guidance has been offered regarding the initialization of arrays and the potential need for defining functions, but there is no explicit consensus on the best method.

Contextual Notes

There are considerations about the specific function to be used and its derivative, with participants noting that the choice of function could affect the number of iterations needed to converge to a solution.

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!
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 24 ·
Replies
24
Views
4K
Replies
4
Views
2K
  • · Replies 21 ·
Replies
21
Views
4K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 17 ·
Replies
17
Views
4K