Blog of Andrés Aravena
CMB2:

# Homework after Class 4

22 February 2017

For this homework you have to prepare a Rmarkdown document. The homework has a mandatory part and two optional ones.

You can download the Rmarkdown file used in the class and the HTML page produced from it.

## Simulate Gravity

N <- 60         # simulate 60 steps
y <- rep(NA, N) # create "empty" y vector
y[1] <- 120     # initial position
speed <- -20    # initial speed
for(i in 2:N) { # for each step
y[i] <- y[i-1] + speed  # update position
if(y[i] < -180 | y[i] > 180){ # if hit floor OR ceil
speed <- -speed     # bounce
}
}
plot(y)
• Modify this code to simulate gravity. Initial speed should be 0 and it must change by -1 on every step
• We want to make a plot to see how the speed changes. Make another version that saves the speed for each step. Now speed must be a vector. You have to insert the correct indices. It is similar to y.

You must send me one file in Rmd format with two pieces of code and two graphics.

## Exercise (optional)

This is optional, but recommended to understand better. On Class 5 we will explore how things move when they are connected to an elastic rubber band.

{: .sidenote}

• Hooke’s law for springs
• Kinetic and Potential Energy

## Good to know (optional)

When we use Scratch we can see a “movie” that is more fun than the graphics on R. Sometimes a movie can be a better way to explain an idea.

• Explore how can we animate graphics in R. Tell me something I don’t know.

Have you seen any of these movies?

• Gravity
• Apollo 13
• The Martian

What do they have in common?