Why I want to do this?
http://en.wikipedia.org/wiki/Random_walk
Here's the code I wrote:
% random walk
x=[];
y=[];
x(1)=0;
y(1)=0;
for i=1:100000
J=rand;
if J<0.25
x(i+1)=x(i)+1;
y(i+1)=y(i);
elseif (J>0.25)&(J<0.5)
x(i+1)=x(i)-1;
y(i+1)=y(i);
elseif (J>0.5)&(J<0.75)
x(i+1)=x(i);
y(i+1)=y(i)+1;
else
x(i+1)=x(i);
y(i+1)=y(i)-1;
end
end
plot(x,y)
And here are the figure I got:
MATLAB applications, tutorials, examples, tricks, resources,...and a little bit of everything I learned ...
Subscribe to:
Post Comments (Atom)
my-alpine and docker-compose.yml
``` version: '1' services: man: build: . image: my-alpine:latest ``` Dockerfile: ``` FROM alpine:latest ENV PYTH...
-
It took me a while to figure out how to insert a space in Mathtype equations. This is especially useful when you write an equation with mult...
-
Recently I read post from Dr. Doug Hull's blog: http://blogs.mathworks.com/videos/2009/10/23/basics-volume-visualization-19-defining-s...
-
To get the slope of a pair of x and y, usually I first plot the curve and then add the trend line. Actually there are two functions i...
It worked great! It also works by adding the z-dimension in a similar fashion.
ReplyDeleteThanks!
Julian
Yes,..not only a working program but it is well written and logically laid out, which makes a much better program,
ReplyDeletethanks for sharing such high quality work!
Thanks for your comments, Antolin and jccbama.
ReplyDeletesir I will need your picture and a statue of yourself will be built next to my programming class! thank you!
ReplyDeleteDear Anonymous, I am so flattered...
ReplyDeleteFYI, you have a slight problem if the random number IS 0.25, 0.5, or 0.75... it'll automatically interpret it as >0.75. I would've added in "<=" to avoid that.
ReplyDeleteHello,
ReplyDeleteGreat information! I’ve been looking for something like this for a while now. Thanks!
Female fitness program
this program lacks comment. for example what is exactly J? why those conditions! but its great overall very simple
ReplyDeleteI agree with you. I was not aware of the importance of commenting by the time I wrote this code, which is six years ago. But now I do! Thank you very much!
Delete