from BellsInequality.m by Bob Day
This code performs trials of a simulated Bell inequality experiment.

BellsInequality.m
%
% This program simulates a Bell's inequality experiment that uses a
% Stern-Gerlach type apparatus.  The entangled particles used in this type of 
% experiment would be spin 1/2 particles such as protons or electons.
%
% From "The New Physics for the Twenty-First Century" by Gordon Fraser(mostly
% quoted but I've made a few alterations):
% "There are many mathematical formulations of the resulting Bell inequality.
% The simplest was first formulated by Eugene Wigner.  For three arbitrary
% angles, A, B, and C, the following inequality must be satisfied: 
% N(uA,dB') <= N(uA,dC') + N(uB,uC').  Here, for example, N(uA,uB') means the
% number of cases when the "up" detector in the Stern-Gerlach apparatus of
% particle 1 (unprimed) oriented along direction A registers simultaneously with 
% the "up" detector of particle 2 (primed) with the Stern-Gerlach apparatus 
% oriented along direction B."
%
% In english this inequality says: "The number of men, uA, (as opposed to women) 
% who have green eyes, dB', (as opposed to blue eyes) is not greater than the 
% sum of the number of men who are tall, dC', (as opposed to short) plus the 
% number of people with green eyes, uB, who are short, dC'. (Note: the
% primed quantities are the complements of the corresponding unprimed
% quantities.  For example dB' (green eyes) == uB (green eyes).)
%
% Quantum mechanics predicts that the spin orientations of singlet state 
% (entangled) particle pairs measured at the two Stern-Gerlach detectors
% are correlated and depend on the angle |A - B| by which the orientations
% of the detectors differ: Pqm(uA,uB) = (sin(|A-B|/2))^2, and 
% Pqm(uA,dB) = 1-Pqm(uA,uB) (again adapted from "The New Physics for the 
% Twenty-First Century" by Gordon Fraser). ("P", here, is the probability of a 
% "spin up" detection, and "qm" is "according to quantum mechanics".)
% "Correlated" means that measuring a characteristic of one particle, say
% gender, influences the result of measuring another characteristic, say
% eye color, of the other particle.
%
% "Local realistic" theories predict that these probabilities are 
% Plr(uB given uA) = 1 - |A-B|/pi and Phv(dB given uA) = 1 - Plr(uB given uA),
% assuming a uniform random distribution of the spin orientation of the 
% particle. (The "lr" is "according to local realistic theories".)
% Local realistic theories say that the results of measurements of
% characteristics of the two particles are independent of one another -- that
% that measuring a characteristic of one particle, say gender, has no influence
% on the result of measuring another characteristic, say eye color, of the other
% particle.
%
% Local realistic theories predict that Bell's inequality will be obeyed,
% quantum mechanics predicts it will be violated.
%
% In this program, the angles are A (man) = 0 degrees, B (green eyes) = 30
% degrees, and C (tall) = 60 degrees.  Change them as you desire.
%

clear all;

                    % Initialize the set counts:
nMG = 0;            % The number of men with green eyes;
nMT = 0;            % The number of men who are tall;
nSG = 0;            % The number of short people with green eyes.

                    % Initialize the quantum spin state:
M = 1;              % Man as opposed to woman (spin up).
G = 1;              % Green eyes as opposed to blue eyes (spin up).
T = 1;              % Tall as opposed to short (spin up).

nTrials = 10000;    % Number of trials;
for nTrials = 1 : nTrials
                    % Generate a spin state.  Each state is either +1 or -1:
  M = M * (2*randi(2) - 3);
  G = G * (2*randi(2) - 3);
  T = T * (2*randi(2) - 3);
  
  A = [M G T];      % Create a proton.

  aax = randi(3); % Pick the axes we will measure.  (We can measure only
  bax = randi(3); % two spin components per trial.)
  
  if ( ((aax == 1) && (bax == 2)) || ((aax == 2) && (bax == 1)) )     
                                     % If gender and eye color |A-B| = 30 ...
    if (A(1) == 1)                   % If it's a man...
      P = (cos(15.0*(pi/180.0)))^2; % QM (quantum mechanics).
      % P = 1.0-(30.0/180.0); % LR (local realistic theories).
      if (rand <= P)                 % If he has green eyes,
        nMG = nMG + 1;               % increment the count.
      end
    end
    
  elseif ( ((aax == 1) && (bax == 3)) || ((aax == 3) && (bax == 1)) )
                                     % If gender and height |A-C| = 60 ...
    if (A(1) == 1)                   % If it's a man...
      P = (cos(30.0*(pi/180.0)))^2; % QM
      % P = 1.0-(60.0/180.0); % LR
      if (rand <= P)                 % If he is tall,
        nMT = nMT + 1;               % increment the count.
      end
    end
    
  elseif ( ((aax == 3) && (bax == 2)) || ((aax == 2) && (bax == 3)) )
                                     % If height and eye color |B-C| = 30 ...
    if (A(3) == -1)                  % If it's a short person...
      P = (sin(15.0*(pi/180.0)))^2; % QM (Note "sin" not "cos" here.)
      % P = 1.0 - (120/180.0); % LR
      if (rand <= P)                 % If the person has green eyes,
        nSG = nSG + 1;               % increment the count.
      end
    end
  end
end
  
nMG, nMT, nSG

Contact us at files@mathworks.com