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:
- counter and relevant vectors are initialized
- start of for loop
- if statement that is used to revert back to previous loop data if a condition fails
- calculations
- 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
Best Answer