Skip to content

Instantly share code, notes, and snippets.

@raffitz
Created July 3, 2016 23:07
Show Gist options
  • Save raffitz/0ac61c3b38b59463c91462eed80c32a5 to your computer and use it in GitHub Desktop.
Save raffitz/0ac61c3b38b59463c91462eed80c32a5 to your computer and use it in GitHub Desktop.
Quadruple integral to calculate average distance between two random points within a unit square
% Matlab script to determine average distance between two random points within a unit square
f=@(x,y,z,w) sqrt((x-z).*(x-z) + (y-w).*(y-w)); % Distance Function between points (x,y) and (z,w)
a=0.5;b=0.5;c=0.5;d=0.5; % Integration limits: -a<=x<a -b<=y<b -c<=z<c -d<=w<d
I=integral2(@(x,y)arrayfun(@(x,y)integral2(@(z,w)f(x,y,z,w),-c,c,-d,d),x,y),-a,a,-b,b)
@raffitz
Copy link
Author

raffitz commented Jul 3, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment