Download this page as a jupyter notebook at Lab 9-TH
LAST NAME, FIRST NAME
R00000000
ENGR 1330 Laboratory 9 - Homework
# Preamble script block to identify host, user, and kernel
import sys
! hostname
! whoami
print(sys.executable)
print(sys.version)
print(sys.version_info)
atomickitty sensei /opt/jupyterhub/bin/python3 3.8.10 (default, Jun 2 2021, 10:49:15) [GCC 9.4.0] sys.version_info(major=3, minor=8, micro=10, releaselevel='final', serial=0)
Numpy is the core library for scientific computing in Python. It provides a high-performance multidimensional array object, and tools for working with these arrays. The library’s name is short for “Numeric Python” or “Numerical Python”.
A pdf file of a summary sheet (you need to download): https://s3.amazonaws.com/assets.datacamp.com/blog_assets/Numpy_Python_Cheat_Sheet.pdf
or the graphic below (same information):
Using the names below:
"Cartman, Kenny, Kyle, Stan, Butters, Wendy, Chef, Mr. Mackey, Randy, Sharon, Sheila, Towelie"
A) Create a 3x4 array and print it out
B) Sort the array alphabetically by column and print it out
C) From the sorted array, slice out a 2x2 array with ('Cartman, Randy, Sharon, Wendy'), and print it out
#import numpy
# make an arry of names
# print array
# sort and print sorted array
# slice and print sliced array
names = np.array([['Cartman','Kenny','Kyle','Stan'],['Butters','Wendy','Chef','Mr. Mackey'],['Randy','Sharon','Sheila','Towelie']])
print(names) #Step1
names_sorted = np.sort(names,axis = 0)
print(names_sorted) #Step2
names_sliced = names_sorted[1:,0:2]
print(names_sliced) #Step3
[['Cartman' 'Kenny' 'Kyle' 'Stan'] ['Butters' 'Wendy' 'Chef' 'Mr. Mackey'] ['Randy' 'Sharon' 'Sheila' 'Towelie']] [['Butters' 'Kenny' 'Chef' 'Mr. Mackey'] ['Cartman' 'Sharon' 'Kyle' 'Stan'] ['Randy' 'Wendy' 'Sheila' 'Towelie']] [['Cartman' 'Sharon'] ['Randy' 'Wendy']]
import numpy as np #import numpy
# Step1:
Array1 = np.array([0,1,2,3,4,5,6,7,8,9])
print(Array1)
# Step2:
Array2 = Array1[Array1%2 != 0]
print(Array2)
# Step3:
Array1[0] = 100
Array1[5] = 500
Array1[9] = 900
print(Array1)
# Step4:
Array2 = np.append(Array2,[11,13,15], axis=None)
print(Array2)
# Step5:
print(np.mean(Array2))
# Step6:
print(np.power(Array2,5))
[0 1 2 3 4 5 6 7 8 9] [1 3 5 7 9] [100 1 2 3 4 500 6 7 8 900] [ 1 3 5 7 9 11 13 15] 8.0 [ 1 243 3125 16807 59049 161051 371293 759375]
import numpy as np #import numpy
#Step1:
A1 = np.array([1,2,3,4,5,6,7,8])
print(A1)
#Step2:
A2 = 2*A1
print(A2)
#Step3:
A3 = 5*A1
print(A3)
[1 2 3 4 5 6 7 8] [ 2 4 6 8 10 12 14 16] [ 5 10 15 20 25 30 35 40]
You will find this link helpful for the nexttwo exercises: https://www.codecademy.com/learn/learn-linear-algebra/modules/math-ds-linear-algebra/cheatsheet
Consider the linear system given by
$$\mathbf{A} \cdot \mathbf{x} = \mathbf{B}$$where
\begin{gather} \mathbf{A}= \begin{pmatrix} 8.17\times10^6 & 0 &-0.5\times10^6\\ -1.5\times10^6 & 7.21\times10^6 & 0 \\ 0 & -1.5\times10^6 & 11.5\times10^6 \\ \end{pmatrix} ~\mathbf{b}= \begin{pmatrix} 2.0\times10^9 \\ 4.0\times10^9 \\ 1.0\times10^9 \\ \end{pmatrix} \end{gather}Use numpy and linalg methods to find x
import numpy
amatrix = [[8.17e6,0,-0.5e6],[-1.5e6,7.21e6,0],[0,-1.5e6,11.5e6]]
A = numpy.array(amatrix)
print(A)
bvector = [2e9,4e9,1e9]
b = numpy.array(bvector)
print(b)
Ainv = numpy.linalg.inv(A)
x = Ainv@b
print(x)
[[ 8170000. 0. -500000.] [-1500000. 7210000. 0.] [ 0. -1500000. 11500000.]] [2.e+09 4.e+09 1.e+09] [254.97177212 607.83046577 166.2387564 ]
import numpy
# already read in the arrays
A = numpy.array(amatrix)
Ainv = numpy.linalg.inv(A)
b = numpy.array(bvector)
x = Ainv@b
for i in range(0,len(x)):
print(x[i])
Consider the steady-state reactor system with feedback as shown below:
A mass balance of the system assuming complete mixing in each reactor (pond, lake, vat, ...) is
\begin{gather} \begin{matrix} W_1 = & (Q+\alpha Q + v A_1) \times C_1 & + 0 \times C_2 &- \alpha Q \times C_3\\ W_2 = & -(Q+\alpha Q) \times C_1 & + (Q+\alpha Q + v A_2) \times C_2 & 0 \times C_3 \\ W_3 = & 0 \times C_1 & - (Q+\alpha Q)\times C_2 & +(Q+\alpha Q + v A_3) \times C_3\\ \end{matrix} \end{gather}where $W_i$ is the loading in kilograms/year , $A_i$ is the reactor surface area in $10^6$ meters squared, $v$ is the settling rate in each reactor in meters/year, $\alpha$ is the recycle fraction, and $Q$ is the volumetric flow rate through the system. The unknown values are the constituient concentrations $C_i$ in each reactor.
Decomposing the system of equations above into a Matrix-vector system $\mathbf{A} \cdot \mathbf{C} = \mathbf{W}$, where $\mathbf{A}$ is the coefficient matrix (which we will build using $Q$,$\alpha$, and $v A_i$), $\mathbf{C}$ is the vector of unknown concentrations of the constituients, and $\mathbf{W}$ is the loading vector yields:
\begin{gather} \begin{pmatrix} (Q+\alpha Q + v A_1) & + 0 &- \alpha Q \\ -(Q+\alpha Q) & + (Q+\alpha Q + v A_2) & 0 \\ 0 & - (Q+\alpha Q)& +(Q+\alpha Q + v A_3) \\ \end{pmatrix} \cdot \begin{pmatrix} C_1 \\ C_2 \\ C_3 \\ \end{pmatrix} ~=~ \begin{pmatrix} W_1 \\ W_2 \\ W_3 \\ \end{pmatrix} \end{gather}Complete a script that constructs $\mathbf{A}$ given $Q=1\times10^6$ cubic meters/year, $\alpha$ = 0.5, and $v$ = 10 meters/year. Estimate the reactor constituient concentrations in parts per million (milligrams).
Now repeat the computation with $\alpha$ = 0.0
import numpy
# constants and arrays
Q = 1.0e6 # cubic meters/year
alpha = 0.5 # dimensionless
v = 10 # meters/year
Area = numpy.array([0.667,0.571,1.000])
Load = numpy.array([2000.,4000.,1000.])
# get units correct
Load = Load*1e6 #Load in milligrams
Area = Area*1e6 #Area in million squre meters
# coefficient array
Amat = numpy.array([[Q*(1+alpha)+v*Area[0],0,-alpha*Q],[-Q*(1+alpha),Q*(1+alpha)+v*Area[1],0],[0,-Q*(1+alpha),Q*(1+alpha)+v*Area[2]]])
# invert and solve for c
c = numpy.linalg.inv(Amat)@Load
print(numpy.round(c,0))
[255. 608. 166.]
import numpy
# constants and arrays
Q = 1.0e6 # cubic meters/year
alpha = 0.0 # dimensionless
v = 10 # meters/year
Area = numpy.array([0.667,0.571,1.000])
Load = numpy.array([2000.,4000.,1000.])
# get units correct
Load = Load*1e6 #Load in milligrams
Area = Area*1e6 #Area in million squre meters
# coefficient array
Amat = numpy.array([[Q*(1+alpha)+v*Area[0],0,-alpha*Q],[-Q*(1+alpha),Q*(1+alpha)+v*Area[1],0],[0,-Q*(1+alpha),Q*(1+alpha)+v*Area[2]]])
# invert and solve for c
c = numpy.linalg.inv(Amat)@Load
print(numpy.round(c,0))
[261. 635. 149.]