In [3]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

Problem 1: The Fibonacci Sequence and Calculating the Golden Ratio

The Fibonacci sequence is defined by $f_{n+1} =f_n + f_{n-1}$ , giving the sequence: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 ...

Your program should implement the function fib, which calculates the $n$-th element in the Fibonacci sequence. It should have one argument, $n$. Its return value should be the $n$-th element.


In [ ]:

Advanced: The ratio of successive elements converges to $$\phi = (1 + \sqrt{5})/2 ≈ 1.61803,$$ the famous golden ratio. Your task is to approximate $\phi$.

Define a function phi_approx that

  • calculates the approximate value of $\phi$ obtained by the ratio of the $n$-th and $(n−1)$-st elements, $f_n /f_{n-1} \approx \phi$ and
  • prints $n,\, f_n, \,f_{n-1},$ and $\phi.$

phi_approx should have one argument, $n$. Its return value should be the approximate value of $\phi$. This function should probably call fib.


In [ ]:

Problem 2: Theme park engineer

For this part you’ll play theme park engineer. You’re assigned to analyze the performance of a drop tower, which is like a roller coaster that only moves vertically. Riders are strapped into seats in the cab, and then motors within the obelisk-shaped structure accelerate the cab up and down. Your task is to plot the position, velocity, and acceleration of the drop tower over time.

First, we load the data from droptower_vdata.txt using the numpy.loadtxt command.


In [ ]:

Next, let's integrate the velocity data to find the position $y(t)$. You can approximate the integral using the Riemann sum $$y(t) \approx \sum_ {t=t_{\text{min}}}^{t_{\text{max}}} v(t)\, dt$$ What is the value of $dt$ for the data? What's the best way to perform the sum? (hint: loops)


In [ ]:

Advanced: You may also elect to use the trapezoidal rule to get a better approximation to the integral $$v(t)\,dt \approx \frac{1}{2}(v(t+1)+v(t)).$$ This can be implemented using the scipy.integrate.cumtrapz command, setting the optional parameter initial = 0


In [ ]:

Now, let's differentiate the velocity to find the acceleration $a(t)$. Using the same velocity data from above, we can write $$a(t) \approx \frac{v(t+1)-v(t)}{dt}$$ For this purpose, you may use the numpy.diff command. The array it returns is one element shorter than the array it accepts; make sure you understand why.


In [ ]:

Finally, let's plot the data as a function of time. You will need three calls to plt.plot() for this. (Don't forget to show the plot!)


In [ ]:

Advanced: Your supervisor wants to know how powerful the drop tower’s motors are. They’re exerting the most force when they’re causing the cab to accelerate upwards. Your task is to calculate the average acceleration of the cab when it’s accelerating upwards.

Use the array of accelerations you calculated above. We don’t care about the negative values of acceleration (because that’s when gravity is doing the work), so create a new array containing only the values of acceleration that are positive. (Hint: use logical indexing.) Then calculate the average of the positive accelerations and print it.


In [ ]: