{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Homework 7\n", "====\n", "#### CHE 116: Numerical Methods and Statistics\n", "Prof. Andrew White\n", "\n", "Version 1.0 (2/22/2016)\n", "\n", "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. System of Equations (5 Points)\n", "====\n", "\n", "Solve this system of equations using the `linalg` package. Answer in Python.\n", "\n", "$$\\begin{array}{lr}\n", "6 x + 4 y & = z^2\\\\\n", "x - y & = 6 \\\\\n", "2x - 2y & = z^2 - 4 \\\\\n", "\\end{array}$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. Chemical Reaction (6 Points)\n", "===\n", "\n", "A set of frist-order chemical reactions can be described by the following system of differential equations:\n", "\n", "$$\\begin{array}{lr}\n", "\\cfrac{dC_1(t)}{dt} = & -2 C_1(t) + C_2(t) + C_3(t)\\\\\n", "\\cfrac{dC_2(t)}{dt} = & 2 C_1(t) - 4 C_2(t)\\\\\n", "\\cfrac{dC_3(t)}{dt} = & 3 C_2(t) - C_3(t)\\\\\n", "\\end{array}$$\n", "\n", "Answer the following questions:\n", "\n", "1. [1] Write down this system of ODEs using a matrix notation\n", "2. [2] Mass is conserved in these equations. How can you tell from the matrix?\n", "3. [1] Find the eigenvalues and eigenvectors for the coefficient matrix. It is *not* Hermitian. Use `linalg`.\n", "4. [2] One of the eigenvalues is special. Make an argument, using math and python, for why this is significant with respect to equilibrium." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. Python Practice (20 Points)\n", "====\n", "\n", "All your functions must have docstrings for full credit.\n", "\n", "1. [4] Create a button and text input, where the value of the text box is printed out. Make it so that the output area is cleared each time the button is pressed.\n", "\n", "2. [4] Create a button that prints a random integer from 0 to 10 using the `random.randint` function.\n", "\n", "3. [4] Make a list of strings. Using your button from part 2, now have it print a random string from your list. \n", "\n", "4. [8] Take the following matrix: `[[3, 2, -6], [2, 6, 4], [3, 4, 0]]` and use an interaction widget to display its eigenvalues and eigenvectors. Your slider should go from 0 to 2 and each value should result in a latex display showing the eigenvalue and eigenvector. Note that Python eats `{}` in strings, so you'll have to use `{{}}`. This is called escaping. Python also eats many things that have a backslash. For example, `\\b` means backspace to python. And `\\\\` means `\\` in python. So you'll have to write `\\\\` when you want LaTeX to see `\\` and in general use some trial in error about backslashes. You can never have too many though! For example, write `\\\\begin{{array}}` to start your matrix. Use three `''' for example'''` to have a string that spans multiple lines. [Summary comic](http://xkcd.com/1638/). Practice getting the LaTeX correct before putting it all together." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. Integration (8 Points)\n", "====\n", "\n", "Compute the following integrals using `scipy`. Report all your answers using `display.Latex` and only three decimal places.\n", "\n", "1. $$\\int_0^1 \\sin^2(x)\\, dx$$\n", "\n", "2. $$\\int_0^\\infty x^{-2}\\, dx$$\n", "\n", "3. Integrate the normal distribution with $\\sigma = 2$, $\\mu = -4$ from $-2$ to $2$. Do not use `scipy.stats`\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "5. Numerical Integration/Differentiation (12 Points)\n", "===\n", "\n", "1. Compute and plot the numerical derivatives of the data given in the next cell. Use a for loop and the central difference rule\n", "2. Repeat part 1 using numpy arrays.\n", "2. Compute the integral of the numerical data using the trapezoidal rule. Use a for loop.\n", "3. Repeat part 3 with numpy" ] }, { "cell_type": "code", "execution_count": 87, "metadata": { "collapsed": true }, "outputs": [], "source": [ "data_5_x = [0.0, 0.2857, 0.5714, 0.8571, 1.1429, 1.4286, 1.7143, 2.0, 2.2857, 2.5714, 2.8571, 3.1429, 3.4286, 3.7143, 4.0, 4.2857, 4.5714, 4.8571, 5.1429, 5.4286, 5.7143, 6.0, 6.2857, 6.5714, 6.8571, 7.1429, 7.4286, 7.7143, 8.0, 8.2857, 8.5714, 8.8571, 9.1429, 9.4286, 9.7143, 10.0, 10.2857, 10.5714, 10.8571, 11.1429, 11.4286, 11.7143, 12.0, 12.2857, 12.5714, 12.8571, 13.1429, 13.4286, 13.7143, 14.0]\n", "data_5_y = [67.9925, 67.5912, 67.4439, 66.7896, 66.4346, 66.3176, 65.7527, 65.1487, 65.7247, 65.1831, 64.5981, 64.5213, 63.6746, 63.9106, 62.6127, 63.3892, 62.6511, 62.601, 61.9718, 60.5553, 61.5862, 61.3173, 60.5913, 59.7061, 59.6535, 58.9301, 59.346, 59.2083, 60.3429, 58.752, 57.6269, 57.5139, 59.0293, 56.7979, 56.2996, 56.4188, 57.1257, 56.1569, 56.3077, 55.893, 55.4356, 56.7985, 55.6536, 55.8353, 54.4404, 54.2872, 53.9584, 53.3222, 53.2458, 53.7111]" ] } ], "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.4.2" } }, "nbformat": 4, "nbformat_minor": 0 }