MATLAB 中 R^5 超立方体的蒙特卡罗积分
我需要编写 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)
I need to write MATLAB code that will integrate over a R^5 hypercube using Monte Carlo. I have a basic algorithm that works when I have a generic function. But the function I need to integrate is:
∫dA
A is an element of R^5.
If I had ∫f(x)dA then I think my algorithm would work.
Here is the algorithm:
% 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
无论如何,我想我明白这里的意图是什么,尽管它看起来确实有点做作。考虑一下尝试通过 MC 求圆面积的问题,如此处所述。这里的样本是从一个单位正方形中抽取的,函数在圆内取值 1,在圆外取值 0。为了求出 R^5 中立方体的体积,我们可以从包含该立方体的其他物体中进行采样,并使用类似的过程来计算所需的体积。希望这足以让其余的实现变得简单。
In any case, I think I understand what the intent here is, although it does seem like somewhat of a contrived exercise. Consider the problem of trying to find the area of a circle via MC, as discussed here. Here samples are being drawn from a unit square, and the function takes on the value 1 inside the circle and 0 outside. To find the volume of a cube in R^5, we could sample from something else that contains the cube and use an analogous procedure to compute the desired volume. Hopefully this is enough of a hint to make the rest of the implementation straightforward.
我在这里猜测一下,因为您作为“正确”答案给出的数字与您陈述练习的方式不匹配(单位超立方体的体积为 1)。
鉴于结果应该是 1/120 - 是否您应该集成 超立方体中的标准单纯形?
你的功能会很清楚。如果 sum(x) <,则 f(x) = 1 1;否则为 0
I'm guessing here a bit since the numbers you give as "correct" answer don't match to how you state the exercise (volume of unit hypercube is 1).
Given the result should be 1/120 - could it be that you are supposed to integrate the standard simplex in the hypercube?
The your function would be clear. f(x) = 1 if sum(x) < 1; 0 otherwise