mahagadorn / r-intro-mahagadorn

r-intro-mahagadorn created by GitHub Classroom
0 stars 0 forks source link

Lost Professor #13

Open mahagadorn opened 7 years ago

mahagadorn commented 7 years ago

Hi Will,

I have been trouble shooting this one for a while...

I am trying to include the miles till doom aspect for the professor questions (lesson 2, Q 15). The code is below and I will follow that with my question.


Doom.Walk.Sim <- function(num.its, TI, Dist){
  Time <- trunc(seq((0), to=(num.its*TI), by=TI))     
  xcor <- round(rnorm(num.its), digits = 2)                                
  xcor <- c(0, xcor)                                                        
  ycor <- round(rnorm(num.its), digits = 2)                                 
  ycor <- c(0, ycor)                                                        
  sum.ycor <- cumsum(ycor)
  distance <- num.its + 1
  for (i in Time) {
    distance[i] <- sqrt(((xcor[i+1] - xcor[i])^2) + (ycor[i+1] - ycor[i])^2)
    if(distance > 5){
      return(c("The professor will die a horribly painful death in", (Time/60), "minutes!"))
    }
  }
}                                                 

Doom.Walk.Sim(100, 5, 5)

The above does return a value, but I know that it is wrong because it is simply repeating time (which is length 101) divided by 60(seconds). This of course isn't correct.

Then I was thinking that we should not be specifying the number of iterations, so I modified my function to preallocate time, as well as, my coordinates. That code is below.


Doom.Walk.Sim <- function(TI, Dist){
  Time <- 0
  Time <- Time + TI
  xcor <- 0                             #starts at zero
  xcor <- xcor + round(rnorm(1), digits = 2)
  ycor <- 0
  ycor <- ycor + round(rnorm(1), digits = 2)                                 
  distance <- 0
  for (i in Time) {
    distance <- sqrt(((xcor[i+1] - xcor[i])^2) + (ycor[i+1] - ycor[i])^2)
    if(distance > 5){
      return(c("The professor will die a horribly painful death in", (Time/60), "minutes!"))
    }
  }
}                                                 

Doom.Walk.Sim(5,5)

I am still getting an error message: Error in if (distance > 5) { : missing value where TRUE/FALSE needed and I am STUCK!!!

I think it might be with my distance section. Should this not be a for loop? Any help would be greatly appreciated.

Thank you, Mal

mahagadorn commented 7 years ago

I am also have a few more question. For our class methods. I put into mine a plot () to generate a new plot everytime the function is called. This creates some complication when attempting to call the functions in the canvas plot. For the canvas object should I be building things from scratch or calling my previously made functions?

willpearse commented 7 years ago

Take a look at your definition of the variable xcor. You've defined it as a vector of length 1, yet you're referencing it with xcor[i+1]. Does that strike you as strange? This is causing a problem in your definition of distance, which is (in turn) causing R a problem when you compare distance to something. See if you can get R to print out distance for you (print(distance)) and see if you can work out what's going on.

Also, remember that you want to see how far the professor's gone from the start point, not from the last time-step. So what should you be comparing with what?...

willpearse commented 7 years ago

Your plot question is a reasonable one... Let me show you a trick below that will help you with this...

plot.will <- plot(x, first=TRUE, ...)}
   if(first)
      plot(x, type="n")
   #whatever code you'd normally use to plot a will object
}

This will make an empty plot window first (if first==TRUE) and then call whatever code you need to do the rest of the plot. You might like to know that there are functions like points and lines that will plot points and lines onto an existing plot...

mahagadorn commented 7 years ago

So, I am definitely making progress, however, now it is happening WAY to soon. My last simulation said it happened in 8 seconds (i know the professor is fast, but he is definitely not that fast.

Here are the updates that I have made.


Doom.Walk.Sim <- function(numb.its, TI, Dist){
  Time <- 0
  Time <- Time + TI
  xcor <- vector(mode = "numeric", length=numb.its)                            #starts at zero
  ycor <- vector(mode = "numeric", length=numb.its) 
  for(i in 1:numb.its){
    xcor <- xcor + rnorm(1)
    ycor <- ycor + rnorm(1)
    distance <- sqrt((xcor[length(xcor)])^2 + (ycor[length(ycor)])^2) 
    if((distance > 5)){
      print(distance)
      return(c("The professor will die a horribly painful death in", ((Time)/60), "minutes!"))
    }
  }
}                                                 

Doom.Walk.Sim(1000,5,5)

Does this take into consideration how far he as gone from the start? I thought it does but I don't know if we need to use cumsum() to get a cumulative sum of distance.