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

AI Thread Summary
The discussion focuses on implementing Newton's method in MATLAB to find the fifth root of a number P by solving the equation x^5 - P = 0. Users are advised to start with an initial estimate and iteratively refine it using the formula xi+1 = xi - ((xi^5 - P) / (5 * xi^4)). There is confusion regarding the use of loops, with suggestions to incorporate the error condition directly in the while loop. Participants also highlight issues with code compilation and the need for proper variable initialization and updates within the loop. Ultimately, the goal is to achieve a solution with an estimated relative error smaller than 0.00001.
pags920
Messages
20
Reaction score
0

Homework Statement


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.


Homework Equations





The Attempt at a Solution



Code:
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.
 
Physics news on Phys.org
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:
% 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:
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
 
Back
Top