Matlab

Moderators: None (Apply to moderate this forum)
Number of threads: 1467
Number of posts: 2144

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Random walk code Posted by Dgrayman on 12 Feb 2012 at 8:21 PM
Hello, this is the Question:
Suppose you want to know how often a single source of fungus will spread more than one foot from a fallen tree. You will be using a computer simulation to answer the question.

i found this code using the m file ranwalk2d.m:

2-dimensional random walk
ranwalk2d.m

Plots the points (u(k),v(k)), k=1:n, in the (u,v)-plane, where (u(k)) and (v(k)) are one-dimensional random walks. The sample code plots four trajectories of the same walk, four different colors.

n=100000;
colorstr=['b' 'r' 'g' 'y'];
for k=1:4
z=2.*(rand(2,n)<0.5)-1;
x=[zeros(1,2); cumsum(z')];
col=colorstr(k);
plot(x(:,1),x(:,2),col);
hold on
end
grid



The problem is i don't want the graph to go below 0 on the y axis. In other words pertaining to the problem, the fungus cant go back to its starting point.

I'm not a computer science person so i'm not that knowledgeable. Can Someone please guide me on how to do random walks on matlab?
Report
Re: Random walk code Posted by cdig on 12 Feb 2012 at 11:35 PM
Change this line
plot(x(:,1),x(:,2),col);

to this
plot(x(:,1), abs(x(:,2)), col);

that will eliminate the negative values.

I really don't know what you want to do, but the function 'abs'
gets the absolute value of any number. If you have negative values, they will become positive on the same plot.
Report
Re: Random walk code Posted by Dgrayman on 13 Feb 2012 at 10:22 AM
i guess I'm trying to figure out how far it will end up from its initial starting point using the random walk process or what have you. So it can go down, but it cannot go below 0. I'm really trying to learn this stuff but ive only used this program to solve linear algebra and differential equations so its difficult for me.
Report
Re: Random walk code Posted by cdig on 14 Feb 2012 at 5:47 PM



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.