%%monte_carlo_ff.m
% Integrand (two-weight)
function ff=monte_carlo_ff (x, y)
ff=x*y^2;% function definition at
End
%%monte_carlo.m
% Monte Carlo calculation of double integrals
function Result=monte_carlo (a,b,c,d,n,m)
% first y after x integral, A is the lower limit of x integral, B is the upper limit of x integral, C is the lower limit of Y integral, D is the upper limit of y integral, N,m is the Monte Carlo parameter
sumxff=0;
For I=1:n
sumyff=0;
xff=a+ (b-a) *rand ();
For j=1:m
yff=c+ (D-C) *rand ();
SUMYFF=SUMYFF+MONTE_CARLO_FF (XFF,YFF);
End
aversumyff=sumyff/m;
sumxff=sumxff+ (b-a) *aversumyff;
End
result=sumxff/n;
End
%%show.m
Clear all;
CLC
Format long;
% double integral int function method for proofreading Monte Carlo method
Syms x y;
z=x^2*y^2;
X1=1;
x2=2;
y1=3;
y2=4;
DISP (' int ' method result ')
Ans_int=int (int (z,y,y1,y2), x,x1,x2)
% Monte Carlo method calculation results
Disp (' Monte_carlo method result ')
Ans_monte_carlo=monte_carlo (1,2,3,4,10000,20000)
% error
Disp (' ERROR ')
Error=abs (Ans_monte_carlo-ans_int)
Monte Carlo algorithm for double integral under MATLAB