{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Details\n", "\n", "This is a take home quiz to test your knowledge of Python in application to the Reactor Design. You are allowed to use any textbooks, look through online resources such as stackoverflow and use the material discussed in class/tutorials. You are strongly encouraged to invest time into this quiz since it will help you catch up with the Python course. You can also use online resources such as https://www.codecademy.com/learn/learn-python to help you grasp the basics of python better.\n", "Another good video is https://www.youtube.com/watch?v=ezuRn1ViIJE.\n", "\n", "Details about the quiz:\n", "\n", "Start time: Thursday March 8 3pm\n", "\n", "Submission deadline: Monday March 12 6 pm\n", "\n", "To submit your quiz finish the exercises in the Jupyter notebook and download it as a PDF and print it. If something goes wrong in opening the Jupyter notebook you may open the PDF (with the quiz) copy paste into a new notebook and then save it. Make sure to include your name and student number.\n", "\n", "I strongly encourage you to open your emails with the information about the anaconda python distribution. Syzygy works as well, but you may experience some problems while running it.\n", "\n", "Good luck!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Student name:** < >\n", "\n", "**Student number:** < > " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "# Problem 1: basics of if/else\n", "\n", "Design a function that will calculate a maximum of two numbers:\n", "\n", "Call this function `max(a, b)`\n", "\n", "Sample output:\n", "\n", "```python\n", "max(2, 3)\n", "3\n", "max(4, 19)\n", "19\n", "max(3, 3)\n", "3\n", "```\n", "\n", "Write the code in the cell below and test your code on the examples above.\n", " " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 2: basics of loops\n", "\n", "Design a function that will calculate factorial of a number.\n", "\n", "By definition factorial of a number is multiplication of all numbers that are <= of the given number, e.g. `4! = 1*2*3*4 = 24`\n", "\n", "Name the designed function as `factorial(k)`. The sample output will be:\n", "\n", "```python\n", "factorial(1)\n", "1\n", "factorial(2)\n", "2\n", "factorial(3)\n", "6\n", "factorial(4)\n", "24\n", "```\n", "\n", "Write the code in the cell below and test your code on the examples above." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 3: matplotlib and numpy\n", "\n", "Lets use the power of Python to tackle CHBE 346 problems.\n", "\n", "Calculate and plot the pressure of saturated vapor of ethanol as a function of temperature in the temperature range from $T_{min} = -57^o C$ to $T_{max}=80^o C$. Create two plots one for pressure in mmHg and the other in kPa. \n", "\n", "In case some of you have forgotten (but I doubt you have) here is an inspiration on where to get the equation for the saturated pressure:\n", "\n", "https://en.wikipedia.org/wiki/Antoine_equation\n", "\n", "To plot you can use the following preamble to import the neccessary python modules:\n", "\n", "```python\n", "import numpy as np # our matlab-like module\n", "import matplotlib.pyplot as plt # plotting modules \n", "plt.style.use('presentation') # just have in your script for prettier plotting \n", "%matplotlib inline\n", "# if 'presentation' doesn't work use 'seaborn' or 'ggplot'\n", "```\n", "\n", "Make sure you assign the xlabel and ylabel\n", "```python\n", "plt.xlabel('T [C]')\n", "plt.ylabel('P [mmHg]')\n", "```\n", "\n", "In case you want to save what you have plotted you can use this command:\n", "```python\n", "plt.savefig('pressure_temp.png')\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 4: solving ODEs for Batch reactor problems\n", "\n", "Zero-th order chemical reaction.\n", "\n", "Imagine you have got the simplest possible reaction kinetics for your batch reactor\n", "\n", "$-r_A = k \\gt 0$\n", "\n", "Your design equation for this reaction in Batch is:\n", "\n", "$dC_A/dt = - k$, where\n", "\n", "$C_{A0} = 10$ moles/L\n", "\n", "$k = 0.1$ moles/(s*L)\n", "\n", "\n", "\n", "1. Calculate and plot: Analytical solution for the given design equation with given parameters $C_{A0}$ and $k$\n", "2. Calculate and plot: Numerical solution for the same equation using the scipy module `odeint`. Plot curves from 1 and 2 on the same graph.\n", "3. Find time $t^*$ when the concentration of the reacting component is equal to zero, i.e. $C_A(t^*) = 0$\n", "\n", "For your code below you can use the following preamble:\n", "\n", "```python\n", "import matplotlib.pyplot as plt # plotting modules\n", "%matplotlib inline \n", "plt.style.use('presentation') # just have in your script for prettier plotting\n", "# insted of 'presentation' you can use 'ggplot'\n", "import numpy as np # our matlab-like module\n", "from scipy.integrate import odeint # integration of ODEs so you don't have to write your finite difference yourself\n", "```\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 5: CSTR and solving algebraic equations\n", "\n", "\n", "Given a continuously stirred tank reactor with a volume V = 66 $m^3$ with a reaction $A \\to B$. The rate of the reaction is:\n", "\n", "$−r_A= k C^2_A$ (k=3 L/mol^3/h)\n", "\n", "The entering molar flow $F_{A0} = 5 mol/$h and the volumetric flow rate is $v_0 = 10 L/h$\n", "\n", "1. Given that the exit concentration is $C_A = 0.005$ mol/L, how much of A (in percent) has reacted away?\n", "\n", "2. If $v_0$ = 10 L/h what would be the exit concentration $C_A$?\n", "\n", "3. For different values of $v_0 = range(1,21) = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]$ L/h what would be the exit concentration $C_A$? Plot $C_A$ as a function of $v_0$.\n", "\n", "\n", "Just a reminder for a CSTR with zero accumulation rate the design equation looks like this:\n", "\n", "$0=F_{A0}−F_A+V \\cdot r_A$, where\n", "\n", "$F_A=v_0 \\cdot C_A$\n", "\n", "To solve the algebraic equation please use the following preamble:\n", "```python\n", "from scipy.optimize import fsolve\n", "import numpy as np\n", "```\n", "\n", "To do the step 3: you can follow this template:\n", "\n", "```python\n", "v_0_array = np.arange(1,21)\n", "C_A_array = np.zeros(20) # just allocating the memmory with dummy values 0\n", "\n", "for i in range(1,21):\n", " v0 = v_0_array[i]\n", " C_A_array[i] = # do the calculation here and put the value C_A\n", "```\n", "\n", "Then to plot:\n", "\n", "```python\n", "import matplotlib.pyplot as plt # plotting modules\n", "%matplotlib inline \n", "plt.style.use('ggplot') \n", "import numpy as np # our matlab-like module\n", "\n", "plt.plot(v_0_array, C_A_array, 'go--')\n", "```\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Problem 6: Linear regression\n", "\n", "1. Fit the data below using $f(x) = A \\cdot x \\cdot e^{-k \\cdot x}$\n", " a. Plot (xdata, ydata) and (xdata, f(xdata) with the optmized parameters\n", " b. Print the optimized parameters\n", "2. (Bonus) Calculate the $\\chi^2$ parameter of fitting\n", "$\\chi^2 = \\Sigma (ydata[i] - f(xdata[i])^2$\n", "\n", "You can use this preamble for this exercise:\n", "\n", "```python\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline \n", "plt.style.use('presentation') # insted of 'presentation' you can use 'ggplot'\n", "from scipy.optimize import curve_fit # this guy will help us fit everything\n", "```\n", "\n", "\n", "\n", "Data:\n", "\n", "```python\n", "import numpy as np\n", "xdata = np.array([0. , 0.02020202, 0.04040404, 0.06060606, 0.08080808,\n", " 0.1010101 , 0.12121212, 0.14141414, 0.16161616, 0.18181818,\n", " 0.2020202 , 0.22222222, 0.24242424, 0.26262626, 0.28282828,\n", " 0.3030303 , 0.32323232, 0.34343434, 0.36363636, 0.38383838,\n", " 0.4040404 , 0.42424242, 0.44444444, 0.46464646, 0.48484848,\n", " 0.50505051, 0.52525253, 0.54545455, 0.56565657, 0.58585859,\n", " 0.60606061, 0.62626263, 0.64646465, 0.66666667, 0.68686869,\n", " 0.70707071, 0.72727273, 0.74747475, 0.76767677, 0.78787879,\n", " 0.80808081, 0.82828283, 0.84848485, 0.86868687, 0.88888889,\n", " 0.90909091, 0.92929293, 0.94949495, 0.96969697, 0.98989899,\n", " 1.01010101, 1.03030303, 1.05050505, 1.07070707, 1.09090909,\n", " 1.11111111, 1.13131313, 1.15151515, 1.17171717, 1.19191919,\n", " 1.21212121, 1.23232323, 1.25252525, 1.27272727, 1.29292929,\n", " 1.31313131, 1.33333333, 1.35353535, 1.37373737, 1.39393939,\n", " 1.41414141, 1.43434343, 1.45454545, 1.47474747, 1.49494949,\n", " 1.51515152, 1.53535354, 1.55555556, 1.57575758, 1.5959596 ,\n", " 1.61616162, 1.63636364, 1.65656566, 1.67676768, 1.6969697 ,\n", " 1.71717172, 1.73737374, 1.75757576, 1.77777778, 1.7979798 ,\n", " 1.81818182, 1.83838384, 1.85858586, 1.87878788, 1.8989899 ,\n", " 1.91919192, 1.93939394, 1.95959596, 1.97979798, 2. ])\n", "\n", "ydata = np.array([ 2.81790690e-03, 5.90294109e-02, 1.09370354e-01, 1.31612856e-01,\n", " 1.33521472e-01, 1.55179411e-01, 1.59437733e-01, 1.43762661e-01,\n", " 1.35183487e-01, 1.16512815e-01, 1.07702053e-01, 9.67242987e-02,\n", " 1.12202764e-01, 6.90304807e-02, 7.15441262e-02, 6.79904733e-02,\n", " 3.16005654e-02, 4.56444047e-02, 3.32902755e-02, 4.51933972e-02,\n", " 2.97970567e-02, 2.11531961e-02, 1.70962388e-02, 1.53322820e-02,\n", " 1.61206486e-02, 3.31898262e-02, 1.08205741e-02, 1.65291108e-02,\n", " 4.24013381e-04, -3.47150134e-03, 8.98809610e-03, 1.61384528e-02,\n", " 1.89444656e-02, 1.05952488e-03, 5.15202567e-03, 1.20937619e-02,\n", " -1.53297244e-02, -4.98358443e-03, -3.04000556e-03, 4.59154157e-04,\n", " -1.00965453e-02, 1.08341542e-02, 5.16711834e-03, -2.15908939e-04,\n", " 4.96411518e-04, 3.92671205e-03, 6.95104585e-04, 8.02129061e-03,\n", " 1.74642041e-03, -2.13299969e-03, -1.44225240e-03, -3.91799324e-03,\n", " -8.78295329e-04, 1.04170809e-02, -1.35132175e-02, 2.11129087e-03,\n", " -7.43327553e-03, 1.21894042e-04, 6.57066929e-03, 1.86150924e-03,\n", " 7.46818689e-03, 3.02554354e-03, -2.50852788e-03, 1.71197306e-02,\n", " -3.17197910e-05, 1.31898549e-02, 1.55137128e-03, 1.04708955e-03,\n", " 1.60318797e-02, 2.29432131e-02, 1.61009431e-02, -8.61571685e-03,\n", " -2.36921503e-03, 1.26538983e-03, -1.50510060e-02, 1.22197433e-02,\n", " 1.11696644e-02, -3.66362776e-03, 9.24461107e-03, 1.15448488e-02,\n", " 5.81401176e-04, 1.07067752e-03, -8.02071597e-03, 6.32535856e-03,\n", " -4.05432003e-03, -1.64541547e-02, 2.99370980e-03, 2.04344112e-03,\n", " 2.07861200e-02, 1.25646189e-02, -5.45704279e-04, -5.27434229e-03,\n", " 9.66709541e-03, -8.43414313e-03, 4.08371369e-05, -5.36371330e-03,\n", " -3.27141551e-03, 2.57467424e-03, 8.27639029e-03, -8.23343126e-03])\n", "```\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "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" } }, "nbformat": 4, "nbformat_minor": 2 }