How to plot an iteration to run like a simulation while iteration is still ongoing?

if k = T > 0
if MaxTime > elaspedtime
Cur = 10;
Best = Cur;
ECur = exp(Cur);
EBest = exp(Best);
tic;
for subit = 1:Maxsubiterations
New = randi([1 100000]);
ENew = exp(New);
CE = ENew - ECur;
if CE < 0
Cur = New;
if ENew < EBest
Best = New;
end
else
P = exp(-CE/T)
R = randi([0 1]);
if R < P
Cur = New;
end
T = alpha*T
if T<=0
break
end
end
end
toc;
elaspedtime = toc;
else
break
end
end
How can i plot the variable Best for each iteration like a simulation as the iteration runs?

Answers (1)

You have one too many ends in your code.
Not sure at what point you want to plot Best, and against what other variable, so here's a simple example that might help.
X=0;
Y=cos(X);
while X<2*pi
X = X+.1;
Y = cos(X);
plot(X,Y,'o')
hold on
end
hold off

2 Comments

hi, i've tried your code and i actually meant for each iteration to plot one one by one like a simulation? yes I notice the extra ends. I have edited the code.
I'm afraid I don't understand what you mean by "plotting like a simulation".
The code I shared adds a new data series to the same plot for each iteration. That data series can be a single point, or a line. It just depends on what data you pass to X and Y.
If you want a new figure for each iteration, this code could do that.
X=0;
Y=cos(X);
while X<2*pi
X = X+.1;
Y = cos(X);
figure
plot(X,Y,'o')
end

Sign in to comment.

Categories

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

Community Treasure Hunt

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

Start Hunting!