The book has two different MATLAB versions of Newtons method
The book has two different MATLAB® versions of Newton\'s method. One is a program (on page 162) and the other is a function (page 170). The comments on page 162 indicate the algorithm does not work on zero roots (i.e., it does not work when the true root is equal to zero). This is the case because of the form of \"myrel\".
Create a variation on the function m-file (page 170) in which the zero-root restriction is removed by changing the definition of \"myrel.\" Specifically, do not divide by x; but leave the rest the same.
Then test your modified code on the MATLAB® pre-defined \"sin\" function (and remember your differential calculus: the derivative of sine is simply cosine).
Set the initial guess to pi/4. [You can still name the function \"newtfun_LastName.m\"].
Solution
Please find the modified program which can handle zero roots as well. Please note that f.m and df.m is there in your directory.
%% Modified Newton\'s method to consider zero roots as well
steps=0;
x=input(\'Initial Guess: \');
re=1e-8;
myrel=1;
while myrel>re & (steps<20)
xold=x;
x=x-f(x)/df(x);
steps=steps+1;
disp([x f(x)]);
if x~=0
myrel=abs((x-xold)/x);
else
myrel=re;
end
end
if myrel<=re
disp(\'Zero found at\')
disp(x);
else
disp(\'Zero NOT found\')
end
