Path: news.mathworks.com!not-for-mail From: "nawal " <nawalyarey@gmail.com> Newsgroups: comp.soft-sys.matlab Subject: Re: random walk & random number Date: Wed, 6 Nov 2013 20:32:08 +0000 (UTC) Organization: The MathWorks, Inc. Lines: 26 Message-ID: <l5e908$nn9$1@newscl01ah.mathworks.com> References: <l5dauc$5p$1@newscl01ah.mathworks.com> <l5e0ii$d19$1@newscl01ah.mathworks.com> Reply-To: "nawal " <nawalyarey@gmail.com> NNTP-Posting-Host: rubyext-04-ls.mathworks.com Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: newscl01ah.mathworks.com 1383769928 24297 172.20.102.180 (6 Nov 2013 20:32:08 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Wed, 6 Nov 2013 20:32:08 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 4779919 Xref: news.mathworks.com comp.soft-sys.matlab:804903 "Roger Stafford" wrote in message <l5e0ii$d19$1@newscl01ah.mathworks.com>... > "nawal " <nawalyarey@gmail.com> wrote in message <l5dauc$5p$1@newscl01ah.mathworks.com>... > > i've created a random wak and plotted it but what I’m stuck on is the last bit (the rg2) because if I run the file I get a value for rg2 and if I run it again I get different value. I want to find a way to get the different values of rg2 at once instead of trying to run it every time and with that I need to plot the values. I don’t know how to do this bit and I’m stuck so if anyone knows how to do please tell me. > > r=[0 0]; > > X=[0];Y=[0]; > > for t= 1:1:100 > > B=rand(1,1)*4; > > if B<1 > > new_position=r+[1 0]; > > elseif B<2 > > new_position=r+[0 1]; > > elseif B<3 > > new_position=r+[-1 0]; > > else > > new_position=r+[0 -1]; > > end > > X=[X new_position(1)]; > > Y=[Y new_position(2)]; > > r=new_position; > > end > > plot(X,Y) > > rg2=mean(X.^2)+mean(Y.^2)-mean(X)^2-mean(Y)^2; > - - - - - - - - - - > What you have calculated here in rg2 is a kind of sample variance of the distance from a mean position throughout a hundred-step "random walk". Naturally this variance will vary from one hundred-step run to another since it is only a sample variance and not the theoretical variance for this stochastic process. However a simple plot of the rg2 values using matlab's 'plot' function would be rather chaotic and not particularly informative. I would think it preferable to make a histogram plot of rg2 values over a large number of repeated trials. To accomplish this it would be wise to enclose your code, including the necessary initializations, in an outer for-loop to do the appropriate repetition and collect the rg2 values in a vector, rather than manually repeating your code. > > Roger Stafford