% {Two Plate Assembly Simulation}
% In assembly of two square $4 \times 4$ steel plates, comprising a part of a medical device,
% each plate is drilled a hole in its center. The plates are
% to be joined by a pin.
% Assembling machine adjusts the plates wrt lower left corner denoted as (0,0)
% in coordinate system {\it xOy}.
% 
% The coordinates of hole centers $X_i$ and $Y_i$ for $i$th plate ($i =1, 2$)
% are independent normally distributed random variables
% with mean 2 and standard deviation 0.001.
% 
% The hole diameters $D_1$ and $D_2$ are normally distributed
% with mean of 0.2 and standard deviation 0.0012, for both plates.
% The pin diameter, $R,$ is also normally distributed with mean 0.195 and standard deviation of
% 0.0005.
% 
% (a) What proportion of pins will go through assembled plates?
% Base your answer on 1,000,000 simulated assembles using MATLAB.
% {\sl Hint.}  The clearance
% \ba
% \min\{D_1, D_2\} - \sqrt{ (X_1 - X_2)^2 + (Y_1 - Y_2)^2 } - R,
% \ea
%   has to be positive for a successful assembly. Why the second term is needed?
% 
% 
% (b) In an assembled pair of plates the pin will wobble if it is too loose. This wobbling will
% occur if
% \ba
% \min\{D_1, D_2\}  - R \geq 0.006.
% \ea
% What fraction of assembled plates would not wobble?
% {\sl Hint:} This is conditional probability, since we restrict
% attention on assembled plates only. Thus in simulating this
% proportion we ignore the cases when the assembly is not possible.
% 
% \newpage
% {\footnotesize
% \begin{verbatim}
%  Normal Probabilities by Simulation.
rng(10,'twister')
M=1000000 ; %number of simulations
  clear = 0;
  clearnowobb=0;
for i = 1:M
    X1 = 2 + 0.001  * randn;
    Y1 = 2 + 0.001  * randn;
    X2 = 2 + 0.001  * randn;
    Y2 = 2 + 0.001  * randn;
    C=sqrt((X1-X2)^2 + (Y1-Y2)^2);
    D1=0.2+0.0012*randn;
    D2=0.2+0.0012*randn;
    D = min(D1, D2);
    R = 0.195 + 0.0005*randn;
    clear = clear + (D-C-R > 0);
    clearnowobb = clearnowobb + (D-C-R > 0)*(D-R<0.006);
end
  p1=clear/M  %(a) 0.9555
  p2 = clearnowobb/clear %(b) 0.9350

% \vspace*{0.3in}
% {\bf References}
% 
% Adapted from:
% 
% Banks, J., Carson, J. S. II, and Nelson, B. (1984)
% {\it Discrete Event System Simulation},
% Second Edition, Prentice Hall, Upper Saddle river, NJ.

