如何在 MATLAB 中矢量化随机游走模拟

发布于 2024-09-24 13:50:57 字数 323 浏览 1 评论 0原文

我正在 MATLAB 中重写蒙特卡罗仿真模型,重点是可读性。该模型涉及许多粒子,表示为 (x,y,z),在具有特定终止概率的一小组状态上随机游走。与输出相关的信息是终止于给定状态的粒子数量。

模拟需要足够的粒子,为每个粒子单独运行它的成本过高。矢量化似乎是提高 MATLAB 性能的方法,但是有没有惯用的方法可以在 MATLAB 中创建此模拟的矢量化版本?

为了实现这一目标,我正在拼尽全力 - 我什至尝试创建一个表示每个粒子状态组合的 (nStates x nParticles) 矩阵,但这种方法在可读性方面很快就会失控,因为粒子从状态反弹彼此独立地陈述。我应该硬着头皮切换到更适合这个的语言吗?

I am rewriting a Monte Carlo simulation model in MATLAB with an emphasis on readability. The model involves many particles, represented as (x,y,z), following a random walk over a small set of states with certain termination probabilities. The information relevant for output is the number of particles that terminate in a given state.

The simulation requires enough particles that running it for each particle individually is cost prohibitive. Vectorization seems to be the way to get performance out of MATLAB, but is there any idiomatic way of creating a vectorized version of this simulation in MATLAB?

I'm beating my head against the wall to accomplish this - I've even tried creating a (nStates x nParticles) matrix representing each particle-state combination, but this approach quickly spirals out of control in terms of readability since particles bounce from state to state independently of one another. Should I just bite the bullet and switch to a language more suitable for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゝ偶尔ゞ 2024-10-01 13:50:57

只需像平常一样编写代码即可。几乎所有 Matlab 函数都可以接受并返回矢量化输入。例如,要模拟一维中 N 个粒子的布朗运动

position = zeros([N 1]); %start at origin
sigma = sqrt(D * dt); %D is diffusion coefficient, dt is time step
for j = 1:numSteps
    position = position + sigma*randn(size(position));
end

,如果您希望每个位置都有不同的 sigma,则可以将 sigma 设为与位置大小相同的向量,并使用“点时间”表示法来指示逐个元素操作

position = position + sigma.*randn(size(position));

如果散射是位置和一些随机元素的任意函数,您只需编写一个矢量化函数,例如

function newstep = step(position)
%diffusion in a overdamped harmonic potential
newstep = -dt*k*position + D*randn(size(position));

for j = 1:numsteps; position = position + step(position);

等等

Just write the code as you normally would. Almost all matlab functions can accept and return vectorized input. For instance, to simulate a brownian motion of N particles in 1 dimension

position = zeros([N 1]); %start at origin
sigma = sqrt(D * dt); %D is diffusion coefficient, dt is time step
for j = 1:numSteps
    position = position + sigma*randn(size(position));
end

if you wanted to have a different sigma for each position, you would make sigma a vector the same size as position and use "dot times" notation to indicate element by element operation

position = position + sigma.*randn(size(position));

if the scattering was an arbitrary function of position and some random element, you would just have to write a vectorized function, e.g.

function newstep = step(position)
%diffusion in a overdamped harmonic potential
newstep = -dt*k*position + D*randn(size(position));

for j = 1:numsteps; position = position + step(position);

and so on

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文