{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "##
Homework #2 (Coding)
\n", "\n", "## Coding\n", "The purpose of this assignment is to familiarize yourself with the type of Python code you will need to write in the projects. In the projects, you will be simulating models, then analyzing the output. The code here is designed to given you practice plotting and analyzing data.\n", "\n", "For each project and homework, you will be turning in a Jupyter Notebook. You can either write all the code directly in the notebook, or you can write support code in .py files that are imported to the notebook. The advantage of the former is that you can turn in just the notebook. The advantage of the latter is that you will not be copy-pasting code from one notebook to another. If you choose the latter option, then you will need to zip up the folder with the notebook and the code, to turn in both. \n", "\n", "We will use the notebook to interveave code with text. Text belongs in \"Markdown\" cells and code belongs in \"Code\" cells. Jupiter Markdown allows you to use the basic components of the LaTex mathematical mark-up language, which will make your reports look professional. And LateX is commonly used, so it is worth learning at least the basics.\n", "\n", "### 1. Generate a sine curve\n", "Generate a sine curve that troughs at zero, peaks at desiredAmp, and has period desiredPeriod. Convince yourself you understand the line that calls sin. This is meant to be a stand-in for simulation results." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "t = np.arange( 0, 360, 0.1 ) # timesteps in hours\n", "desiredPeriod = 24 # in hours\n", "desiredAmp = 4.5 # in arbitrary units (a.u.)\n", "y = desiredAmp/2 * (1 + np.sin( (np.pi/12)*t ))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2. Plot the sine curve\n", "Plot t and y, being sure to label the x- and y-axes.\n", "\n", "Then add code to highlight the peaks. To do so, re-plot the data, but use only the data at the peaks, and make the symbol a magenta triangle. To find the peaks, use scipy.signal.find_peaks. For data as clean as these, there is no need to add any threshold or distance parameter.\n", "\n", "Note: It is frequently a good idea to put plotting code in a separate cell from the calculation code, so that you don't need to redo a calculation if you want to adjust your plot. So, that is what I am modeling here, by having this next cell be separate from the previous one." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "\n", "# Write code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3. Write a get_amplitude function\n", "\n", "Write a function that takes as input a 1D numpy ndarray of values, and returns the amplitude. We will make the wild and crazy assumption that the values are oscillating beautifully, and that the peak-to-trough amplitude is just the maximum value minus the minimum value.\n", "\n", "Test it on the variable y computed in a previous cell. The answer should be 4.5." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Write code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 4. Write a get_period function\n", "\n", "For very smooth data (which ours will be), it is possible to compute the period using a \"stroboscope\" method. In other words, if you have a signal over 15 cycles, you can find the some point on all 15 cycles (such as the peak), then use the times of those markers to compute the period. With 15 cycles, you will have 14 period values. The official period of the data is the mean of those 14 values.\n", "\n", "Write a get_period function that computes the period of the data. For the sake of simplicity, you may use the peaks. But to have a more stable result, I have found that a good marker is the point at which the signal crosses the mean as its value falls. \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Write code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 5. Verify that your methods work with different periods and amplitudes. \n", "\n", "For most values, this is a trivial change. But you could push them to the point at which it breaks down. Wouldn't that be fun? What are the limits of period and amplitude for this set-up? Answer in general terms (i.e. how big or little could the period be, given the time array we are using)?\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Write code here." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }