MATLAB:绘制随机游走 [英] MATLAB: plotting a random walk

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

问题描述

所以我需要创建一个函数,该函数为连续进行随机跳跃的个人绘制位置与时间的关系图.每次跳跃都是以概率R右移一个单位,否则是左移一个单位.参数必须为R =可能向右跳一个单位; a =初始位置;和numjumps =个人跳的次数.我还需要使用binornd()函数.

so I need to create a function that plots location versus time for an individual who makes successive random jumps. Each jump is one unit to the right with probability R, and otherwise its one unit to the left. The arguments need to be R = probably of jumping one unit to the right; a = the initial location; and numjumps = number of jump the individual makes. I also need to use the binornd() function.

到目前为止,我编写的代码是:

What I've coded so far is:

function plot_sim(a,numjumps,R)
loc = a;
time = 0;
for i = 1:numjumps;
    loc = loc + (2*binornd(1,R)-1);
    time = time + 1;
    hold on;
    plot(time,loc,'-')
end

我必须用plot_sim(0,25,0.5)对其进行评估.而且我很困惑,因为即使我有plot(time,loc,'-'),它也不会绘制为连接线,而只会绘制为单独的点.我试过将绘图函数包含在for循环之外,但不起作用.我什至尝试更改圆点的颜色,但这甚至不起作用.我编码错了吗?

And I have to evaluate it with plot_sim(0,25,0.5). And I am just confused because even though I have plot(time,loc,'-'), it doesn't plot as connecting lines, it just plots as separate dots. I've tried including the plot function outside of the for loop and that doesn't work. I've even tried changing the colours of the dots and that doesn't even work. Am I coding this wrong?

推荐答案

解决您的问题的方法非常简单.绘制线条时,您需要至少两个点,以便绘制线条.您实际上需要做的是记住先前的位置,以便在每个时间步都可以从先前的位置绘制一条线到当前位置.

The solution to your problem is quite simple. When you are plotting lines, you need to have at least two points so that you can draw lines. What you actually need to do is remember the previous position so that at each time step, you can draw a line from the previous position to the current position.

我要做的是首先生成一个图形,该图形会产生一个跳跃,我们可以从初始位置到此点画一条线.之后,然后运行您跟踪我们先前事件的循环,在下一个时间步生成您的新事件,然后从前一个时间的上一个事件到当前的当前事件画一条线.

What I would do is first generate a figure that generates one jump and we can draw a line from the initial position to this point. After, then run your loop where we keep track of the previous event, generate your new event at the next time step then draw a line from the previous event at the previous time to the current event at the current time.

请记住,时间在水平轴上,位置在垂直轴上.这样,事件将保存为两个元素向量,其中第一个元素是时间,第二个元素是位置.

Remember, the time is on the horizontal axis and the position is on the vertical axis. As such, the events are saved as a two element vector where the first element is time, and the second element is the position.

因此,请尝试执行以下操作:

Therefore, try doing this:

function plot_sim(a,numjumps,R)
%// Keep the previous event
%// x coordinate is time
%// y coordinate is position
%// Time = 0
prev_loc = [0 a];

%// Generate the next event
%// Time = 1
loc = [1 prev_loc(2) + 2*binornd(1,R)-1];

%// Close all figures then open up a new one
close all;
figure;
hold on;

%// Plot a line from the previous position to the current one
plot([prev_loc(1) loc(1)], [prev_loc(2) loc(2)]);

%// For each new position...
for i = 2:numjumps
    %// Remember the previous position
    prev_loc = loc;
    %// Generate the next position
    loc = [i prev_loc(2) + (2*binornd(1,R)-1)];    
    %// Plot the position
    plot([prev_loc(1) loc(1)], [prev_loc(2) loc(2)]);
end

当我尝试使用plot_sim(0.25,10,0.25)运行它时,这就是我得到的,所以a = 0.25, numjumps = 10, R = 0.25:

Here's what I get when I try running it with plot_sim(0.25,10,0.25), so a = 0.25, numjumps = 10, R = 0.25:

请记住,您可能无法获得与我相同的情节,因为它是随机的.每次运行此功能时,您应该获得不同的随机游走,这就是我们所期望的.

Remember, you may not get the same plot I do because it's random. Every time you run this function, you should get a different random walk and that's what we expect.

这篇关于MATLAB:绘制随机游走的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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