"break" in for loop seems to not be working

40 visualizaciones (últimos 30 días)
JAKE SEIFERT
JAKE SEIFERT el 17 de Ag. de 2019
Comentada: JAKE SEIFERT el 18 de Ag. de 2019
I'm having an issue with a current bit of code I am working on (see below). It seems that the "break" in my for loop is not working. I must have an error somewhere but I cannot see it. Here is a rundown of the section of code:
  1. counter and relevant vectors are initialized
  2. start of for loop
  3. if statement that is used to revert back to previous loop data if a condition fails
  4. calculations
  5. end
My output is in the image below, note that all of the preload_sf and some of the sep_sf values should not be there, as the break will cause the for loop to convert current values to previous values that satisfy the conditions (user inputs), along with ending the loop. Instead, it seems that the break does nothing, and the loop cycles through all available preload values. So does anyone have an idea as to why my break command is not working for me in this context?
Hopefully this makes sense with only this small section of code. The script right now is around 400 lines, and I don't think most of that is necessary to understanding this problem.
preload_vector=0.50:0.01:0.90; %creating vector of preload values
length1=length(preload_vector); %determining the length of the vector for later use
if analysis == "Static" %implying static analysis
count2=0; %initializing second counter
%initializing safety factors
Ni=1:length1; %initializing
Nsep_calc=1000000:10000000; %arbitrary vector for initialization
Ny_calc=1000000:10000000; %arbitrary vector for initialization
for L=1:length1 %cycling through all available preload values until safety factors are met
%stopping secondary loop if safety factors are too low, one
if Ni(L)<1.0 || Nsep_calc(L)<Nsep || Ny_calc(L)<Ny %if one of these fails - then the loop must revert
Ni(L)=Ni(count2); %sets to value in last loop
Nsep_calc(L)=Nsep_calc(count2); %sets to value in last loop
Ny_calc(L)=Ny_calc(count2); %sets to value of previous loop
break
end
count2=count2+1; %used to recall values from of previous loop
preload=preload_vector(count2);
%Fastener Calculations
Fi=preload*ps*At; %calculating force on fastener due to preload for each grade
max_stress=(Pb+Fi)/At; %calculating stress due to preload and external loads for each grade
Ti=u*Fi*major_dia; %calculating torque to acheive preload from above
tau=(16*Ti)/(pi*((2*sqrt(Am/pi))^3)); %calculating shear stress from preload for each grade
eff=sqrt((Fi/At)^2+(3*tau^2)); %calculating effective stress from preload for each grade
Psep=Fi/(1-C); %load at which sep. occurs
%storing last value of Ni for each secondary loop iteration
Ni(L)=ts/eff; %calculating preload application safety factor
%calculating yield safety factor
Ny_calc(L)=ys/max_stress;
%calculating separation safety factor
Nsep_calc(L)=Psep/max_load;
end
%storing static analysis data
Ti_stored(i)=Ti;
preload_stored(i)=preload; %percent preload
Ny_calc_stored(i)=Ny_calc(L);
Nsep_calc_stored(i)=Nsep_calc(L);
Ni_stored(i)=Ni(L);
end

Respuesta aceptada

JAKE SEIFERT
JAKE SEIFERT el 17 de Ag. de 2019
So, after some sleep and more troubleshooting today I figured out a few things:
  1. The "break" command does not work if the criteria used to break the loop is a function of the loop index itself. I'm not sure why that is. What lead me to this conclusion is in the code below. When an arbitrary number was used in the conditional statement, the for loop broke as expected. Very interesting, as at some point in the above code, L at an iteration of the loop was equal to 6.
  2. For the second item I got lucky, since all of my calculations below my previous break statement were functions of the preload variable, which is a function of the counter, I was able to accomplish what I needed by storing only the counter variable for the entire loop sequence, and then when a condition was met for the current loop, reducing that counter by an increment of one and recalculating. Not the most elegant solution in my opinion (I wish my first bit of code would have worked), but the code still runs incredibly fast.
Thank you all for your help!
%1*****troubleshooting statement*****
if Ni(6)<1.0
break
end
%2*****updated code*****
for L=1:length1 %cycling through all available preload values until safety factors are met
%stopping secondary loop if preload safety factor is too low
%***bolts will break before Ny, Nf3, or Nsep is reached,
%those are filtered out at the end of the code
if Ni<1.0 && count2>2 %***if the count is too low there will be no integers for the next line
preload=preload_vector(count2-1); %reverses to the previous preload value that satisfied the above condition
%performing calculations with the acceptable preload
Fi=preload*ps*At; %calculating force on fastener due to preload for each grade
max_stress=(Pb+Fi)/At; %calculating stress due to preload and external loads for each grade
Ti=u*Fi*major_dia; %calculating torque to acheive preload from above
tau=(16*Ti)/(pi*((2*sqrt(Am/pi))^3)); %calculating shear stress from preload for each grade
eff=sqrt((Fi/At)^2+(3*tau^2)); %calculating effective stress from preload for each grade
Psep=Fi/(1-C); %load at which sep. occurs
%storing last value of Ni for each secondary loop iteration
Ni=ts/eff; %calculating preload application safety factor
%calculating yield safety factor
Ny_calc=ys/max_stress;
%calculating separation safety factor
Nsep_calc=Psep/max_load;
break
end
%****************************************************************************************************************************************
count2=count2+1;
preload=preload_vector(count2);
%Fastener Calculations
Fi=preload*ps*At; %calculating force on fastener due to preload for each grade
max_stress=(Pb+Fi)/At; %calculating stress due to preload and external loads for each grade
Ti=u*Fi*major_dia; %calculating torque to acheive preload from above
tau=(16*Ti)/(pi*((2*sqrt(Am/pi))^3)); %calculating shear stress from preload for each grade
eff=sqrt((Fi/At)^2+(3*tau^2)); %calculating effective stress from preload for each grade
Psep=Fi/(1-C); %load at which sep. occurs
%storing last value of Ni for each secondary loop iteration
Ni=ts/eff; %calculating preload application safety factor
%calculating yield safety factor
Ny_calc=ys/max_stress;
%calculating separation safety factor
Nsep_calc=Psep/max_load;
end
%storing static analysis data
Ti_stored(i)=Ti;
preload_stored(i)=preload; %percent preload
Ny_calc_stored(i)=Ny_calc;
Nsep_calc_stored(i)=Nsep_calc;
Ni_stored(i)=Ni;
end
  2 comentarios
Walter Roberson
Walter Roberson el 18 de Ag. de 2019
It would help if we had the complete program to test with.
>> for i = 1 : 100; if i^2 - 2*i + 5 > 17; break; end; end
>> i
i =
5
Looks like functions work.
JAKE SEIFERT
JAKE SEIFERT el 18 de Ag. de 2019
I have the program working flawlessly now with the corrections that I mention above.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Stress and Strain en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by