How can I plot each iteration of optimization function variables while retaining a fixed quantity of prior iterations on the same plot?

I am using fmincon to minimize f(x) and dynamically plotting x each time a new x is tried. In my case, x is basically an array of points in a 2D. My intent is to be able to observe the evolution of x throughout the process by plotting new x over the previous ones. Since the plot becomes uselessly cluttered after hundreds (or thousands) of iterations (and greatly slows down execution), I'm wondering if there is a way to dynamically discard (remove from the figure) iterations < n-k, where n is the current iteration, and k is some quantity of prior iterations that remain. I want to observe how the points move around the design space, each point having a trail of the last k iterations. I don't need to retain the values for each iterations, just want to see the progress as the code runs.
Here is an outline of how I have the code set up:
  • Main script which sets x0, bounds, and calls a function ObjConstr
  • ObjConstr does three things: (1) sets up the figure properties; (2) Calls fmincon using nested objective and constraint function exactly as described here; and (3) calls a functon PlotFun for each new x
  • PlotFun takes x, does some basic manipulations on it, then plots to the figure
It works fast, until the plot gets too cluttered and it slows to a crawl, so my idea is to only retain x0 and the last k iterations of x. I'm guessing it might be necessary to store x for the k iterations, and then only plot them together, wait until the next k iterations are obtained, then clear the plot and re-plot the new iterations. I could use a consistent color scheme for the iterations to make the sequence apparent. But that's not quite what I want. Is there an efficient way to simply drop the earliest retained iteration each time a new x is plotted, without storing the x values?
If this is impractical then I would appreciate suggestions/examples for alternate means of visualizing the progress. I've considered some type of histogram, but really would like to see the points in coordinate space. I am still somewhat novice user, so I may be missing something dead simple.

 Accepted Answer

I'm guessing it might be necessary to store x for the k iterations, and then only plot them together, wait until the next k iterations are obtained, then clear the plot and re-plot the new iterations.
Instead, you could store handles to the last k scatter plots and then delete the earliest one. Example,
plotIter=@() scatter(rand(5,1), rand(5,1),'filled'); %add the latest iteration to the plot
H(1)=plotIter();hold on
H(2)=plotIter();
H(3)=plotIter(); %The first k=3 plots
On every new iteration after the first k (here k=3), you delete H(1) and add a scatter plot for the latest x,
delete(H(1))
H=[H(2:end), plotIter()];

1 Comment

I've gotten it to work exactly as I wanted by adapting your answer. Thank you for the example.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products

Release

R2016b

Asked:

on 17 Dec 2021

Commented:

on 19 Dec 2021

Community Treasure Hunt

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

Start Hunting!