% BRIEF RECAP of October 29th, 1998 NEEP271 lecture
% written by Fabian Waleffe 98/10/31
%
% NUMERICAL INTEGRATION: relationship between TR and Simpson:
%
% if TR(N) is the Trapezoidal rule with N segments (N+1 points) approximation
% to an integral and SR(N) is the Simpson's rule with the same N segments
% (i.e. 2N+1 points) approx to the same integral then
%
%                  [4 TR(2N) - TR(N)]/3 = SR(N)
%
% ===================================================================
%
% MONTE-CARLO INTEGRATION:
% useful to get rough approximation to integral over complex domains
%
% IDEA: enclose domain in simpler domain, throw darts at random, uniformly
% distributed over the simpler domain, count number of darts that fall
% inside complex domain. Then size (i.e. Volume, Area,...) of complex domain
% is approximately  M/N * size of simple domain, where N= total number
% of random points and M=number of points that fell inside complex domain.
%
% Example: integral from 0 to pi of sin(x) dx  = 2 (simple test case)
%
% => Take N random points (x_i, y_i) uniformly distributed in [0,pi] x [0,1]
%    then count how many points are below below  y = sin(x) 
%
% Very useful Matlab built-in function: find, see "help find"
% without "find" you'd need a loop to count the number of pts with sin(x) >y.
%

N=100; x=rand(N,1)*pi; y=rand(N,1); M=length(find(sin(x)-y>0)); M/N*pi

% Repeat several times
% for more reliable estimate: average over several tries or increase N.