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:
<- function(N, pos_init, speed_init, force, mass, delta) {
simulate <- rep(NA, N) # N: number of times we use the "flash"
pos <- rep(NA, N) # first position and first speed
speed 1] <- pos_init
pos[1] <- speed_init
speed[for(step in 2:N){
<- force(pos[step-1], speed[step-1], mass)/mass
accel <- speed[step-1] + delta * accel
speed[step] <- pos[step-1] + delta * speed[step]
pos[step] if(pos[step] <= 0) { # if we hit the floor...
<- -speed[step] # we bounce
speed[step]
}
}return(pos)
}
In the same .Rmd
file you should include these two
functions:
<- function(pos, speed, mass) {
gravity <- -9.8*mass # -g times mass
force return(force)
}
<- function(pos, speed, mass, K=20, L=1) {
coil <- K*(L-pos)
force return(force)
}
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))
Questions:
Find the R code to determine:
- What is the highest position reached by the ball in each one of the two simulations?
- What is the time of flight? That is, how many steps are required to hit the floor?
- How do these two values (highest position, time of flight) change when you change the initial speed to a value between -5 and 10?