Producing 100 iterations from Newton's method in matlab

Click For Summary
The discussion focuses on implementing Newton's method in MATLAB to compute 100 iterations. The initial approach suggested using a loop to calculate each iteration, but a more efficient method was proposed using an array to store values. Participants emphasized the importance of defining the function and its derivative, either inside or outside the loop, depending on the context. A specific example was provided, demonstrating how to set up the function and its derivative for a quadratic equation. The conversation concluded with advice on adjusting the number of iterations based on the convergence behavior of the method.
noblegas
Messages
266
Reaction score
0

Homework Statement

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

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.

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)

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 x in the function f(x), 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!
 
(a) The polarisation pattern is elliptical with maximum (1,1) and minimum (-1,-1), and anticlockwise in direction. (b) I know the solution is a quarter-wave plate oriented π/4, and half-wave plate at π/16, but don't understand how to reach there. I've obtained the polarisation vector (cos π/8, isin π/8) so far. I can't find much online guidance or textbook material working through this topic, so I'd appreciate any help I can get. Also, if anyone could let me know where I can get more...

Similar threads

  • · Replies 1 ·
Replies
1
Views
640
  • · Replies 22 ·
Replies
22
Views
2K
  • · 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
3K
  • · Replies 17 ·
Replies
17
Views
3K