I needed to write a Newton method code on Matlab and processed through here. But when I try to use it, it gives this error after calculating a few times:
Attempted to access df(8); index out of bounds because numel(df)=1.
Error in newtonmethod (line 11) tz=ti-(f(ti)/df(ti));
function newtonmethod(f)
ti = 10;
tz = 8;
abstol = 0.0001;
counter = 0;
h=0.1;
df=((f(ti+h)-f(ti))/h);
while (abs(ti-tz)>abstol)
ti=tz;
tz=ti-(f(ti)/df(ti));
counter=counter+1;
end
ti=tz;
fprintf(tz,'counter=',counter )
end
What should I do?