598 lines
130 KiB
Plaintext
598 lines
130 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-e894ec2378fa46eb",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# You must run this cell, but you can ignore its contents.\n",
|
||
|
"\n",
|
||
|
"import hashlib\n",
|
||
|
"\n",
|
||
|
"def ads_hash(ty):\n",
|
||
|
" \"\"\"Return a unique string for input\"\"\"\n",
|
||
|
" ty_str = str(ty).encode()\n",
|
||
|
" m = hashlib.sha256()\n",
|
||
|
" m.update(ty_str)\n",
|
||
|
" return m.hexdigest()[:10]"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 2,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-ada87b3b188b2d39",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# You must also run this cell.\n",
|
||
|
"\n",
|
||
|
"import numpy as np\n",
|
||
|
"import matplotlib.pyplot as plt"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-d7462f8b0f718bef",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"source": [
|
||
|
"# Exercise - Optimization first steps\n",
|
||
|
"\n",
|
||
|
"We are going to take our first steps towards optimization by returning to a bumblebee example.\n",
|
||
|
"\n",
|
||
|
"We are going to define the positions of a flower and the flight path of a bumblebee."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-78cb75cddd4fc5ef",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"flower = np.array([7.5, 10.3])"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 4,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-ab9736432799913b",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"def make_bee_track(t):\n",
|
||
|
" pos0 = (-10,3)\n",
|
||
|
" velocity = (4.0, 0.2)\n",
|
||
|
" pos_x = pos0[0] + t*velocity[0]\n",
|
||
|
" pos_y = pos0[1] + t*velocity[1]\n",
|
||
|
" return np.array([pos_x,pos_y])\n",
|
||
|
"\n",
|
||
|
"t = np.linspace(0,15,20)\n",
|
||
|
"bee_track = make_bee_track(t)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-08928dcd585933a7",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"source": [
|
||
|
"Here we plot these positions."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 5,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-4b58d56276437c3a",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAisAAAGdCAYAAADT1TPdAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy81sbWrAAAACXBIWXMAAA9hAAAPYQGoP6dpAAApN0lEQVR4nO3dfXRU5YHH8d8wwJBAMghIXkyMoQQqRaTEmoYKiSJxgVo4qAcXV2HlCFUCZnEPltJdoxUiVqlYioUji9hVcFdR2S11yW5JgPKygUKJ4HHBBkgwMVXZJASYkMmzf2RzS8gLE8hknpDv55w5MvfezDx5Djpf79u4jDFGAAAAluoW6gEAAAC0hlgBAABWI1YAAIDViBUAAGA1YgUAAFiNWAEAAFYjVgAAgNWIFQAAYLXuoR7A1aqrq9Pnn3+uiIgIuVyuUA8HAAAEwBijqqoqxcbGqlu31veddPpY+fzzzxUfHx/qYQAAgCtQXFysuLi4Vrfp9LESEREhqf6XjYyMDPFoAABAICorKxUfH+98jrem08dKw6GfyMhIYgUAgE4mkFM4OMEWAABYjVgBAABWC2qsvPbaaxoxYoRziCY1NVW//e1vnfXGGGVnZys2NlZhYWFKT0/X4cOHgzkkAADQyQT1nJW4uDi98MILGjx4sCRp/fr1mjx5sg4cOKBvfetbevHFF7V8+XK98cYbGjJkiJ5//nmNHz9en376aUAn3AAA0FbGGNXW1srv94d6KNc0t9ut7t27t8ttRVzGGNMOYwpYv3799LOf/UyPPvqoYmNjlZWVpaefflqS5PP5FBUVpWXLlmnOnDkBvV5lZaW8Xq8qKio4wRYA0KqamhqVlpbq7NmzoR5KlxAeHq6YmBj17Nmzybq2fH532NVAfr9f//qv/6rq6mqlpqaqqKhIZWVlysjIcLbxeDxKS0vTrl27WowVn88nn8/nPK+srAz62AEAnV9dXZ2KiorkdrsVGxurnj17cjPRIDHGqKamRn/+859VVFSkpKSky974rTVBj5XCwkKlpqbq/Pnz6tOnj95//30NGzZMu3btkiRFRUU12j4qKkonTpxo8fVycnL07LPPBnXMAIBrT01Njerq6hQfH6/w8PBQD+eaFxYWph49eujEiROqqalRr169rvi1gn410NChQ3Xw4EHt2bNHjz/+uGbMmKEjR4446y+tWmNMq6W7aNEiVVRUOI/i4uKgjR0AcO25mv/DR9u011wHfc9Kz549nRNsb7vtNhUUFGjFihXOeSplZWWKiYlxti8vL2+yt+ViHo9HHo8nuIMGAADW6PC8NMbI5/MpMTFR0dHRys3NddbV1NQoPz9fo0eP7uhhAQAASwU1Vn784x9rx44dOn78uAoLC7V48WLl5eXpoYceksvlUlZWlpYuXar3339fH3/8sWbOnKnw8HBNnz49mMMCAODq+P1SXp60YUP9P4N8GbQxRrNnz1a/fv3kcrnUt29fZWVlBfU9bRLUw0BffPGFHn74YZWWlsrr9WrEiBH66KOPNH78eEnSwoULde7cOT3xxBM6ffq0UlJStHXrVu6xAgCw16ZN0pNPSiUlf1kWFyetWCFNnRqUt/zoo4/0xhtvKC8vT4MGDdL9998flPexVVBjZe3ata2ud7lcys7OVnZ2djCHAQBA+9i0Sbr/funSW5SdOlW//N13gxIsn332mWJiYpzTJLp3t+t7iC9cuKAePXoE7fU5JRoAgED4/fV7VJq7l2rDsqysdj8kNHPmTM2bN08nT56Uy+XSTTfd1GSb06dP65FHHtF1112n8PBwTZgwQUePHv3/oRldf/31eu+995ztR44cqYEDBzrPd+/erR49eujMmTOSpIqKCs2ePVsDBw5UZGSk7rrrLv3xj390ts/OztbIkSP1T//0Txo0aJA8Ho+CeY9ZYgUAgEDs2NH40M+ljJGKi+u3a0crVqzQc889p7i4OJWWlqqgoKDJNjNnztS+ffu0efNm7d69W8YYTZw4URcuXJDL5dLYsWOVl5cnqT5sjhw5ogsXLji3EsnLy1NycrL69OkjY4wmTZqksrIybdmyRfv379eoUaM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q8zMTPXt21f9+/fXT37yEzV8vV9NTY0WLlyoG264Qb1791ZKSory8vIa/fyuXbs0duxYhYWFKT4+XvPnz1d1dXWHjP1qESsAgC7LGKPq6uo2P1atWqWEhATdddddSkhI0KpVq9r081fyHcLr169X9+7dtXfvXr366qv6+c9/rtdff12S9Ld/+7f6/e9/r40bN+rQoUN64IEH9Fd/9Vc6evSoJKmwsFD33HOPpk6dqkOHDumdd97Rzp07lZmZ2a7zGSx86zIAoEto7huAq6ur1adPnw4fy5kzZ9S7d++At09PT1d5ebkOHz4sl8slSfrRj36kzZs369/+7d+UlJSkkpISxcbGOj9z99136/bbb9fSpUv1yCOPKCwsTKtXr3bW79y5U2lpaaqurg7at1C317cudw/K6AAAQLv67ne/64SKJKWmpurll1/Wvn37ZIzRkCFDGm3v8/nUv39/SdL+/ft17NgxvfXWW856Y4zq6upUVFSkm2++uWN+iStErAAAuqzw8HCdOXOmTT9z6tQp3Xzzzaqrq3OWud1uHTlyRDfccEPA79ue3G639u/fL7fb3Wh5w16juro6zZkzR/Pnz2/yszfeeGO7jiUYiBUAQJflcrnadDhGkoYMGaI1a9Zozpw58vv9crvdWr16dZM9G+1tz549TZ4nJSXp29/+tvx+v8rLyzVmzJhmf3bUqFE6fPiwBg8eHNQxBgsn2AIA0EazZs3S8ePHtW3bNh0/flyzZs0K+nsWFxdrwYIF+vTTT7Vhwwb94he/0JNPPqkhQ4booYce0iOPPKJNmzapqKhIBQUFWrZsmbZs2SJJevrpp7V7927NnTtXBw8e1NGjR7V582bNmzcv6ON
|
||
|
"text/plain": [
|
||
|
"<Figure size 640x480 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"fig, ax = plt.subplots(nrows=1, ncols=1)\n",
|
||
|
"ax.plot( [flower[0]], [flower[1]], 'or', label='flower' )\n",
|
||
|
"ax.plot( bee_track[0], bee_track[1], '.-k', label='bee')\n",
|
||
|
"ax.axis('equal')\n",
|
||
|
"ax.legend();"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-701acd6a5b4a9581",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"source": [
|
||
|
"## Q1 Draw a figure as above with, additionally, a blue line between each point on `bee_track` and `flower`.\n",
|
||
|
"\n",
|
||
|
"When complete, your figure should look like this:\n",
|
||
|
"\n",
|
||
|
"<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAisAAAGdCAYAAADT1TPdAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAA9hAAAPYQGoP6dpAABcgElEQVR4nO3dd3hUZfo+8Hsy6Z2SSkIPSpWiYlAkKuBaUcAVdRUrIKIiuiKLruhPwL7iUhSwrgrsShER9ysiTSnSoggsBggkgcRQQhqpM+/vj8eTMz0zIeVE7s91zaWZTDlzMsy553mf9z0mpZQCERERkUH5NfUGEBEREXnCsEJERESGxrBCREREhsawQkRERIbGsEJERESGxrBCREREhsawQkRERIbGsEJERESG5t/UG3CurFYrjh8/joiICJhMpqbeHCIiIvKCUgrFxcVITEyEn5/n2kmzDyvHjx9HcnJyU28GERER1UF2djaSkpI83qbZh5WIiAgA8mIjIyObeGuIiIjIG0VFRUhOTq45jnvS7MOKNvQTGRnJsEJERNTMeNPCwQZbIiIiMjSGFSIiIjK0Bg0r8+bNQ69evWqGaFJTU/H111/X/F4phWnTpiExMREhISFIS0vD3r17G3KTiIiIqJlp0J6VpKQkvPzyy+jcuTMA4KOPPsKwYcOwe/dudO/eHa+++irefPNNfPjhh+jSpQteeuklDBkyBAcOHPCq4YaIiMhXSilUV1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rHU9KsAUoVZvXo1rrzyStx///3o0qULRo0ahSNHjng8d19DMqmGnBjdCIqKihAVFYXCwkJERkY29eYQEZFBlZeXIzMzEx06dEBwcHBTb855wdM+9+X4zcoKERERGRrDChERERkawwoREREZGsMKERERGRrDChERERkawwoREREZGsMKERERGRrDChERERkawwoREZHBpaWlYeLEiU29GU2GYYWIiIgMjWGFiIiIDI1hhYiIqA5ycnKwbt065OTkNMrzVVdXY8KECYiOjkarVq3w7LPPQju9X2VlJZ5++mm0adMGYWFh6N+/P9avX293/82bN+PKK69ESEgIkpOT8dhjj6G0tLRRtv1cMawQEdF5SymF0tJSny9z585Fu3btcPXVV6Ndu3aYO3euT/evyzmEP/roI/j7+2Pbtm14++238Y9//AMLFy4EANx333344YcfsHjxYvz888+47bbb8Kc//QkZGRkAgD179uDaa6/F8OHD8fPPP2PJkiX4/vvvMWHChHrdnw2FZ10mIqLzgqszAJeWliI8PLzRt6WkpARhYWFe3z4tLQ35+fnYu3cvTCYTAOCZZ57BypUr8eWXXyIlJQU5OTlITEysuc/gwYNx6aWXYsaMGbjnnnsQEhKCd999t+b333//PQYNGoTS0tIGOwt1fZ112b9Bto6IiIjq1WWXXVYTVAAgNTUVb7zxBnbs2AGlFLp06WJ3+4qKCrRq1QoAsHPnThw8eBCffvppze+VUrBarcjMzETXrl0b50XUEcMKERGdt0JDQ1FSUuLTfY4dO4auXbvCarXWXGc2m7Fv3z60adPG6+etT2azGTt37oTZbLa7XqsaWa1WjB07Fo899pjTfdu2bVuv29IQGFaIiOi8ZTKZfBqOAYAuXbpg/vz5GDt2LCwWC8xmM959912nykZ927p1q9PPKSkp6NOnDywWC/Lz8zFw4ECX9+3bty/27t2Lzp07N+g2NhQ22BIREfnogQcewJEjR7Bu3TocOXIEDzzwQIM/Z3Z2NiZNmoQDBw5g0aJF+Oc//4nHH38cXbp0wV133YV77rkHy5YtQ2ZmJrZv345XXnkFq
|
||
|
"\n",
|
||
|
"\n",
|
||
|
"Hint, draw the flower first. Then, make a for loop which steps through each position of the bee. Inside the for loop, draw a line segment between the bee and the flower."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 6,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": true,
|
||
|
"grade_id": "cell-43f8774b1a82b15b",
|
||
|
"locked": false,
|
||
|
"points": 1,
|
||
|
"schema_version": 3,
|
||
|
"solution": true,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAisAAAGdCAYAAADT1TPdAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy81sbWrAAAACXBIWXMAAA9hAAAPYQGoP6dpAABccUlEQVR4nO3dd3hUVf4/8Pdk0jsllQQIEJAqTTEoEhVwxQ6oWLECAiqLrsiiK/oTsK/sUhSx7iqwShER9ysiBJQiLYrAYoBAEkgMNY3UmfP74+PNnZ5JSLmR9+t55tFpd+7cDHPec87nnGtSSikQERERGZRPU+8AERERkScMK0RERGRoDCtERERkaAwrREREZGgMK0RERGRoDCtERERkaAwrREREZGgMK0RERGRovk29A+fLarXi+PHjCAsLg8lkaurdISIiIi8opVBUVIT4+Hj4+HjuO2n2YeX48eNITExs6t0gIiKiOsjOzkZCQoLHxzT7sBIWFgZA3mx4eHgT7w0RERF5o7CwEImJidXtuCfNPqxoQz/h4eEMK0RERM2MNyUcLLAlIiIiQ2NYISIiIkNr0LCyYMEC9OrVq3qIJiUlBV9//XX1/UopzJgxA/Hx8QgKCkJqair27t3bkLtEREREzUyD1qwkJCTg5ZdfRqdOnQAAH330EW6++Wbs3r0b3bt3x6uvvoo333wTH374ITp37oyXXnoJQ4cOxYEDB7wquCEiIqotpRSqqqpgsViaelf+0MxmM3x9fetlWRGTUkrVwz55rWXLlnjttdfw4IMPIj4+HpMn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rIidGNoLCwEBERESgoKEB4eHhT7w4RERlUWVkZMjMzkZSUhMDAwKbenQuCp2Nem/abPStERERkaAwrREREZGgMK0RERGRoDCtERERkaAwrREREZGgMK0RERGRoDCtERERkaAwrREREZGgMK0RERAaXmpqKyZMnN/VuNBmGFSIiIjI0hhUiIiIyNIYVIiKiOsjJycH69euRk5PTKK9XVVWFSZMmITIyEq1atcKzzz4L7fR+FRUVePrpp9GmTRuEhIRgwIAB2LBhg93zN2/ejCuvvBJBQUFITEzE448/jpKSkkbZ9/PFsEJERBcspRRKSkpqfZk/fz7atWuHq6++Gu3atcP8+fNr9fy6nEP4o48+gq+vL7Zt24Z//OMf+Pvf/45FixYBAB544AH88MMPWLJkCX7++Wfcdttt+NOf/oSMjAwAwJ49e3DttddixIgR+Pnnn7F06VJ8//33mDRpUr0ez4bCsy4TEdEFwdUZgEtKShAaGtro+1JcXIyQkBCvH5+amor8/Hzs3bsXJpMJAPDMM89g1apV+PLLL5GcnIycnBzEx8dXP2fIkCG49NJLMWvWLNx3330ICgrCO++8U33/999/j8GDB6OkpKTBzkJdX2dd9m2QvSMiIqJ6ddlll1UHFQBISUnBG2+8gR07dkAphc6dO9s9vry8HK1atQIA7Ny5EwcPHsQnn3xSfb9SClarFZmZmejatWvjvIk6YlghIqILVnBwMIqLi2v1nGPHjqFr166wWq3Vt5nNZuzbtw9t2rTx+nXrk9lsxs6dO2E2m+1u13qNrFYrxo0bh8cff9zpuW3btq3XfWkIDCtERHTBMplMtRqOAYDOnTtj4cKFGDduHCwWC8xmM9555x2nno36tnXrVqfrycnJ6NOnDywWC/Lz8zFo0CCXz+3bty/27t2LTp06Neg+NhQW2BIREdXSQw89hCNHjmD9+vU4cuQIHnrooQZ/zezsbEyZMgUHDhzA4sWL8c9//hNPPPEEOnfujLvvvhv33Xcfli9fjszMTGzfvh2vvPIK1qxZAwCYOnUqtmzZgokTJyI
|
||
|
"text/plain": [
|
||
|
"<Figure size 640x480 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# Your code here. Check it is correct by comparing your figure with the above.\n",
|
||
|
"fig, ax = plt.subplots(nrows=1, ncols=1)\n",
|
||
|
"ax.plot( [flower[0]], [flower[1]], 'or', label='flower' )\n",
|
||
|
"ax.plot( bee_track[0], bee_track[1], '.-k', label='bee')\n",
|
||
|
"for i in range(bee_track.shape[1]):\n",
|
||
|
" bee_pos = bee_track[:,i]\n",
|
||
|
" ax.plot( [flower[0], bee_pos[0]], [flower[1], bee_pos[1]], 'b-' )\n",
|
||
|
"ax.axis('equal')\n",
|
||
|
"ax.legend();"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-45a057824a59090e",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"source": [
|
||
|
"## Q2 write a function called `my_distance` which takes two arguments, each of which is a length 2 sequence of `x`, `y` position and returns the Euclidean distance between these points."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 7,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-9d86e35ff582bf8f",
|
||
|
"locked": false,
|
||
|
"schema_version": 3,
|
||
|
"solution": true,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"def my_distance(a,b):\n",
|
||
|
" a = np.array(a)\n",
|
||
|
" b = np.array(b)\n",
|
||
|
" return np.sqrt(np.sum((a-b)**2))"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 8,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": true,
|
||
|
"grade_id": "cell-d96cb3c1a49758db",
|
||
|
"locked": true,
|
||
|
"points": 0,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"assert my_distance((0,0),(3,4)) == 5\n",
|
||
|
"assert my_distance((3,4), (0,0)) == 5\n",
|
||
|
"assert my_distance((13,14), (10,10)) == 5\n",
|
||
|
"assert my_distance((10,10), (13,14)) == 5"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-9426c238469156ae",
|
||
|
"locked": true,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"source": [
|
||
|
"## Q3 compute the distance between each point on `bee_track` and `flower`. Put the results in a 1D numpy array called `distance`.\n",
|
||
|
"\n",
|
||
|
"Hint: recall the function you wrote in the \"numpy basics\" exercise called `compute_distance`."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 9,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-bf78a3429e48b28d",
|
||
|
"locked": false,
|
||
|
"schema_version": 3,
|
||
|
"solution": true,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"array([18.96154002, 16.02203642, 13.18581669, 10.53661864, 8.256474 ,\n",
|
||
|
" 6.73148224, 6.5154281 , 7.71901634, 9.83412849, 12.40287921,\n",
|
||
|
" 15.19694713, 18.11235632, 21.09886634, 24.13009179, 27.19108233,\n",
|
||
|
" 30.27281036, 33.36953095, 36.47742595, 39.59386407, 42.71697555])"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"distance = np.zeros(bee_track.shape[1])\n",
|
||
|
"for i in range(bee_track.shape[1]):\n",
|
||
|
" bee_pos = bee_track[:,i]\n",
|
||
|
" distance[i] = my_distance(bee_pos, flower)\n",
|
||
|
"display(distance)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 10,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": true,
|
||
|
"grade_id": "cell-70fd1e669decb646",
|
||
|
"locked": true,
|
||
|
"points": 1,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# If this runs without error, it means the answer in your previous cell was correct.\n",
|
||
|
"assert ads_hash(np.round(distance*1000).astype(np.int32))=='54f4f2edcb'"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Q4 make a plot of the bee track parameter `t` on the X axis and `distance` on the Y axis.\n",
|
||
|
"\n",
|
||
|
"It should look like this:\n",
|
||
|
"\n",
|
||
|
"<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAX4AAAEGCAYAAABiq/5QAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAoZklEQVR4nO3dd3hUdd7+8feH0HsLRap0RRQwEFhd197XhopSRCnK2suui4/uPrrFn+u6llXXlY6CKPa+u4K6ikJC6ChIEJEOASQEAqTM5/fHDDwRKQEyc2Yy9+u6cs3MyUzOTULuOTnne87X3B0REUkeFYIOICIisaXiFxFJMip+EZEko+IXEUkyKn4RkSRTMegApdGwYUNv3bp10DFERBLK7NmzN7l76r7LE6L4W7duTVZWVtAxRE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VOAyO3GWGQQkfhVVBzijpfn8uk3OTx8eRcu69Ys6EjlWjRH9aSaWd3I/WrA2cAS4B1gUORpg4C3o5VBROJfKOTc+9oCPly0nt9dfDzX9mwZdKRyL5oXumgKTDCzFMJvMFPc/T0zmwFMMbMhwErgqihmEJE45u78/p1FvDF3Dfec04Ehpx4bdKSkELXid/cFQLf9LN8MnBWt9YpIYnB3/t+HS5g4cyU3/aINt57ZLuhISUNn7opIIP4+bRkjP1vOwF6tGHF+J02ZGEMqfhGJudGfL+eJqUvp0705D13SWaUfYyp+EYmplzJW8qf3F3NRl6b8pU8XzZMbABW/iMTMm3NXc/9bC/fOk1sxRRUUBH3XRSQm/rUoPE9ur2MbaJ7cgJXqO29mHcxsmpkti
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 11,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": true,
|
||
|
"grade_id": "cell-acdc61a7b1539944",
|
||
|
"locked": false,
|
||
|
"points": 1,
|
||
|
"schema_version": 3,
|
||
|
"solution": true,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"Text(0, 0.5, 'distance')"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 11,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAjMAAAGwCAYAAABcnuQpAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy81sbWrAAAACXBIWXMAAA9hAAAPYQGoP6dpAABNpUlEQVR4nO3deVxU9eLG8c+wDYuAobIpKu4rLmAuWdqiv7xmmZWVuWtpaUW2aNvNuqVlN1uupanlUpneymw3NbdKTUBx30XFBXFllQFmzu8PjBuphQicGXjer9d5JWeG4zOJM4/nnO/3azEMw0BERETERbmZHUBERETkSqjMiIiIiEtTmRERERGXpjIjIiIiLk1lRkRERFyayoyIiIi4NJUZERERcWkeZgcoaw6Hg6NHj+Lv74/FYjE7joiIiBSDYRhkZGQQHh6Om9tfn3up8GXm6NGjREREmB1DRERESiA5OZlatWr95XMqfJnx9/cHCv5nBAQEmJxGREREiiM9PZ2IiIjCz/G/UuHLzO+XlgICAlRmREREXExxbhHRDcAiIiLi0lRmRERExKWpzIiIiIhLU5kRERERl6YyIyIiIi5NZUZERERcmsqMiIiIuDSVGREREXFpKjMiIiLi0lRmRERExKWpzIiIiIhLU5kRERERl6YyIyIiIiXicBj8tOO42TFUZkRERKRk3li6i2Fz4nnxm22m5lCZERERkcv29aajvLtiHwBRtQJNzaIyIyIiIpdly+E0nvp8EwAjrqvH7W1qmZpHZUZERESKLTUjhwc+iicnz0HXxjV46uYmZkdSmREREZHiseXbGflRAsfScqhXw4937m2Du5vF7FgqMyIiIvL3DMPguS+3suHQWfy9PZg5MIYAb0+zYwEqMyIiIlIMs349wGcJh3GzwJR+balXo4rZkQqpzIiIiMhf+nnPCV7+bjsAz/yjKV0a1TA5UVEqMyIiInJJSSezGD1vIw4D7mhbi2GdI82OdAGVGREREbmojJw87p8bT9q5PNrUrsort7fAYjH/ht8/U5kRERGRC9gdBo/OT2RvaiahAd683z8ab093s2NdlMqMiIiIXODfS3axfGcqVg833h8QTXCAt9mRLkllRkRERIr4KvEIU1cWLFUw6c4oWkVUNTfQ31CZERERkUKbD5/lqc83AzCyS31ua13T5ER/T2VGREREgPNLFcxNwJbv4IYmwTz5f43NjlQsKjMiIiKCLd/OiI8SSEnPoX4NP966p7VTLFVQHCozIiIilZxhGDz75VY2HjpLgLcHMwe1c5qlCopDZUZERKSS+/DXA3x+fqmCd+9rS2R1P7MjXRaVGRERkUps9e4TvHJ+qYJnezbj2obOtVRBcajMiIiIVFIFSxVswGHAXdG1GHpNXbMjlYjKjIiISCWUnpPH8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338GNTYJ5ontjsyNVGCozIiIiZSwnz84Dc+NJzbDRMLgKb93TGnc3jVwqLSozIiIiZcgwDMZ9sZlNh9Oo6uvJzEEx+Ht7mh2rQlGZERERKUPTVu1nUeJR3N0svNevLXWq+ZkdqcJRmRERESkjP+04zqQfdwIwvlczOjWobnKiisnUMjN16lSioqIICAggICCAjh078sMPPxQ+PnjwYCwWS5GtQ4cOJiYWEREpnt3HM3h0fiKGAf3a16Z/hzpmR6qwPMz8zWvVqsWrr75KgwYNAJgzZw633XYbGzdupHnz5gDcfPPNzJo1q/B7vLy0JLqIiDi3M1m5DJ8TT6Ytn/aRQYzv1VxLFZQhU8tMr169inz9yiuvMHXqVNatW1dYZqxWK6GhoWbEExERuWx5dgcPfbKBQ6ezqXWVD1P7R+Plobs6ypLT/N+12+3Mnz+frKwsOnbsWLh/5cq
|
||
|
"text/plain": [
|
||
|
"<Figure size 640x480 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# Your code here. Check it is correct by comparing your figure with the above.\n",
|
||
|
"fig, ax = plt.subplots(nrows=1, ncols=1)\n",
|
||
|
"ax.plot( t, distance )\n",
|
||
|
"ax.set_xlabel('t')\n",
|
||
|
"ax.set_ylabel('distance')"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Q5 Using `calc_distance_func` from the lecture, find the value of `t` that minimizes the distance between the bee and the flower. Save the result in `best_t`."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 12,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-23d19427610d2072",
|
||
|
"locked": false,
|
||
|
"schema_version": 3,
|
||
|
"solution": true,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"name": "stdout",
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"t: 0.0 -> dist: 18.96154002184422\n",
|
||
|
"t: 1.0 -> dist: 15.253196386331622\n",
|
||
|
"t: 2.6180339999999998 -> dist: 9.762703375781847\n",
|
||
|
"t: 15.908845000462023 -> dist: 46.31882030539557\n",
|
||
|
"t: 7.694671914602477 -> dist: 14.474578507642399\n",
|
||
|
"t: 4.55713707768905 -> dist: 6.429979961714315\n",
|
||
|
"t: 4.606244750761416 -> dist: 6.44546748591665\n",
|
||
|
"t: 4.427556823536537 -> dist: 6.417932686426614\n",
|
||
|
"t: 3.7363806287215797 -> dist: 7.03303244654773\n",
|
||
|
"t: 4.454999062553287 -> dist: 6.416983793293304\n",
|
||
|
"t: 4.455122430031719 -> dist: 6.416983777420453\n",
|
||
|
"t: 4.455112223273879 -> dist: 6.416983777290153\n",
|
||
|
"t: 4.455112157328219 -> dist: 6.416983777290158\n",
|
||
|
"t: 4.45511228921954 -> dist: 6.416983777290159\n",
|
||
|
" message: \n",
|
||
|
" Optimization terminated successfully;\n",
|
||
|
" The returned value satisfies the termination criteria\n",
|
||
|
" (using xtol = 1.48e-08 )\n",
|
||
|
" success: True\n",
|
||
|
" fun: 6.416983777290153\n",
|
||
|
" x: 4.455112223273879\n",
|
||
|
" nit: 10\n",
|
||
|
" nfev: 14\n"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"# Your code here. \n",
|
||
|
"\n",
|
||
|
"def compute_distance(a,b):\n",
|
||
|
" a = np.array(a)\n",
|
||
|
" b = np.array(b)\n",
|
||
|
" return np.sqrt(np.sum((a-b)**2))\n",
|
||
|
"\n",
|
||
|
"def calc_distance_func(t):\n",
|
||
|
" x1, y1 = flower\n",
|
||
|
" x2, y2 = make_bee_track(t)\n",
|
||
|
" dist = compute_distance((x1,y1), (x2,y2))\n",
|
||
|
" print(f't: {t} -> dist: {dist}')\n",
|
||
|
" return dist\n",
|
||
|
"\n",
|
||
|
"import scipy.optimize\n",
|
||
|
"result = scipy.optimize.minimize_scalar(calc_distance_func)\n",
|
||
|
"print(result)\n",
|
||
|
"best_t = result.x"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 13,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": true,
|
||
|
"grade_id": "cell-0a3594afd2952a33",
|
||
|
"locked": true,
|
||
|
"points": 1,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# If this runs without error, it means the answer in your previous cell was correct.\n",
|
||
|
"assert ads_hash(np.round(best_t*1000).astype(np.int32))=='dec1ab2f6d'"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 14,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"array([7.82044889, 3.89102244])"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 14,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"make_bee_track(best_t)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Q6 What is the position of the bee when it is closest to the flower? Save the result as a numpy array in `best_pos`."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 15,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": false,
|
||
|
"grade_id": "cell-cac598c7f739d146",
|
||
|
"locked": false,
|
||
|
"schema_version": 3,
|
||
|
"solution": true,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Your code here. \n",
|
||
|
"best_pos = make_bee_track(best_t)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 16,
|
||
|
"metadata": {
|
||
|
"nbgrader": {
|
||
|
"grade": true,
|
||
|
"grade_id": "cell-9d882991838f0996",
|
||
|
"locked": true,
|
||
|
"points": 1,
|
||
|
"schema_version": 3,
|
||
|
"solution": false,
|
||
|
"task": false
|
||
|
}
|
||
|
},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# If this runs without error, it means the answer in your previous cell was correct.\n",
|
||
|
"assert type(best_pos)==np.ndarray\n",
|
||
|
"assert best_pos.ndim==1\n",
|
||
|
"assert best_pos.shape==(2,)\n",
|
||
|
"assert ads_hash(np.round(best_pos[0]*1000).astype(np.int32))=='e33b9415bc'\n",
|
||
|
"assert ads_hash(np.round(best_pos[1]*1000).astype(np.int32))=='f71cbfce4c'"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"celltoolbar": "Create Assignment",
|
||
|
"kernelspec": {
|
||
|
"display_name": "Python 3 (ipykernel)",
|
||
|
"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.11.7"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 4
|
||
|
}
|