CSC 120, Assignment #3, Script to run the requested simulations.
> source("a3funs.r")
Define a function to produce three plots of amount of grass over time, cow row position over time, and cow column position over time, from the results of a simulation.
> plot_grass_pos <- function (r) {
+
+ par(mfrow=c(3,1))
+
+ plot(r$grass,pch=20,xlab="Time",ylab="")
+ title("Amount of grass")
+
+ plot(r$pos[,1],xlab="Time",ylab="",ylim=c(1,nr),pch=20)
+ title("Cow row position")
+
+ plot(r$pos[,2],xlab="Time",ylab="",ylim=c(1,nc),pch=20)
+ title("Cow column position")
+ }
Set the random number seed so results will be reproducible. Note that the seed is supposed to be set to your student ID number.
> set.seed(999999999)
Do the first simulation.
> nr <- 10 # Number of rows
> nc <- 15 # Number of columns
> steps <- 400 # Time steps to simulate for
> pr <- 0.004 # Probability of grass growing
>
> par(mfrow=c(2,2))
> r <- simulate (nr, nc, pr, steps, plot=c(100,200,300,400))
> plot_grass_pos(r)
Do the second simulation.
> nr <- 10 # Number of rows
> nc <- 15 # Number of columns
> steps <- 400 # Time steps to simulate for
> pr <- 0.007 # Probability of grass growing
>
> par(mfrow=c(2,2))
> r <- simulate (nr, nc, pr, steps, plot=c(100,200,300,400))
> plot_grass_pos(r)
Do the third simulation.
> nr <- 20 # Number of rows
> nc <- 20 # Number of columns
> steps <- 1000 # Time steps to simulate for
> pr <- 0.002 # Probability of grass growing
>
> par(mfrow=c(2,2))
> r <- simulate (nr, nc, pr, steps, plot=c(100,200,500,1000))
> plot_grass_pos(r)