绘制布朗运动Matlab [英] Plotting brownian motion matlab

查看:206
本文介绍了绘制布朗运动Matlab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我只想说我不习惯使用matlab,但是我需要做一个作业,我应该创建一个布朗运动".我的代码当前看起来像这样:

First of all, I just want to say that I'm not that used to using matlab, but I need for an assignment, I'm supposed to create a "brownian movement". My code is currently looking like this:

clf
hold on
prompt = 'Ge ett input';
size = input(prompt) ;
numParticles = input('Ange antal partiklar');
axis([-size size -size size]);
Part = [];
color = 'brkgmyco';
for i = drange(1:numParticles)
   Part = [Part [0;0]];
end
for i = drange(1:200)

    dxdy = randn(2,numParticles);
    k = Part
    Part = Part + dxdy;

我关心的是如何打印,我什至希望在每次打印时都稍有延迟,所以您真的可以看到分配的情况,这是否可以从我现在编写的代码中实现,或者应该被改变?预先感谢!

My concern is how to print, I would even want like a small delay on every print, so you really can see what's happening for the assignment, is this possible to achieve from the code I've written for now or should anything be changed? Thanks in advance!

推荐答案

无论您尝试执行什么操作,代码都有一些基本问题:

Here are some basic problems with your code, regardless of what you are trying to do:

  1. 您将size用作变量名.这样做会覆盖MATLAB的功能 size .
  2. 函数 zeros 创建一个由零初始化的数组,不需要为此循环.
  3. 您可以使用dxdy = randn(2,numParticles,200)进行一次计算,而不必在循环中计算randn 200次,然后只需在循环中引用dxdy(:,:,i).
  4. 求和也是如此.而不是在循环中求和以获取累计和,请使用 cumsum 类似于Part = cumsum(randn(2,numParticles,200),3);,然后在循环内引用Part(:,:,i).
  1. You use size as a variable name. Doing so overrides MATLAB's function size.
  2. The function zeros creates an array initialized by zeros, no need for a loop for that.
  3. Instead of calculating randn for 200 times in a loop, you can do it once, with dxdy = randn(2,numParticles,200) and then simply refer to dxdy(:,:,i) within the loop.
  4. The same holds for summation. Instead of summing within a loop to get the cumulative sum, use cumsum like Part = cumsum(randn(2,numParticles,200),3); and then refer to Part(:,:,i), within the loop.

现在完成您的任务.您说您想知道如何打印,但是我相信您想 plot ,因为您使用了诸如axisclfhold之类的命令,这些命令引用了图形对象.但是,您从来没有真正做过任何事情.
在2D模式下进行绘图的基本功能是 plot ,但是还有许多其他更具体的功能.其中之一是scatter,它具有一个姐妹函数 gscatter ,它使用xygroup的三元组,并绘制每个由group(k)着色的(x(k),y(k)).

Now to your task. You said you want to know how to print, but I believe you want to plot because you use some commands like axis, clf and hold, that refer to graphic objects. However, you never really do plot anything.
The basic and general function for plotting in 2D is plot, but there are many other more specific functions. One of them is scatter, and it has a sister function gscatter, that takes triples of x, y and groupand plot each (x(k),y(k)) colored by their group(k).

此代码在轴上绘制粒子并为其运动设置动画:

This code plots the particles on an axes, and animate their movement:

prompt = 'Ge ett input';
scope = input(prompt) ;
numParticles = input('Ange antal partiklar');
N = 500;
Part = cumsum(randn(2,numParticles,N)*scope/100,3);
h = gscatter(Part(1,:,1),Part(2,:,1),1:numParticles);
axis([-scope scope -scope scope]);
legend off
for k = 2:N
    for p = 1:numParticles
        h(p).XData = Part(1,p,k);
        h(p).YData = Part(2,p,k);
    end
    drawnow
end

这是您要寻找的吗?

这篇关于绘制布朗运动Matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆