How to plot an iteration to run like a simulation while iteration is still ongoing?
14 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (1)
Cris LaPierre
le 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 commentaires
Sasthikapreeya D/O Ponnarasu
le 20 Mar 2020
Modifié(e) : Sasthikapreeya D/O Ponnarasu
le 20 Mar 2020
Cris LaPierre
le 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
Voir également
Catégories
En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!