MATLAB: Newton’s method to determine roots / I’m not sure if what I got so far is correct

newton’s method to determine roots

>> x = 3;
Tol = 0.001;
count = 0;
dx=1; %this is a fake value so that the while loop will execute
f=-11; % because f(3)=-11
fprintf('step x dx f(x)\n')
fprintf('---- ----------- --------- ----------\n')
fprintf('%3i %12.8f %12.8f %12.8f\n',count,x,dx,f)
xVec=x;fVec=f;
while (dx > Tol || abs(f)>Tol) %note that dx and f need to be defined for this statement to proceed
count = count + 1;
fprime = 5*x^4-20*x^3+6*x^2+28*x;
xnew = x - (f/fprime); % compute the new value of x
dx=abs(x-xnew); % compute how much x has changed since last step
x = xnew;
f = x^5-5*x^4+2*x^3+14*x^2-29; % compute the new value of f(x)
fprintf('%3i %12.8f %12.8f %12.8f\n',count,x,dx,f)
end
step x dx f(x)
---- ----------- --------- ----------
0 3.00000000 1.00000000 -11.00000000
1 6.66666667 3.66666667 4477.99588477
2 5.64985329 1.01681337 1440.74956648
3 4.86575495 0.78409835 457.60831319
4 4.27678772 0.58896722 141.56939561
5 3.85764079 0.41914694 41.17150694
6 3.59446346 0.26317733 10.13724563
7 3.47377942 0.12068404 1.53513578
8 3.44792517 0.02585424 0.06059241
9 3.44681794 0.00110723 0.00010774
10 3.44681597 0.00000198 0.00000000
>>

Best Answer

You can compare your results with the result obtained from the ROOTS command. If they agree, it will be a good sign that you've done it right.