Your loop should already use the previous y0
every consecutive time it operates.
If you want to display the y0
and the number of iterations in the Matlab command window, you first need to change the newton
-function to return not just y0
but also the number of iterations, iteraties
:
function [nulpunt, iteraties]=newton(x,y0)
Also, your function should actually return the final y0
as nulpunt
:
if iteraties==100;
fprintf('Maximaal aantal iteraties bereikt')
nulpunt = 0; % in this case, no solution was found, return the baseline value;
else
fprintf('De benadering van het nulpunt y*(%.2f) is %.4f gevonden in %d iteraties.\n',x,y0,iteraties)
nulpunt = y0;
end
to display the number of iterations and y0
after each execution of newton
, add these two lines after you call the function:
[y0, iteraties] = newton(x(i),y0);
disp(['This point was found: ',num2str(y0)])
disp(['It took ',num2str(iteraties),' iterations.'])
This may actually be redundant, as your function already outputs which y0
it found and how long that took.