Blog of Andrés Aravena

Exercises after Class 8

10 March 2017

The next week we will have a graded quiz. It will be like an exam and it will allow you to practice for the real exam.

To prepare for that, please take the code of the function simulate that we developed in classes, and put it in a .Rmd file. The code is:

simulate <- function(N, pos_init, speed_init, force, mass, delta) {
  pos <- rep(NA, N)   # N: number of times we use the "flash"
  speed <- rep(NA, N) # first position and first speed
  pos[1] <- pos_init
  speed[1] <- speed_init
  for(step in 2:N){
    accel <- force(pos[step-1], speed[step-1], mass)/mass
    speed[step] <- speed[step-1] + delta * accel
    pos[step] <- pos[step-1] + delta * speed[step]
    if(pos[step] <= 0) { # if we hit the floor...
      speed[step] <- -speed[step] # we bounce

In the same .Rmd file you should include these two functions:

gravity <- function(pos, speed, mass) {
  force <- -9.8*mass # -g times mass

coil <- function(pos, speed, mass, K=20, L=1) {
  force <- K*(L-pos)

And then add two graphics, like this:

plot(simulate(N=200, pos_init=1.5, speed_init=5, force=gravity, mass=1, delta=0.01))
plot(simulate(N=200, pos_init=1.5, speed_init=5, force=coil, mass=1, delta=0.01))


Find the R code to determine: