MATLAB: Newton method to find polinom roots

newton

Hi!
I wrote Newton method in matlab to find the polinom roots but I didn't get it which the answer write.
p = [ 1 -1 -6 0]
x0 = 2;
So this is my code:
function [pd,X] = newtonMethod(p,x0)
n = length(p);
pd = polyder(p)
result = [];
for i = 1:8
fx(i) = polyval(p,x0);
fdx(i) = polyval(pd,x0);
x(i) = x0 - (fx/fdx);
result = [result; x(i)];
x0 = x(i);
end
result
end
And i get this:
6.0000
4.4028
2.9345
1.4956
0.0557
-1.3792
-2.8141
-4.1472
But the answer:
6.0000
4.4000
3.4891
3.0912
3.0041
3.0000
3.0000
3.0000

Best Answer

p = [ 1 -1 -6 0]
x0 = 2;
n = length(p);
pd = polyder(p)
result = [];
for i = 1:8
fx = polyval(p,x0);
fdx = polyval(pd,x0);
x0 = x0 - (fx/fdx);
result = [result; x0];
end
result