Homework 14¶
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
Before doing the homework, review the notes on arrays. Memorize the key functions. Practice.
Problem 1¶
- Make an array of $x$ from 0 to 5 with 1000 points.
- create the following arrays
- $2x$
- $x^2 -3x + 4$
- $\sin(5x)$
- Plot each array versus $x$ on a single plot.
Problem 2¶
- Ideal gas law $$\rho = \frac{MP}{RT}$$
- M=29 kg/kmol
- R=8314 J/kmol*K
- P=101325 Pa
- Find $\rho$ for $T=300,400,\ldots1000$ K
- Store $\rho$ and $T$ as arrays
- Plot $\rho$ versus $T$
Problem 3¶
- Redo Problem 2 by writing a function for $\rho(T)$ as you would have in a previous lecture.
- Call the function with an array of $T$ and store the result as an array of $\rho$.
- Plot $\rho$ versus $T$.
Problem 4¶
- You have an array of $x$ points $x=0.0, 0.1, \ldots, 50$.
- Create $x$
- Create $y=\sin(x)$
- At interior points, use a central difference approximation to find the derivative $$\left(\frac{dy}{dx}\right)_i \approx \frac{y_{i+1}-y_{i-1}}{2\Delta x},$$ where $\Delta x = x_1-x_0$.
- At edge point use a one-sided difference approximation, $$\left(\frac{dy}{dx}\right)_0 \approx \frac{y_{1}-y_{0}}{\Delta x},$$ $$\left(\frac{dy}{dx}\right)_{n-1} \approx \frac{y_{n-1}-y_{n-2}}{\Delta x}.$$
- Plot $dy/dx$ versus $x$, and $\cos(x)$ versus $x$.
Problem 5¶
Solve the rate equation below to $t=5$ s using the Explicit Euler method $y_{i+1} = y_i + \Delta tf(y_i,t_i).$ $$\frac{dy}{dt} = -ky,$$ $$y_0=1,$$ $$k=2.$$
Use $\Delta t = 0.2$.
Store $t$ and $y$ in arrays.
Plot $y$ versus $t$, and $y_0e^{-kt}$ versus $t$ on the same plot.
Problem 6¶
The Haaland equation relates the friction factor $f$ in turbulent pipe flow to the Reynolds number $Re$. $$\frac{1}{\sqrt{f}} = -1.8\log_{10}\left[\left(\frac{\epsilon/D}{3.7}\right)^{1.11}+ \frac{6.9}{Re}\right].$$
Write a function to compute $f(Re, \epsilon/D)$.
Create an array of 100 points that is uniformly spaced on a log scale, for $1000\le Re\le 1\times 10^8$
Plot $f$ versus $\log_{10}(Re)$.
Use $\epsilon/D=0.001$.