Line plot in a loop

My code to plot the progress of my Newton's method algorithm vs. time currently looks like this:
f = @(x) % function
while % [not terminating condition]
% [Newton's method]
plot(t,log(abs(f(z))), '*-')
end
But of course, the '-' in the linespec doesn't do anything, since only a point, not a line, is being plotted in each iteration. How can I plot a solid line linking the last point plotted to the new one? I know I could store previous t and z and do plot([t_old t],[log(abs(f(z_old))) log(abs(f(z)))]) each time, but that is not ideal, especially since log(abs(f(z_old))) would need to be recalculated each time. There must be a smarter way, using a plot handle or line handle or some such. Can you help? Thank you.

 Accepted Answer

Jan
Jan on 23 Sep 2013

0 votes

You could simply store log(abs(F(z_old))) and append the new value only. This would be much cheaper than the repeated calculation of the logarithm.
You can obtain the formerly drawn data from the XData and YData of the line object. Then you have to store the handle replied by plot() only and obtain the formerly coordinates by get(LineH, 'XData') etc. But the difference to storing the data directly is small.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Tags

Asked:

on 23 Sep 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!