Results 1 to 4 of 4

Math Help - Random Walk in 2D

  1. #1
    Junior Member
    Joined
    Jan 2010
    Posts
    39

    Random Walk in 2D

    I want to write a program to simulate a Simple Random walk in 2D, but I don't really have any similar code to look at for help. Would like to use R but can't seem to find any resourses for that. Everything seems to be in C++ etc.
    Fortran 90 could also be an option. Using gnuplot to plot graphs.
    Can anyone help me out?
    Thanks.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Member
    Joined
    Mar 2009
    Posts
    133
    Something like

    Code:
    rw <- function(n){
    	y <- numeric(n)
     	for (i in 1:(n-1)){
    	y[i+1] <- y[i]+rnorm(1)
     	}
     plot(y, type="l")
    }

    Will create a function that will plot a random walk with initial level of 0, and steps that are IID normal(0,1) in R
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Junior Member
    Joined
    Jan 2010
    Posts
    39
    But is that code not for a 1-dim walk?
    Would I have to use a 2d vector (x,y) that goes to (x+1,y) when then move right, (x-1) for left, (x,y+1) for up and (x,y-1) for down?
    I could generate a random number from the U[0,1] distribution to decide which step left,right,up,down to take.

    Is this along the right lines?
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Member
    Joined
    Mar 2009
    Posts
    133
    how about;

    Code:
    rw <- function(n){
    	y <- data.frame(numeric(n),numeric(n))
     	for (i in 1:(n-1)){
    	y[i+1,1] <- (y[i,1]+rnorm(1))
    	y[i+1,2] <- y[i,2]+rnorm(1)
    	}
     plot(y, type="l")
    }
    Or if you were after discrete steps +1 or -1
    Code:
    rw <- function(n){
    	y <- data.frame(numeric(n),numeric(n))
     	for (i in 1:(n-1)){
    	y[i+1,1] <- (y[i,1]+ (ifelse(runif(1)<.5,1,-1)))
    	y[i+1,2] <- y[i,2]+(ifelse(runif(1) <.5,1,-1))
    	}
     plot(y, type="l")
    }
    Last edited by Robb; November 17th 2010 at 04:14 AM.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Random walk
    Posted in the Advanced Statistics Forum
    Replies: 4
    Last Post: March 26th 2011, 10:43 PM
  2. Random Walk
    Posted in the Advanced Statistics Forum
    Replies: 1
    Last Post: October 18th 2010, 05:29 PM
  3. an almost random walk SP
    Posted in the Advanced Statistics Forum
    Replies: 3
    Last Post: July 15th 2010, 11:58 AM
  4. Random Walk in 1-D
    Posted in the Advanced Statistics Forum
    Replies: 2
    Last Post: May 3rd 2010, 10:14 AM
  5. Random walk
    Posted in the Advanced Statistics Forum
    Replies: 2
    Last Post: February 19th 2009, 06:22 AM

Search Tags


/mathhelpforum @mathhelpforum