MATLAB Answers

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

3 views (last 30 days)
Sasthikapreeya D/O Ponnarasu
Commented: Cris LaPierre on 20 Mar 2020
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?

  0 Comments

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 19 Mar 2020
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

Sasthikapreeya D/O Ponnarasu
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.
Cris LaPierre
Cris LaPierre on 20 Mar 2020
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.

Sign in to answer this question.


Translated by