我需要编写 MATLAB 代码,该代码将使用 Monte Carlo 在 R^5 超立方体上进行积分。当我有一个通用函数时,我有一个基本算法。但我需要集成的功能是:
∫dA
A是R^5的一个元素。
如果我有 ∫f(x)dA,那么我认为我的算法会起作用。
这是算法:
% Writen by Jerome W Lindsey III
clear;
n = 10000;
% Make a matrix of the same dimension
% as the problem. Each row is a dimension
A = rand(5,n);
% Vector to contain the solution
B = zeros(1,n);
for k = 1:n
% insert the integrand here
% I don't know how to enter a function {f(1,n), f(2,n), … f(5n)} that
% will give me the proper solution
% I threw in a function that will spit out 5!
% because that is the correct solution.
B(k) = 1 / (2 * 3 * 4 * 5);
end
mean(B)