{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Homework 9"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 1\n",
    "\n",
    "Carefully read all of the [notes on functions](http://nbviewer.jupyter.org/url/ignite.byu.edu/cbe263/lectures/python_functions_worked.ipynb).\n",
    "Pay special attention to the code cells.\n",
    "\n",
    "Just state if you did this."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 2\n",
    "\n",
    "There are lots of libraries available for importing functions in python. One of the most important is numpy. \n",
    "\n",
    "Type the following in a code cell\n",
    "\n",
    "```python\n",
    "import numpy as np\n",
    "```\n",
    "\n",
    "This imports the numpy library. To take $\\sqrt x$ we use `np.sqrt(x)`\n",
    "* The `np.` part is because we imported the library `as np`.\n",
    "* If we had just done `import numpy` then we would use `numpy.sqrt(x)`, but that is less concise.\n",
    "\n",
    "Browse the functions [at this site](https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.math.html) and try a couple of them out.\n",
    "\n",
    "Here is another good [numpy reference](https://docs.scipy.org/doc/numpy-1.15.1/reference/)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 3\n",
    "\n",
    "Write a function to compute the volume of a sphere given it's radius.\n",
    "Get $\\pi$ from numpy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 4\n",
    "\n",
    "Write a function to compute the vapor pressure of a species given $A$, $B$, and $C$ according to the following equation\n",
    "\n",
    "$$P_i^{sat} = \\exp\\left(A + \\frac{B}{T+C}\\right).$$\n",
    "\n",
    "Assume $T$ has units of K. Include a docstring documenting the function. Use numpy for $\\exp$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 5\n",
    "A function can return more than two quantities, say `x1` and `x2` if you use the following return statement\n",
    "`return x1, x2`, where `x1` and `x2` are variables or expressions.\n",
    "You would the call the function as\n",
    "`x1,x2 = f(a,b,c)`.\n",
    "\n",
    "Write a function that will return the two roots of a quadratic equation $ax^2+bx+c=0$, given $a$, $b$, and $c$. Assume the roots are real. \n",
    "\n",
    "Try out the function for $x^2-5x+5$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 6\n",
    "\n",
    "The first derivative of a function $f(x)$ can be approximated as\n",
    "$$\\frac{df(x)}{dx} \\approx \\frac{f(x+\\Delta x) - f(x)}{\\Delta x}.$$\n",
    "A good estimate for $\\Delta x$ is \n",
    "$$\\Delta x = \\epsilon|x|,$$\n",
    "where $\\epsilon$ is a small number.\n",
    "\n",
    "Write a function called ```derivative``` that takes ```f```, ```x```, and ```eps``` as arguments and returns the derivative evaluated at ```x```. Here, ```f``` is a function, ```x``` is where you are evaluating the derivative, and ```eps``` is $\\epsilon$ in the above equation. Give ```eps``` a default value of $10^{-8}$. \n",
    "\n",
    "Test it out on the following\n",
    "* $f(x) = x^2$ for $x=3$.\n",
    "* $f(x) = \\sin(x)$ for $x=\\pi/2$\n",
    "* $f(x) = \\exp(x)$ for $x=1.01325$\n",
    "\n",
    "In each case, compare to the exact solution.\n",
    "\n",
    "For $\\sin$ and $\\exp$, you can use numpy and call the derivative function like so:\n",
    "```python\n",
    "d = derivative(np.sin, np.pi/2)\n",
    "d = derivative(np.exp, 1.01325)\n",
    "```\n",
    "For $f(x)=x^2$, you will need to write your own function that you can pass to the ```derivative``` function.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 7\n",
    "\n",
    "Have AI solve Problem 6 and copy the resulting code here? Which AI tool did you use? What is different? Do you have any questions about its results? Do you understand its code?"
   ]
  },
  {
   "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.4"
  },
  "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": 1
}
