Practice Final Exam, ChE 263

This exam covers the following topics:

  • Problem 1: Variable types and formats
  • Problem 2: Debugging
  • Problem 3: Solve Linear Equations
  • Problem 4: Differentiate and Integrate
  • Problem 5: Interpolation
  • Problem 6: General Nonlinear Regression and Plotting
  • Problem 7: Equation Solution and Plotting
  • Problem 8: Parameter Estimation
  • Problem 9: Implicit Equation Solve

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.

In [ ]:
# import packages

Problem 1: Variable types and formats

Part A

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.

Hint: A string is converted to a number with int() or float().

In [ ]:
 

Part B

Show $\pi$ to 30 decimal places.

In [ ]:
 

Problem 2: Debugging

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.

import time
for i in range(5,1)
    time.wait(1.0)
    print(i)
print('Blast off")

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:

5
4
3
2
1
0
Blast off
In [ ]:
 

Problem 3: Solve Linear Equations

Find the values of $x_0$, $x_1$ that satisfy the following equations:

$5 x_0 + x_1 = 2$

$3 x_0 + 12 x_1 = 1$

Put equations into matrix form with $A \;x = b$ and solve $x = A^{-1}\; b$. The $A$ matrix is given below.

In [ ]:
 

Problem 4: Differentiate and Integrate

Part A

Compute the derivative $\frac{d f(x)}{dx}$ for the following function:

$f(x) = \frac{1}{1+e^{-x}}$

Find a solution with scipy (numerically) and sympy (symbolically). For the numeric solution (scipy), use value of $x=0.2$.

In [ ]:
 
In [ ]:
 

Part B

Compute the integral for the function:

$f(x) = \frac{1}{1+e^{-x}}$

Find a solution with scipy (numerically) and sympy (symbolically). For the numeric solution, use limits of integration for $x$ between $-1$ and $1$:

$\int_{-1}^{1} \left(\frac{1}{1+e^{-x}}\right) dx$

In [ ]:
 
In [ ]:
 

Problem 5: Interpolation

Data for x and y is shown below:

x = [1,3,7,10]
y = [2,9,20,35]

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.

In [ ]:
 

Problem 6: Nonlinear Regression and Plotting

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:

$P = A \, e^{n\,t}$

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.

In [ ]:
 

Problem 7: Equation Solution and Plotting

Part A

The ideal gas law is shown below:

$P V_m=R_g T$

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}$.

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$.

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.

In [ ]:
 

Part B

Repeat part A but use the non-ideal gas Van der Waals Equation of State:

$P+\frac{a}{V_m^2}=\frac{R_g T}{V_m-b}$

with constants $a$ and $b$ for ethanol with critical properties $T_c=514 K$ and $P_c=6.137\mathrm{x}10^6 Pa$:

$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}$

$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}$

Compare the ideal gas and non-ideal gas results on the same plot.

In [ ]:
 

Problem 8: Parameter Estimation and Statistics

The yield (concentration) of green fluorescent protein produced in a reaction over time has been determined and is shown below.

Time (minutes)  Concentration (mg/mL)
15              107.32
30              203.05
45              341.26
60              401.24
180             844.01
480             1135.12
720             1374.70
1200            1651.26

Fit the data to the equation

$G = A t^2 + B\ln{t}+D$

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$.

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.

In [ ]:
 
In [ ]:
 

9. Implicit Equation Solve

Determine the two roots of the polynomial using fsolve from scipy.optimize.

$x^2+3x+2=0$

Verify the solutions from the quadratic formula:

$\frac{-b\pm \sqrt{b^2-4ac}}{2a}$

with solutions:

$x_0=\frac{-3+\sqrt{9-8}}{2} = \frac{-2}{2} = -1$

$x_1=\frac{-3-\sqrt{9-8}}{2} = \frac{-4}{2} = -2$

A better way to find polynomial roots is with numpy such as

import numpy as np
np.polynomial.polynomial.polyroots([2,3,1])

but use this exercise to practice fsolve and how to use a different initial guess to find alternate solutions.

In [ ]:
 
In [ ]:
 
In [ ]: