
% Circuit is given as
%  S = e1  int [ (e1 int e3) uni (e4 int (e5 uni e6)) int e7,
% where "int" is intersection and "uni" is the union,
%  Probabilities of work for the elements e1-e7 are
% 0.9, 0.5, 0.3, 0.1, 0.4, 0.5, 0.8, respectively.
% Approximate the probability that the system S is operational.

  disp('Circuit Problem')
  lw = 2; 
  set(0, 'DefaultAxesFontSize', 16);
  fs = 15;
  rand('twister',1)
  msize = 10;
%
M=1000000;
st = 0;
%
for i = 1:M
e1 = rand < 0.9; e2 = rand < 0.5; e3 = rand < 0.3;
e4 = rand < 0.1; e5 = rand < 0.4; e6 = rand < 0.5;
e7 = rand < 0.8;
%===============
% 
% s1 = e2*e3;
% s2 = e5+e6 > 0;  
% s3 = e4*s2;
% s4 = s1+s3>0;
%  s = e1*s4*e7; %If the circuit works s=1, else s=0
 
% Alternatively...
s1 = min(e2,e3);
s2 = max(e5,e6);  
s3 = min(e4,s2);
s4 = max(s1,s3);
s = min([e1;s4;e7]);

st = st + s;
end
works = st/M; % exact result is 0.15084
fails = 1 - works;

disp(['The system works w/p ', num2str(works,5)]);
disp(['The system fails w/p ', num2str(fails,5)]);

