Producing 100 iterations from Newton's method in matlab

In summary, the conversation discusses implementing a do loop around Newton's method using MATLAB. The suggested approach is to define the function and its derivative outside the loop, then use the loop to calculate the values of x. The conversation also mentions the possibility of using a more complicated function and finding its derivative using WolframAlpha.
  • #1
noblegas
268
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
  • #2
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).
 
  • #3
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.
 
  • #4
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'.
 
  • #5
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
 
  • #6
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!
 

FAQ: Producing 100 iterations from Newton's method in matlab

What is Newton's method?

Newton's method is an algorithm used to find the roots of a given function. It is based on the idea of using linear approximations to iteratively approach the roots of a function.

How does Newton's method work?

Newton's method starts with an initial guess for the root of a function. Then, it uses the function's derivative to calculate the slope of the tangent line at that point. The root of the tangent line is then used as the next guess, and this process is repeated until the desired level of accuracy is achieved.

Why is it important to produce 100 iterations from Newton's method?

Producing 100 iterations from Newton's method allows for a more accurate approximation of the root of a function. It also helps to ensure that the algorithm has converged to the correct root and is not stuck in a local minimum or maximum.

What are the inputs for implementing Newton's method in MATLAB?

The inputs for implementing Newton's method in MATLAB include the function to be evaluated, its derivative, an initial guess for the root, and the desired level of accuracy.

How can I produce 100 iterations from Newton's method in MATLAB?

To produce 100 iterations from Newton's method in MATLAB, you can use a for loop that iteratively calculates the next guess for the root using the previous guess and the function's derivative. The loop can be set to run for 100 iterations or until the desired level of accuracy is achieved.

Similar threads

Replies
2
Views
758
Replies
21
Views
3K
Replies
16
Views
3K
Replies
5
Views
1K
Replies
3
Views
2K
Replies
17
Views
3K
Back
Top