MATLAB: How to join markers in a semilogy plot generated from a for loop

linesplot

I searched for the existing questions and answers on the community, but it doesn't seem to work and I only get the markers plotted but they are not joined by a line.
I am using Matlab R2019b on Mac OS Catalina.
Below is the code. Everything is working except that the markers are not joined by the line.
clear all % clear all variables
close all % close all figures
clc
xr = pi; % initial right boundary
xl = 0; % initial left boundary
fprintf('j \t xc \t fc \n')
for j = 1:1000 % j is the number of iterations on the interval
xc = (xl+xr)/2; % calculate the midpoint
fc = exp(xc) - tan(xc); % calculate function (e^x - tan(x))
if fc>0
xl = xc;
else
xr = xc;
end
if abs(fc) < 10^(-12)
break
end
if j>1
semilogy(j,abs(fc),'b*--')
hold on
drawnow
end
fprintf('%d \t %.4f \t %e \n',j,xc,fc)
end
fprintf('-----------\n')
fprintf('The value of root is: %.4f \n',xc) % print value of root
fprintf('The error of the iterative procedure after %d iterations is: %e \n',j,fc) % print value of function

Best Answer

clf
xr = pi; % initial right boundary
xl = 0; % initial left boundary
fprintf('j \t xc \t fc \n')
semilogy(nan, nan);
ax = gca;
h = animatedline(ax, 'Marker','*');
for j = 1:1000 % j is the number of iterations on the interval
xc = (xl+xr)/2; % calculate the midpoint
fc = exp(xc) - tan(xc); % calculate function (e^x - tan(x))
if fc>0
xl = xc;
else
xr = xc;
end
if abs(fc) < 10^(-12)
break
end
if j>1
addpoints(h,j,abs(fc));
drawnow
end
fprintf('%d \t %.4f \t %e \n',j,xc,fc)
end
fprintf('-----------\n')
fprintf('The value of root is: %.4f \n',xc) % print value of root
fprintf('The error of the iterative procedure after %d iterations is: %e \n',j,fc) % print value of function
Related Question