带吸附边界的一维随机游走MATLAB仿真
Intro
让你用MATLAB仿真带吸附边界的一维随机游走,初始位置0,位置为10或-10结束。
仿真5000次
记录下每次step个数,组成长度为5000的一维向量v
对向量v进行分析
Answer
clear;
close;
clear all;
close all;
clc;
v=[];% v vector we want
tmp=0;
for tmp=1:1:5000
y=0;
cnt=0;% count steps
i=1;
while true
step=sign(randn);%1 with probability of 0.5,-1 with probability of 0.5
y(i+1)=y(i)+step;
cnt=cnt+1;
if((y(i+1)==10)||(y(i+1)==-10))
v(tmp)=cnt;
break;
end
%pause(.00005);
%plot(y)
i=i+1;
end
end
v_min=min(v);
v_max=max(v);
v_mean=mean(v);
v_median=median(v);
hist(v,1e3);%histagram with 1e3 bins
Figure

Reference
https://www.math.cmu.edu/~shlomo/courses/BioSystems/Lectures/RandomWalk.pdf
这个slide挺没意思的,当然有些matlab函数用法的例子还是得记录一下
C = fix(10*rand(3,2)); %create a 3 by 2 matrix, of integers 0->9
fix; %round toward zero
plot(x); % plot the values in a vector x
bar(x); % create a bar graph form a vector
sum(x); %
hist(x); % create a histogram from the values of x
hist(x,n); % histogram with n bins
x = rand(n,1); % a vector of n random numbers U[0,1]
randn(); % a single gauss r.v.
randn(n,1); % vector of n gauss r.v.
randn(4,1) 是列向量
randn(1,4) 是行向量
randn(n); % an n by n matrix of gauss r.v.
randi([3 1],10,60) %生成10~60之间的随机整数 ,规格3\*1
Ind = find(x>=0.5); % ind is an array of indices satisfying ….
clear all; % clear memory
功率谱估计
1、直接法(周期图法)
periodogram函数是用来计算功率谱密度的
[Pxx,f]=periodogram(x,window,nfft,Fs);
X:所求功率谱密度的信号;
window:所使用的窗口,默认是问boxcar,其长度必须答与x的长度一致;
nfft:采样点数;
2、间接法,对自相关函数进行傅里叶变换得到
3、Bartlett平均周期图的方法是将NNN点的有限长序列x(n)x(n)x(n)分段求周期图再平均。
4、Welch法对Bartlett法进行了两方面的修正,一是选择适当的窗函数w(n)w(n)w(n),并再周期图计算前直接加进去,加窗的优点是无论什么样的窗函数均可使谱估计非负。二是在分段时,可使各段之间有重叠,这样会使方差减小。
matlab是一门神奇的语言
我可能是个笨蛋
你要分清
psd,pwelch,periodogram 区别自己查
angle,phase 推荐用angle吧
wgn,awgn,randn 区别自己查
你还要弄清楚这些
clc:清除命令窗口的内容
clear:清除工作空间的所有变量
clear all:清除工作空间的所有变量,函数,和MEX文件
(MEX文件是一种可在matlab环境中调用的C语言(或fortran)衍生程序,mex的编译结果实际上就是一个带输出函数mexFunction 的dll文件。)
clf:清除当前的Figure
close:关闭当前的Figure窗口
close all:关闭所有的Figure窗口
这些命令一般写在matlab程序的第一行。
等等等等的区别
Mathematical Explanation
https://zhuanlan.zhihu.com/p/146230212
看这篇知乎文章吧
1e6个样本时候的histgram长这样,你大概能猜到是什么分布吧。是的,这个stackexchange回答提到,这是Discrete phase-type distribution。
记录一下,我猜这个随机游走step数目服从的分布的均值是100


浙公网安备 33010602011771号