So, after some sleep and more troubleshooting today I figured out a few things:
- 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.
- 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