{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Practice Final Exam, ChE 263\n", "\n", "This exam covers the following topics:\n", "\n", "* Problem 1: Variable types and formats\n", "* Problem 2: Debugging\n", "* Problem 3: Solve Linear Equations\n", "* Problem 4: Differentiate and Integrate\n", "* Problem 5: Interpolation\n", "* Problem 6: General Nonlinear Regression and Plotting\n", "* Problem 7: Equation Solution and Plotting\n", "* Problem 8: Parameter Estimation\n", "* Problem 9: Implicit Equation Solve\n", "\n", "A template for each problem is provided with instructions on what is needed. In some cases, the problem is to debug a section of code to produce a specific result. In other cases, the problem is to import a module and produce a result." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# import packages" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 1: Variable types and formats\n", "\n", "#### Part A\n", "Add **x=2.0** (float) and **y='3'** (string) together to produce a number 5 (integer). Show the type of variable with **type()** to confirm that the result is an integer.\n", "\n", "Hint: A string is converted to a number with **int()** or **float()**." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Part B\n", "\n", "Show $\\pi$ to 30 decimal places." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 2: Debugging\n", "\n", "You are working on the launch sequence of a rocket. The following code is intended to count backwards from 5 to 0 with steps of -1 in 1 second intervals. Print `blast off` after reaching zero.\n", "\n", "```python\n", "import time\n", "for i in range(5,1)\n", " time.wait(1.0)\n", " print(i)\n", "print('Blast off\")\n", "```\n", "\n", "The code has a few bugs (errors) that prevent it from running or producing the correct result. Find the errors in the code to produce:\n", "\n", "```\n", "5\n", "4\n", "3\n", "2\n", "1\n", "0\n", "Blast off\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 3: Solve Linear Equations\n", "\n", "Find the values of $x_0$, $x_1$ that satisfy the following equations:\n", "\n", "$5 x_0 + x_1 = 2$\n", "\n", "$3 x_0 + 12 x_1 = 1$\n", "\n", "Put equations into matrix form with $A \\;x = b$ and solve $x = A^{-1}\\; b$. The $A$ matrix is given below." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 4: Differentiate and Integrate\n", "\n", "#### Part A\n", "\n", "Compute the derivative $\\frac{d f(x)}{dx}$ for the following function:\n", "\n", "$f(x) = \\frac{1}{1+e^{-x}}$\n", "\n", "Find a solution with scipy (numerically) and sympy (symbolically). For the numeric solution (scipy), use value of $x=0.2$." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Part B\n", "\n", "Compute the integral for the function:\n", "\n", "$f(x) = \\frac{1}{1+e^{-x}}$\n", "\n", "Find a solution with scipy (numerically) and sympy (symbolically). For the numeric solution, use limits of integration for $x$ between $-1$ and $1$:\n", "\n", "$\\int_{-1}^{1} \\left(\\frac{1}{1+e^{-x}}\\right) dx$" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 5: Interpolation\n", "\n", "Data for `x` and `y` is shown below:\n", "\n", "```python\n", "x = [1,3,7,10]\n", "y = [2,9,20,35]\n", "```\n", "\n", "Create a cubic spline interpolation to approximate the value of `y` at `x=5`. Show a cubic spline interpolation on a plot together with the data points. Label the plot with appropriate $x$ and $y$ axis labels, and a legend. Save the plot as `plot.png`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 6: Nonlinear Regression and Plotting\n", "\n", "Pressure in a tank is recorded as $P = [1.5,2.6,3.5,10.2,20.3,30.2]$ at respective times of $[0,1,2,3,4,5]$ min. Create a nonlinear approximation of the pressure trend as:\n", "\n", "$P = A \\, e^{n\\,t}$\n", "\n", "where $t$ is time, $A$ is an unknown pre-exponential factor, and $n$ is also an unknown parameter. Show the optimized parameter values as well as a plot with the data points." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 7: Equation Solution and Plotting\n", "\n", "#### Part A\n", "\n", "The ideal gas law is shown below:\n", "\n", "$P V_m=R_g T$\n", "\n", "where $P$ is the pressure, $V_m$ is the molar volume, $T$ is the temperature, and $R_g$ is the universal gas constant. Use $R_g=8.314 \\frac{J}{mol\\,K}$.\n", "\n", "Create the function $P(V_m,T)$ such that the pressure ($P$) is a function of molar volume $\\left(V_m\\right)$ and temperature ($T$). Convert the pressure value from $Pa$ to $bar$.\n", "\n", "Use the function and range variables to create a $P$ vs $T$ plot where $T$ ranges from $100K$ to $1200K$ and $V_m$ = 2.24 $\\frac{L}{mol}$ = 0.00224 $\\frac{m^3}{mol}$ . Include $x$ and $y$ labels on the plot and a legend." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Part B\n", "\n", "Repeat part A but use the non-ideal gas Van der Waals Equation of State:\n", "\n", "$P+\\frac{a}{V_m^2}=\\frac{R_g T}{V_m-b}$\n", "\n", "with constants $a$ and $b$ for ethanol with critical properties $T_c=514 K$ and $P_c=6.137\\mathrm{x}10^6 Pa$:\n", "\n", "$a = 25 \\frac{\\left(R_g T_c\\right)^2}{64 P_c} = 25 \\frac{\\left(8.314 \\frac{J}{mol\\,K} 514 K\\right)^2}{64 \\,\\mathrm{x}\\,6.137\\mathrm{x}10^6 Pa}$ = $1.1623 \\frac{Pa\\,mol^2}{m^6}$\n", "\n", "$b = \\frac{R_g T_c}{8 P_c} = \\frac{8.314 \\frac{J}{mol\\,K} 514 K}{8 \\,\\mathrm{x}\\,6.137\\mathrm{x}10^6 Pa}$ = $8.70416\\mathrm{x}10^{-5} \\frac{m^3}{mol}$\n", "\n", "Compare the ideal gas and non-ideal gas results on the same plot." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem 8: Parameter Estimation and Statistics\n", "\n", "The yield (concentration) of green fluorescent protein produced in a reaction over time has been determined and is shown below.\n", "\n", "```\n", "Time (minutes) Concentration (mg/mL)\n", "15 107.32\n", "30 203.05\n", "45 341.26\n", "60 401.24\n", "180 844.01\n", "480 1135.12\n", "720 1374.70\n", "1200 1651.26\n", "```\n", "\n", "Fit the data to the equation \n", "\n", "$G = A t^2 + B\\ln{t}+D$\n", "\n", "Where $G$ is the concentration of GFP in mg/mL at a given time ($t$) in minutes and $A$, $B$, and $D$ are adjustable parameters to minimize the difference between predicted and measured $G$.\n", "\n", "Solve for $A$, $B$, and $D$ to fit the data. Create a plot of the measured and predicted values and determine the $R^2$ value." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 9. Implicit Equation Solve\n", "\n", "Determine the two roots of the polynomial using `fsolve` from `scipy.optimize`.\n", "\n", "$x^2+3x+2=0$\n", "\n", "Verify the solutions from the quadratic formula:\n", "\n", "$\\frac{-b\\pm \\sqrt{b^2-4ac}}{2a}$\n", "\n", "with solutions:\n", "\n", "$x_0=\\frac{-3+\\sqrt{9-8}}{2} = \\frac{-2}{2} = -1$\n", "\n", "$x_1=\\frac{-3-\\sqrt{9-8}}{2} = \\frac{-4}{2} = -2$\n", "\n", "A better way to find polynomial roots is with `numpy` such as\n", "\n", "```python\n", "import numpy as np\n", "np.polynomial.polynomial.polyroots([2,3,1])\n", "```\n", "\n", "but use this exercise to practice `fsolve` and how to use a different initial guess to find alternate solutions." ] }, { "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.7.4" } }, "nbformat": 4, "nbformat_minor": 1 }