1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Producing 100 iterations from Newton's method in matlab

  1. Nov 20, 2009 #1
    1. The problem statement, all variables and given/known data


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



    3. 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
     
  2. jcsd
  3. Nov 20, 2009 #2
    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).
     
  4. Nov 21, 2009 #3
    Thats only the algorithm right? Its not the code for the whole loop.
     
  5. Nov 21, 2009 #4
    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 (Text):

    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 (Text):

    function newton=nwtn(f)

    fp(a)=zeros(100);

    fp(a)=diff(f);

    then the loop.
     
    where in both cases, fp=f'.
     
  6. Dec 2, 2009 #5
    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
     
  7. Dec 2, 2009 #6
    If that is your function that you want to use then it'd be more easily done the first way I said,

    Code (Text):

    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!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Producing 100 iterations from Newton's method in matlab
Loading...