{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "# Homework 14"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Before doing the homework, review the notes on arrays. Memorize the key functions. Practice."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 1\n",
    "\n",
    "- Make an array of $x$ from 0 to 5 with 1000 points.\n",
    "- create the following arrays\n",
    "    - $2x$\n",
    "    - $x^2 -3x + 4$\n",
    "    - $\\sin(5x)$\n",
    "- Plot each array versus $x$ on a single plot."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 2\n",
    "\n",
    "- Ideal gas law\n",
    "$$\\rho = \\frac{MP}{RT}$$\n",
    "- M=29 kg/kmol\n",
    "- R=8314 J/kmol*K\n",
    "- P=101325 Pa\n",
    "- **Find $\\rho$ for $T=300,400,\\ldots1000$ K**\n",
    "    - Store $\\rho$ and $T$ as arrays\n",
    "    - Plot $\\rho$ versus $T$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 3\n",
    "\n",
    "- Redo Problem 2 by writing a function for $\\rho(T)$ as you would have in a previous lecture.\n",
    "    - Call the function with an array of $T$ and store the result as an array of $\\rho$.\n",
    "    - Plot $\\rho$ versus $T$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 4\n",
    "\n",
    "- You have an array of $x$ points $x=0.0, 0.1, \\ldots, 50$.\n",
    "- Create $x$\n",
    "- Create $y=\\sin(x)$\n",
    "- At interior points, use a central difference approximation to find the derivative \n",
    "$$\\left(\\frac{dy}{dx}\\right)_i \\approx \\frac{y_{i+1}-y_{i-1}}{2\\Delta x},$$\n",
    "where $\\Delta x = x_1-x_0$.\n",
    "- At edge point use a one-sided difference approximation,\n",
    "$$\\left(\\frac{dy}{dx}\\right)_0 \\approx \\frac{y_{1}-y_{0}}{\\Delta x},$$\n",
    "$$\\left(\\frac{dy}{dx}\\right)_{n-1} \\approx \\frac{y_{n-1}-y_{n-2}}{\\Delta x}.$$\n",
    "- Plot $dy/dx$ versus $x$, and $\\cos(x)$ versus $x$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 5\n",
    "- Solve the rate equation below to $t=5$ s using the Explicit Euler method $y_{i+1} = y_i + \\Delta tf(y_i,t_i).$\n",
    "$$\\frac{dy}{dt} = -ky,$$\n",
    "$$y_0=1,$$\n",
    "$$k=2.$$\n",
    "\n",
    "- Use $\\Delta t = 0.2$.\n",
    "- Store $t$ and $y$ in arrays.\n",
    "- Plot $y$ versus $t$, and $y_0e^{-kt}$ versus $t$ on the same plot."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 6\n",
    "- The Haaland equation relates the friction factor $f$ in turbulent pipe flow to the Reynolds number $Re$.\n",
    "$$\\frac{1}{\\sqrt{f}} = -1.8\\log_{10}\\left[\\left(\\frac{\\epsilon/D}{3.7}\\right)^{1.11}+ \\frac{6.9}{Re}\\right].$$\n",
    "\n",
    "- Write a function to compute $f(Re, \\epsilon/D)$.\n",
    "- Create an array of 100 points that is uniformly spaced on a log scale, for $1000\\le Re\\le 1\\times 10^8$\n",
    "- Plot $f$ versus $\\log_{10}(Re)$. \n",
    "- Use $\\epsilon/D=0.001$."
   ]
  }
 ],
 "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.3"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
