% "Bridge" Connnection in a Circuit
clear all

  disp('Bridge Connection Exercise')
  lw = 2; 
  set(0, 'DefaultAxesFontSize', 16);
  fs = 15;
  rand('twister',0)
  msize = 10;
%
%Theoretical result: P(S)=0.06264
M=100000000;
st = 0;
tic
for i = 1:M
e1 = rand < 0.3; e2 = rand < 0.8; e3 = rand < 0.2;
e4 = rand < 0.2; e5 = rand < 0.5; e6 = rand < 0.6;
e7 = rand < 0.4;
%===============
s1 = min(max(e2, e4),max(e3, e5));
s2 = max(min(e2,e3),min(e4,e5));  
s = e1 * (s1*e7 + s2*(1-e7)) * e6;
st = st + s;
end
toc
works = st/M;
fails = 1 - works;
disp(['The system works w/p ', num2str(works,5)]);
disp(['The system fails w/p ', num2str(fails,5)]);