Skip to content

Commit f27dcc5

Browse files
committed
Add Monte Carlo integration and update README
This commit adds Monte Carlo integration via `sxm_monte_carlo`. This commit also updates the README.
1 parent 877e5c2 commit f27dcc5

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

README.md

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
11
# Sane Extensions to MATLAB #
22
---
33

4-
**S**ane **E**xtensions to **M**ATLAB (sxm) is a library of MATLAB functions and classes which perform some useful tasks in an arguably saner way.
4+
**S**ane **E**xtensions to **M**ATLAB (sxm) is a library of MATLAB functions and classes which perform some useful tasks in a much saner way than MATLAB does natively.
55

66
A brief overview of features:
77

88
- ADTs
99
- List
1010
- Set
11-
- Wrappers around common MATLAB operations (e.g. plotting, solving linear systems, etc.)
11+
- Wrappers around common MATLAB operations
12+
- Plotting
13+
- Solving BVPs
14+
- Numerical methods
15+
- Numerical differentiation
16+
- Forward difference
17+
- Backward difference
18+
- Central difference
19+
- Linear systems of equations
20+
- Naive Gaussian elimination
21+
- IVP solving
22+
- Euler's method
23+
- Monte Carlo methods
1224
- Performance testing extensions
13-
- Various mathematical utilities of historical interest (e.g. [Egyptian multiplication](https://en.wikipedia.org/wiki/Ancient_Egyptian_multiplication))
25+
- Raising square matrices to arbitrary powers
26+
- Summing arbitrarily large vectors
27+
- Naive Gaussian solution of arbitrarily large Hilbert matrices
28+
- Euler's method for arbitrary number of steps
29+
- Various mathematical utilities of historical interest
30+
- [Egyptian multiplication](https://en.wikipedia.org/wiki/Ancient_Egyptian_multiplication)
1431

sxm_monte_carlo.m

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
% SXM_MONTE_CARLO Integrate arbitrary shapes using Monte Carlo method.
2+
% area = sxm_monte_carlo(domain, shapefunc, n) finds the area or volume
3+
% (or other, higher-dimensional equivalent) of the shape defined by
4+
% shapefunc. Note that domain must *totally* enclose the shape defined by
5+
% shapefunc. Also note that accuracy is proportional to the number of
6+
% points, n.
7+
%
8+
% Example
9+
% area = sxm_monte_carlo([-5 5; -5 5], @(vec)(vec(1)^2+vec(2)^2<=1),
10+
% 10000);
11+
% finds the area of the unit circle centred at the origin by using
12+
% 10000 points in the square region bounded by x=-5, x=5, y=-5, y=5.
13+
function area = sxm_monte_carlo(dom, mem, n)
14+
if n == 0 % bounds check the number of points
15+
error('nonpositive number of points')
16+
end
17+
18+
sz = size(dom);
19+
20+
dim = sz(1);
21+
vec = zeros(dim);
22+
hits = 0;
23+
points = zeros([n 2]);
24+
25+
for i = 1:n
26+
% construct vector elementwise
27+
for j = 1:dim
28+
a = dom(j,1);
29+
b = dom(j,2);
30+
vec(j) = (b-a).*rand + a;
31+
end
32+
33+
% check for membership
34+
if mem(vec)
35+
hits = hits + 1; % increment hit counter
36+
end
37+
38+
points(i,1) = vec(1);
39+
points(i,2) = vec(2);
40+
end
41+
42+
% calculate area of n-box
43+
total_area = abs(dom(1,1) - dom(1,2));
44+
45+
for i = 2:dim
46+
a = dom(i,1);
47+
b = dom(i,2);
48+
total_area = total_area * abs(b - a);
49+
end
50+
51+
p = hits / n;
52+
area = p * total_area;
53+
end

0 commit comments

Comments
 (0)