# Sensitivity Analysis and Filtering of Machinable Parts Using Density-Based Topology Optimization
## Appendix A Appendix A Appendix A.1. Machinability MATLAB(r) Code
Machinability MATLAB(r) Code  
```
function[machnmbr,grayflag,coreflag,machflag,neigflag,periflag,eroflag]= machnumber(xPhys,nclx,nely,nelz,th) machflag=zeros(size(xPhys));%Ifmachflag=1 meansthere'saccesibility coreflag=zeros(size(xPhys));%Ifcoreflag=1 meansitsnotfrontier neigflag=zeros(size(xPhys));%Ifneigflag=1 meansaccessthroughneigbourhood periflag=zeros(size(xPhys));%Ifperiflag=1 meansitstheperimeteroftheinitialvolume eroflag=zeros(size(xPhys));%Ifcroflag=1 meansitsremovablesolid fork1=2:(nelz-1) forj=2:(nelz-1) forj=2:(nelz-1) ifxPhys(j1,i1,k1)>=0.5 %Countnonfrontierelements ifall(xPhys(j1,i1,(k1-1:k1+1))>=th)&all(xPhys(j1,(i1-1:i1+1),k1)>=th) all(xPhys((j1-1:j1+1),i1,k1)>=th)  coreflag(j1,i1,k1)=1; end %Accessxdirection ifmachflag(j1,i1,k1)==0&&(all(xPhys(j1,1:i1-1,k1)<th)||all(xPhys(j1,i1+:nelx,k1)<th)) machflag(j1,i1,k1)=1; end %Accessthroughneighbourxdirection if(all(xPhys(j1+1,1:i1,k1+1)<=th)||all(xPhys(j1-1,1:i1,k1+1)<=th)||... all(xPhys(j1+1,1:i1,k1-1)<=th)||all(xPhys(j1-1,1:i1,k1-1)<=th)||... all(xPhys(j1+1,i1:nelx,k1+1)<=th)||all(xPhys(j1-1,i1:nelx,k1+1)<=th)||... all(xPhys(j1+1,i1:nelx,k1-1)<=th)||all(xPhys(j1-1,i1:nelx,k1-1)<=th))... &&machflag(j1,i1,k1)==0 neigflag(j1,i1,k1)=1; end end end end end end forkl=1:nelz fori1=1:nelz forj1=1:nely forj1=1:nely forj1=1:nely forj1=1:nely forj1=1:nely forj1=1:nely forj1=1:nely forj1=1:nely forj1=1:nely forj1=1:nelz if(i1==1|i==nelx|j1==1|j1==nely|k1==1|k1==nelz)&xPhys(j1,i1,k1)>=th& machflag(j1,i1,k1)==0&neigflag(j1,i1,k1)==0&coreflag(j1,i1,k1)==0&coreflag(j1,i1,k1)==0  periflag(j1,i1,k1)=1; end end end end %Percentajeofaccesibleelements machnmbr=((nmz(machflag(:))+nmz(neigflag(:)))*100)/(sum(xPhys(:)>=th)-sum(coreflag(:))-nmz(periflag(:))); thmat=zeros(size(xPhys)); thmat(find(xPhys>=th))=1; grayflag=thmat-(machflag+coreflag+neigflag+periflag);%Flagforfrontierelements end
### Filter Radii MATLAB(r) Code
``` functionrfil=machradiix(nele,melx,nely,nelz,xTilde,j1,i1,k1,th) %RADIUSINXDIRECTION racesflag=0;raccnflag=0;racceflag=0;raccwflag=0;races=0;racc=0;raccw=0;raccw=0;racgsflag=0;rneigmflag=0;meigeflag=0;rneigwflag=0;rneigsm=0;rneigsm=0;rneigsm=0;rneigsm=0;rneigsm=0;%RADIUSACCESSINXDIRECTION %xsouthdirection radpos=0;j2=j1-1; ifxTilde(j2,i1,k1)==th races=NaN; else whileraccsflag==0 ifj2>1&all(xTilde(j2,(1:i1-1),k1)<th)==1 racesflag=1; elseifj2>=1 radpos=radpos+1; end races=j2-j1; if(j2>1&xTilde(j2,i1,k1)<th)&raccsflag==0,j2=j2-1;elseraccsflag=1; end end %xnorthdirection radpos=0;j2=j1+1; ifxTilde(j2,i1,k1)>=th racc=NaN; else whileraccnflag==0 ifj2<=nely&all(xTilde(j2,(1:i1-1),k1)<th)==1 raccnflag=1; elseifj2<=nely radpos=radpos+1; end racc=j2-j1; if(j2<nely&xTilde(j2,i1,k1)<th)&raccnflag==0,j2=j2+1;elseraccnflag=1; end %xeastdirection radpos=0;k2=k1-1; ifxTilde(j1,i1,k2)>=th racec=NaN; else whileracceflag==0 ifk2>=1&all(xTilde(j1,(1:i1-1),k2)<th)==1 racceflag=1; elseifk2>=1 radpos=radpos+1; end racec=k2-k1;if (k2>1 & xTilde(j1,i1,k2)<th) & racceflag==0, k2=k2-1; else racceflag=1; end end end  
% x west direction  radpos=0; k2=k1+1;  if xTilde(j1,i1,k2)>=th  raccw=NaN; else  while raccwflag==0  if k2<=nelz & all(xTilde(j1,(1:i1-1),k2)<th)==1  raccwflag=1;  elseif k2<=nelz  radpos=radpos+1;  end  raccw=k2-k1;  if (k2<nelz & xTilde(j1,i1,k2)<th) & raccwflag==0, k2=k2+1; else raccwflag=1; end end end  
% RADIUS NEIGHBOUHROD IN X DIRECTION  
% x south direction  radpos=0; j2=j1-1;  if xTilde(j2,i1,k1)>=th  rneigs=NaN; else  while rneigsflag==0  if j2>=1 & all(xTilde(j2,(1:i1),k1)<th)==1  rneigsflag=1;  elseif j2>=1  radpos=radpos+1;  end  rneigs=j2-j1;  if (j2>1 & xTilde(j2,i1,k1)<th) & rneigsflag==0, j2=j2-1; else rneigsflag=1; end end  
% x north direction  radpos=0; j2=j1+1;  if xTilde(j2,i1,k1)>=th  rneig=NaN; else  while rneignflag==0  if j2<=nely & all(xTilde(j2,(1:i1),k1)<th)==1  rneignflag=1;  elseif j2<=nely & radpos=radpos+1;  end  rneig=j2-j1;  if (j2<nely & xTilde(j2,i1,k1)<th) & rneignflag=0, j2=j2+1; else rneignflag=1; end end end % x east direction  radpos=0; k2=k1-1;  if xTilde(j1,i1,k2)>=th  rneige=NaN;  else  while rneigeflag==0  if k2>=1 & all(xTilde(j1,(1:i1),k2)<th)==1  rneigeflag=1;  elseif k2>=1  radpos=radpos+1;  end  rneige=k2-k1;  if (k2>1 & xTilde(j1,i1,k2)<th) & rneigeflag=0, k2=k2-1; else rneigeflag=1; end  end  
% x west direction  radpos=0; k2=k1+1;  if xTilde(j1,i1,k2)>=th  rneigw=NaN;  else  while rneigwflag==0  if k2<=melz & all(xTilde(j1,(1:i1),k2)<th)==1  rneigwflag-1;  elseif k2<=melz  radpos=radpos+1;  end  rneigw=k2-k1;  if (k2>nelz & xTilde(j1,i1,k2)<th) & rneigwflag=0, k2-k2+1; else rneigwflag=1; end end  
fill=[raccs raccn racec raccw meiosis rneig rneige rneigw];  minValue = min(abs(fil()));  minIndex=find(abs(fil)==minValue);  if mean(size(minIndex))>1  pref1=fil(minIndex);  rfil=pref1(1);  elseif isnan(minValue)  rfil=0;  else  rfil=fil(minIndex);  end end