Returning to a previous iteration when a flag appears.

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 Comments

You could just wrap the relevant section of code up in a
while ~isempty( flag )
...
end
within what I assume is a for loop, to re-run a single iteration until it has no flag. I'm not sure how that fits with the code you posted though, which seems to be concatenating flags. The while loop would be infinite if your code is concatenating flags rather than having some guaranteed mechanism to empty the flag variable during an iteration.
Hi Adam
Thankyou for the answer.
Yes sorry the code sits within a loop of 361 sections of the circuit for i = 1:361
Runs through each section calculating accelerations, speeds, steering etc, then has the flag. It is also split into left and right turns and acceleration or braking zones, which are then optimised outside of the function.
I was storing all data just to see how the flag was working and to see which section created the flag. Would it be easier to just create the flag when required inside the while loop, therfore if a flag is triggered it would start again?
How would it best do do this in my script to avoid an infinite loop?
An example of my current script:
for i = 1:361
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));
end
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
end
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.
Thanks Adam
I'll have a play around with the loop and set an exit strategy if needed. Really appreciate your assistance.
Kieran

Sign in to comment.

Answers (0)

Categories

Tags

Asked:

on 27 Feb 2020

Edited:

on 28 Feb 2020

Community Treasure Hunt

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

Start Hunting!