Returning to a previous iteration when a flag appears.

3 vues (au cours des 30 derniers jours)
Kieran Reeves
Kieran Reeves le 27 Fév 2020
Modifié(e) : Kieran Reeves le 28 Fév 2020
Hello
I have recently started using the flag feature in Matlab. I have a code that works through specific vehicle dynamic optimisation during a lap of a circuit. The full lap is broken into sections of i = 1:361.
Due to the optimisation in some cases of a lap the result is out of the bounds of the vehicle constraints and is to be ignored and therefore I am using flag. For instance:
flag = [];
if (VAfcnD(vx(i+1)) ~=0 || VAfcn(vx(i+1)) ~=0 || imag(CombSlipLimD(i+1))~=0 || imag(CombSlipLim(i+1)) ~=0)
flag=[flag, i+1];
end
Some laps run perfect and some have a flag. So I want to run several laps and discount the flagged laps.
The problem lies in that when an error occurs (so lets say at i=15), the code adds a flag to i=15, which is perfect, but then continues with i=16 making the lap invalid and causing an error. Is there a way to add in some code that allows the iteration to run again if flag is not empty (so in this case re-run i = 15), or to clear the lap and start again at i=1 until a lap is completed without a flag.
Thankyou
Kieran
  4 commentaires
Adam
Adam le 27 Fév 2020
Something like this should work:
for i = 1:361
flag = true;
while flag
Downforce(i+1) = Flift * (vx(i+1)^2);
Cfriction(i+1) = Cfric + mu * Downforce(i+1);
k(i+1) = Cfriction(i+1)/(vx(i+1)^2);
Delta(i+1) = -wb * k(i+1) * 0.0175;
CombSlipLimD(i+1) = VAfcnD(vx(i+1)) * sqrt(1-(LatAy(i+1)/VAfcnAy(vx(i+1)))^2);
ax(i+1) = min(VAfcnD(vx(i+1)),CombSlipLimD(i+1));
flag = (VAfcnD(vx(i+1)) ~=0 || VAfcn(vx(i+1)) ~=0 || imag(CombSlipLimD(i+1))~=0 || imag(CombSlipLim(i+1)) ~=0)
end
end
It won't guarantee a non-infinite loop though as that will be determined by whether something is guaranteed to change at some point when it re-runs that loop that causes the flag to return false (i.e. no problem). If nothing changes it will always return true, or if the calculation always results in a problem then in both those cases it will be an infinite loop because essentially the loop is
while true
...
end
which is a theoretically infinite loop. It relies on whatever code is inside there to always have an exit strategy (either a break command or, in this case our true is a variable so when this is set to false it will end) in order to not be infinite in a practical case.
Kieran Reeves
Kieran Reeves le 27 Fév 2020
Modifié(e) : Kieran Reeves le 28 Fév 2020
Thanks Adam
I'll have a play around with the loop and set an exit strategy if needed. Really appreciate your assistance.
Kieran

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Graphics Performance dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by