#!/usr/bin/env python # coding: utf-8 # # Homework 11 # In[2]: import numpy as np import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') # ### Problem 0 (not graded) # # 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. # In[ ]: # ### 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$ # In[ ]: # ### 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$. # In[ ]: # ### Problem 4 # # We have an array of times $t$, and we want the array of differences $dt$ between times. # * How big is $t$? # * How big will $dt$ be? # * Create $dt$ using a loop. # * Create $dt$ using slice-indexing. # # # In[3]: t = np.array([ 0.01136646, 0.01655847, 0.02548247, 0.07698612, 0.17964401, 0.19813924, 0.22419994, 0.28622096, 0.28884997, 0.3627257 , 0.42555072, 0.44965767, 0.47257237, 0.59335946, 0.73842352, 0.85900369, 0.88851226, 0.89153229, 0.89355469, 0.97705246, ]) # ### Problem 5 # # * 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 $x$ versus $y$, and $x$ versus $y^{\prime}$, and $x$ versus $\cos(x)$. # In[ ]: # ### Problem 6 # * 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. # In[ ]: # ### Problem 7 # * 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$. # In[ ]: