Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: MATLAB: Finding the 5th Root using Newton's Method

  1. May 26, 2012 #1
    1. The problem statement, all variables and given/known data
    The solution of the nonlinear equation x^5-P=0 gives the fifth root of the number
    P. A numerical solution of the equation can be calculated with Newton’s
    method. The solution process starts by choosing a value x1 as a first estimate of
    the solution. Using this value, a second, more accurate solution x2 can be calculated
    with x2=x1-((x1^5-P)/(5*x1^4)) , which is then used for calculating a third, still more
    accurate solution x3, and so on. The general equation for calculating the value of
    the solution xi+1 from the solution x1 is xi+1 = xi -((xi^5-P)/(5*xi^4)) . Write a userdefined
    function that calculates the fifth root of a number. For function name and
    arguments use y=fifthroot(P), where the input argument P is the number
    whose fifth root is to be determined, and the output argument y is the value (5th sqrt(P)) .
    In the program use x=P for the first estimate of the solution. Then, by using the
    general equation in a loop, calculate new, more accurate solutions. Stop the looping
    when the estimated relative error E defined by is smaller than
    0.00001.


    2. Relevant equations



    3. The attempt at a solution

    Code (Text):
    function [y] = fifthroot(P)
    format short
    x=P;
    xi=1;
    i=5;
    while n==1:i
        xi=xi-((xi^5-P)/(5*xi)^4);
        E=abs((xi-x)/x);
        if E<0.00001
            break
        end
        disp(x)
    end
    This was my previous attempt. I am not sure whether a for loop or a while loop is the best to use for this situation. My problem is trying to use a previous answer in computing the next answer. Not quite sure how to retain a previous answer for using in the next one.
    1. The problem statement, all variables and given/known data



    2. Relevant equations



    3. The attempt at a solution
     
  2. jcsd
  3. May 26, 2012 #2
    Your code doesn't even compile. What in the world does while n == 1:i mean? You should instead put the E term in the while statement alongside the 'break' condition. Here is a nice skeleton code I have written

    Code (Text):

    % Define parameters
    endCondition = 0.00001;

    E = inf; % Initialize such that while loop begins
    while E > endCondition
        % code to compute new estimate
        % Update E (for use in the while loop compare)
    end
     
    Last edited: May 26, 2012
  4. Apr 4, 2014 #3
    i have tried to apply what the both of you said but it is not working for me. Here is what i did. Please let me know what i did wrong.
    function [y]= fifthroot (P)
    x=p
    xi=1
    i=5
    endcondition=0.00001;
    xi=xi-((xi^5-P)/(5*xi)^4);
    E=abs((xi-x)/x);
    if E<0.00001
    break
    while E > endcondition %compute new estimate
    update E (for use in the while loop compare)
    end
    end
    disp (x)
    end
    i would really appreciate it
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted