While and If loop with stopping criteria
Afficher commentaires plus anciens
I am trying to write a while loop with a stopping criteria, but I have difficulties in implementing this. So, within the loop I have to execute a function to calculate a structure array out. After that I have to find an error by comparing field from structure array out and that is the present value BestSol_Iter with the previous one BestSol.Cost. If the error is greater than 1e-6 then I have to execute the function again and evaluate the error Error = BestSol.Cost - BestSol_Iter. My stopping criteria is Tol= 1e-6.
I have started like this:
iter = 0; %Starting iteration
Tol = 1e-6; %Tolerance criteria for best cost
Error = 1; %Starting initial error
%Call the PSO function to calculate out structure
out = PSO_EBW_SD(problem, params);
BestSol = out.BestSol;
BestCosts = out.BestCosts;
particle = out.pop;
%START THE WHILE LOOP
while (Error > Tol)
BestSol_Iter = BestSol.Cost;
%Run the PSO function to calculate out structure
out = PSO_EBW_SD(problem, params);
%Print the out
BestSol = out.BestSol;
BestCosts = out.BestCosts;
particle = out.pop;
%Calculate the error
Error = BestSol.Cost - BestSol_Iter;
if Error <= Tol
break
if Error > Tol
continue
end
end
end
At the end I want to have the info about numbers of iterations necessary to satisfy the stopping criteria Tol=1e-6. Also I want to save the out structure for every iteration.
Tips and suggestions are very welcome.
Many thanks!
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!