5th root with Newton-Raphson code

4 Ansichten (letzte 30 Tage)
GEORGIOS BEKAS
GEORGIOS BEKAS am 11 Mär. 2018
Bearbeitet: John D'Errico am 11 Mär. 2018
This is a code I have created to calculate numerically the 5th root. Something is wrong.
x = 0.05;
x_old = 500;
iter = 0;
while abs(x_old-x) > 10^-3 && x ~= 0
x_old = x;
x = x - (5*x*x*x*x);
iter = iter + 1;
end

Akzeptierte Antwort

GEORGIOS BEKAS
GEORGIOS BEKAS am 11 Mär. 2018
If n is the number whose 5th root has to be calculated:
function f = fifth_root(n)
xold=10;
for i = 1:100
xnew = xold - (xold*xold*xold*xold*xold-n)/(5*xold*xold*xold*xold);
xold=xnew;
end
f=xnew
end
  1 Kommentar
John D'Errico
John D'Errico am 11 Mär. 2018
Bearbeitet: John D'Errico am 11 Mär. 2018
Yes. That will work. And very good of you to accept your own answer when I spent the time to find the bug in your code and explain how to fixit.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

John D'Errico
John D'Errico am 11 Mär. 2018
Bearbeitet: John D'Errico am 11 Mär. 2018
What is wrong? I'd start by pointing out that this is NOT Newton's method as you wrote it.
https://en.wikipedia.org/wiki/Newton%27s_method
Newton (or Newton/Raphson if you wish) has
xnew = xold - f(xold)/f'(xold)
So, if f(x) = x^5 - y, then what should you have written there? So, y is the value you want to compute a root of. Clearly, the 5th root of y will produce zero in f, as I wrote it.
So now you need to write the Newton iteration to use fas I wrote it, as well as the derivative of f.
  1 Kommentar
GEORGIOS BEKAS
GEORGIOS BEKAS am 11 Mär. 2018
Bearbeitet: GEORGIOS BEKAS am 11 Mär. 2018
while abs(xold-xnew)<10^-3
xnew = xold - (xold*xold*xold*xold*xold)/(5*xold*xold*xold*xold)
xold=xnew
end

Melden Sie sich an, um zu kommentieren.

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by