- In the above code, you haven't stored the "c" values anywhere and you are trying to access the "c" values in "fprintf('%d \t %20f \t %20f\n',i ,fnc(c(i)),fp(c(i)))".
- In your code, "c" is just a variable but you are trying to access it as an array.
- Below is the modified code where i am storing the "c" values (which is "x" values) in "x_values".
function approximation = newtonsMethod( fnc, x0, delta )
maxnumIter = 1000;
n = 0; % Initialize iteration counter.
syms fp; % derivative
fp = diff(fnc);
c = x0;
x_values=[x0];
while ( (abs(subs(fnc,c))>delta) && (n < maxnumIter) )
c = double(c - subs(fnc,c)/subs(fp,c));
x_values=[x_values c];
n = n+1;
end
if (n<100)
c(end);
disp('num_iter f(x) fprime(x) ')
disp('________________________________________________________________________________________')
for i=1:n
fprintf('%d \t %20f \t %20f\n',i ,fnc (x_values(i)),fp (x_values(i)))
end
end
if( abs(subs(fnc,c))> delta)
disp(['Warning: Tolerance not met after ' num2str(maxnumIter) ' iterations.']);
end
approximation = c;
end