{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Assignment 1\n",
"\n",
"## **Your name here** (**Your student number here**)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This assignment is due on **Monday, February 11th**, at noon. You need to submit the `ipynb` file via Quercus. If you are unsure how to submit the assignment, please check before the deadline. Submissions of `html` files or otherwise scrambled files will not be accepted."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The first assignment guides you through the steps of building a numerical solver for ordinary differential equations. The goal is to solve any differential equation which can be brought into this standard form:\n",
"$$ \\frac{d\\mathbf{y}}{dt} = f(\\mathbf{y}, t).$$\n",
"Although this equations looks very simple, because $\\mathbf{y}$ can be an arbitrarily large vector, complex problems can be described by it. To solve a differential equation, you also need initial conditions: $\\mathbf{y}(t_0)=\\mathbf{y}_0$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this assignment you will use the Euler to solve a specific differential equation. But you will write the Euler method in a very general form, so that it can also be used for any other equation that can be brought into the above standard form.\n",
"\n",
"The Euler method works be discretizing time and making many small timesteps $h$, starting from the the initial time $t_0$ to some final time $t = t_0+N_{steps}\\cdot h$.\n",
"\n",
"One step looks like this:\n",
"$$\\mathbf{y}_{n+1} = \\mathbf{y}_{n} + h\\cdot f(\\mathbf{y}, t_n)$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part 1\n",
"\n",
"We are looking for the trajectory of a projectile subject to gravity (in the vertical direction) and a quadratic drag force. Choose a suitable coordinate system and then complete the following equations (Newton's second law in two dimensions):"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$m \\cdot \\frac{d^2x}{dt^2} = ... $$\n",
"$$m \\cdot \\frac{d^2y}{dt^2} = ... $$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, we need to transform these two second order differential equations into four first order equations to use the Euler method. To do that, first complete the following equations. The first two are almost exactly the same as above. Write the last two as a trivial identity. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\\frac{d^2x}{dt^2} = ... $$\n",
"$$\\frac{d^2y}{dt^2} = ... $$\n",
"$$\\frac{dx}{dt} = ... $$\n",
"$$\\frac{dy}{dt} = ... $$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, define a vector $\\mathbf{y}$ and then rewrite the above equations as a vector equation (note that we use the symbol $y$ to represent a coordinate and the symbol $\\mathbf{y}$ to represent a vector):"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$$\\frac{d\\mathbf{y}}{dt} = \\begin{pmatrix} ...\\\\...\\\\...\\\\...\\end{pmatrix}$$\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part 2\n",
"\n",
"Complete the implementation of the function $f(\\mathbf{y},t)$ that takes the vector $\\mathbf{y}$ and the current time $t$ as an argument and returns the derivative of $\\mathbf{y}$. This is what we will use for the Euler method."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def f(y,t):\n",
" dydt = np.zeros(4)\n",
" ...\n",
" return dydt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part 3\n",
"\n",
"Complete the code in the following cell to implement the Euler method. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y0 = np.array([1.,2.,3.,4.]) # some initial conditions, you might need to adjust these \n",
"h = 0.001\n",
"Nsteps = 1000\n",
"trajectory = np.zeros((Nsteps,4))\n",
"y = y0\n",
"for i in range(Nsteps):\n",
" y = ...\n",
" ...\n",
" trajectory[i] = y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part 4\n",
"\n",
"The above implementation of the Euler method keeps a log of the trajectory. Plot this trajectory using matplotlib. Add labels and units. Make sure your axes are not stretched more in one dimension than the other. Play around with the initial conditions (or the drag constant) to find a trajectory where you can see that the drag affects the shape of the trajectory (it would be a parabola without any drag). "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig, ax = plt.subplots()\n",
"ax.plot( ... )\n",
"..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}